auth
2014-09-01
Parent:1aa3a85ff853
auth/context.go.old
Rough out tokens and begin the memstore. Rough out the Token type for working with OAuth2 access and refresh tokens. Rough out the TokenStore interface that dictates how Tokens will be stored and retrieved. Write tests for the successful (in the working-as-intended sense) calls to TokenStore. Begin a Memstore type that stores data in memory. Implement the TokenStore interface for Memstore.
1 package auth
3 import (
4 "encoding/json"
5 "html/template"
6 "io"
7 "log"
8 "net/http"
10 "github.com/justinas/nosurf"
11 )
13 type Context struct {
14 Config ServerConfig
15 Clients ClientStore
16 Tokens TokenStore
17 Profiles ProfileStore
18 Sessions SessionStore
19 Log *log.Logger
20 Templates Templates
21 }
23 type Templates struct {
24 Error *template.Template
25 Confirmation *template.Template
26 Login *template.Template
27 }
29 type jsonError struct {
30 Error string `json:"error,omitempty"`
31 Description string `json:"error_description,omitempty"`
32 URI string `json:"error_uri,omitempty"`
33 State string `json:"state,omitempty"`
34 }
36 func (c Context) RenderError(w io.Writer, err error) {
37 if c.Templates.Error == nil {
38 log.Println("Error template is nil, can't render error.")
39 return
40 }
41 renderErr := c.Templates.Error.Execute(w, map[string]interface{}{
42 "err": err,
43 })
44 if renderErr != nil {
45 log.Printf("Error executing error template (oh, the irony): %s\n", renderErr)
46 return
47 }
48 }
50 func (c Context) RenderJSONError(w io.Writer, code, description, baseURI string) {
51 d, err := json.Marshal(jsonError{
52 Error: code,
53 Description: description,
54 URI: baseURI,
55 })
56 if err != nil {
57 log.Printf("Error marshalling json error (oh, the irony): %s\n", err)
58 return
59 }
60 _, err = w.Write(d)
61 if err != nil {
62 log.Printf("Error writing json error: %s\n", err)
63 return
64 }
65 }
67 func (c Context) RenderConfirmation(w io.Writer, r *http.Request, req AuthRequest) {
68 if c.Templates.Confirmation == nil {
69 log.Println("Confirmation template is nil, can't render confirmation.")
70 return
71 }
72 err := c.Templates.Confirmation.Execute(w, map[string]interface{}{
73 "scope": req.Scope,
74 "client": req.Client,
75 "csrf_token": nosurf.Token(r),
76 })
77 if err != nil {
78 log.Printf("Error executing confirmation template: %s\n", err)
79 return
80 }
81 }
83 func (c Context) RenderLogin(w io.Writer, r *http.Request) {
84 if c.Templates.Login == nil {
85 log.Println("Login template is nil, can't render confirmation.")
86 return
87 }
88 err := c.Templates.Login.Execute(w, map[string]interface{}{
89 "csrf_token": nosurf.Token(r),
90 })
91 if err != nil {
92 log.Printf("Error executing login template: %s\n", err)
93 return
94 }
95 }
97 func (c Context) RenderJSONToken(w io.Writer, data AccessData) {
98 d, err := json.Marshal(data)
99 if err != nil {
100 log.Printf("Error marshalling json token: %s\n", err)
101 return
102 }
103 _, err = w.Write(d)
104 if err != nil {
105 log.Printf("Error writing json token: %s\n", err)
106 return
107 }
108 }