auth
15:fc5df8e68c7b Browse Files
Render results. Render JSON tokens and errors. Render HTML errors, confirmation, and login pages.
access.go client.go context.go storage.go
1.1 --- a/access.go Sat Aug 16 02:57:49 2014 -0400 1.2 +++ b/access.go Sat Aug 16 05:33:23 2014 -0400 1.3 @@ -21,15 +21,16 @@ 1.4 1.5 // AccessData represents an access grant (tokens, expiration, client, etc) 1.6 type AccessData struct { 1.7 - PreviousAuthorizeData *AuthorizeData 1.8 - PreviousAccessData *AccessData // previous access data, when refreshing 1.9 - AccessToken string 1.10 - RefreshToken string 1.11 - ExpiresIn int32 1.12 - CreatedAt time.Time 1.13 - TokenType string 1.14 - ProfileID uuid.ID 1.15 - AuthRequest 1.16 + PreviousAuthorizeData *AuthorizeData `json:"-"` 1.17 + PreviousAccessData *AccessData `json:"-"` // previous access data, when refreshing 1.18 + AccessToken string `json:"access_token"` 1.19 + RefreshToken string `json:"refresh_token,omitempty"` 1.20 + ExpiresIn int32 `json:"expires_in"` 1.21 + CreatedAt time.Time `json:"-"` 1.22 + TokenType string `json:"token_type"` 1.23 + Scope string `json:"scope,omitempty"` 1.24 + ProfileID uuid.ID `json:"-"` 1.25 + AuthRequest `json:"-"` 1.26 } 1.27 1.28 // IsExpired returns true if access expired 1.29 @@ -141,6 +142,7 @@ 1.30 RedirectURI: redirectURI, 1.31 Scope: authData.Scope, 1.32 }, 1.33 + Scope: authData.Scope, 1.34 PreviousAuthorizeData: &authData, 1.35 } 1.36 1.37 @@ -207,6 +209,7 @@ 1.38 Client: client, 1.39 Scope: scope, 1.40 }, 1.41 + Scope: scope, 1.42 PreviousAccessData: &refreshData, 1.43 } 1.44 err = fillTokens(&data, true, ctx) 1.45 @@ -261,6 +264,7 @@ 1.46 Client: client, 1.47 Scope: scope, 1.48 }, 1.49 + Scope: scope, 1.50 } 1.51 1.52 err = fillTokens(&data, true, ctx) 1.53 @@ -297,6 +301,7 @@ 1.54 Client: client, 1.55 Scope: scope, 1.56 }, 1.57 + Scope: scope, 1.58 } 1.59 1.60 err = fillTokens(&data, true, ctx)
2.1 --- a/client.go Sat Aug 16 02:57:49 2014 -0400 2.2 +++ b/client.go Sat Aug 16 05:33:23 2014 -0400 2.3 @@ -26,7 +26,7 @@ 2.4 if client == nil { 2.5 return NilClientError 2.6 } 2.7 - err = ctx.Clients.UpdateClient(client.ID, name, logo, redirectURI) 2.8 + err := ctx.Clients.UpdateClient(client.ID, name, logo, redirectURI) 2.9 if err != nil { 2.10 return err 2.11 }
3.1 --- a/context.go Sat Aug 16 02:57:49 2014 -0400 3.2 +++ b/context.go Sat Aug 16 05:33:23 2014 -0400 3.3 @@ -1,34 +1,98 @@ 3.4 package auth 3.5 3.6 import ( 3.7 + "encoding/json" 3.8 + "html/template" 3.9 "io" 3.10 "log" 3.11 ) 3.12 3.13 type Context struct { 3.14 - Config ServerConfig 3.15 - Clients ClientStore 3.16 - Tokens TokenStore 3.17 - Profiles ProfileStore 3.18 - Log *log.Logger 3.19 + Config ServerConfig 3.20 + Clients ClientStore 3.21 + Tokens TokenStore 3.22 + Profiles ProfileStore 3.23 + Log *log.Logger 3.24 + Templates Templates 3.25 +} 3.26 + 3.27 +type Templates struct { 3.28 + Error *template.Template 3.29 + Confirmation *template.Template 3.30 + Login *template.Template 3.31 +} 3.32 + 3.33 +type jsonError struct { 3.34 + Error string `json:"error,omitempty"` 3.35 + Description string `json:"error_description,omitempty"` 3.36 + URI string `json:"error_uri,omitempty"` 3.37 + State string `json:"state,omitempty"` 3.38 } 3.39 3.40 func (c Context) RenderError(w io.Writer, err error) { 3.41 - // TODO: write error to w in a template 3.42 + if c.Templates.Error == nil { 3.43 + log.Println("Error template is nil, can't render error.") 3.44 + return 3.45 + } 3.46 + renderErr := c.Templates.Error.Execute(w, map[string]interface{}{ 3.47 + "err": err, 3.48 + }) 3.49 + if renderErr != nil { 3.50 + log.Printf("Error executing error template (oh, the irony): %s\n", renderErr) 3.51 + return 3.52 + } 3.53 } 3.54 3.55 func (c Context) RenderJSONError(w io.Writer, code, description, baseURI string) { 3.56 - // TODO: write error to w in json formatting 3.57 + d, err := json.Marshal(jsonError{ 3.58 + Error: code, 3.59 + Description: description, 3.60 + URI: baseURI, 3.61 + }) 3.62 + if err != nil { 3.63 + log.Printf("Error marshalling json error (oh, the irony): %s\n", err) 3.64 + return 3.65 + } 3.66 + _, err = w.Write(d) 3.67 + if err != nil { 3.68 + log.Printf("Error writing json error: %s\n", err) 3.69 + return 3.70 + } 3.71 } 3.72 3.73 func (c Context) RenderConfirmation(w io.Writer) { 3.74 - // TODO: render HTML confirmation page 3.75 + if c.Templates.Confirmation == nil { 3.76 + log.Println("Confirmation template is nil, can't render confirmation.") 3.77 + return 3.78 + } 3.79 + err := c.Templates.Confirmation.Execute(w, nil) 3.80 + if err != nil { 3.81 + log.Printf("Error executing confirmation template: %s\n", err) 3.82 + return 3.83 + } 3.84 } 3.85 3.86 func (c Context) RenderLogin(w io.Writer) { 3.87 - // TODO: render HTML login page 3.88 + if c.Templates.Login == nil { 3.89 + log.Println("Login template is nil, can't render confirmation.") 3.90 + return 3.91 + } 3.92 + err := c.Templates.Login.Execute(w, nil) 3.93 + if err != nil { 3.94 + log.Printf("Error executing login template: %s\n", err) 3.95 + return 3.96 + } 3.97 } 3.98 3.99 func (c Context) RenderJSONToken(w io.Writer, data AccessData) { 3.100 - // TODO: render token to w in json formatting 3.101 + d, err := json.Marshal(data) 3.102 + if err != nil { 3.103 + log.Printf("Error marshalling json token: %s\n", err) 3.104 + return 3.105 + } 3.106 + _, err = w.Write(d) 3.107 + if err != nil { 3.108 + log.Printf("Error writing json token: %s\n", err) 3.109 + return 3.110 + } 3.111 }
4.1 --- a/storage.go Sat Aug 16 02:57:49 2014 -0400 4.2 +++ b/storage.go Sat Aug 16 05:33:23 2014 -0400 4.3 @@ -6,7 +6,7 @@ 4.4 GetClient(id uuid.ID) (Client, error) 4.5 CreateClient(name, logo, redirectURI string, owner uuid.ID) (Client, error) 4.6 UpdateClient(client uuid.ID, name, logo, redirectURI *string) error 4.7 - RemoveClient(id uuid.IDt) error 4.8 + RemoveClient(id uuid.ID) error 4.9 ListClients(id uuid.ID, page, num int) ([]Client, error) 4.10 } 4.11