auth
auth/config.go
Switch to a JWT approach. We're going to use a JWT as our access tokens (as discussed in &yet's excellent post https://blog.andyet.com/2015/05/12/micro-services-user-info-and-auth and my ensuing conversation with Fritzy). The benefit of this approach is that we can do authentication and even some authorization without touching the database at all. The drawback is that we can no longer revoke access tokens, only the refresh tokens that grant the access tokens. We need a new config variable to set our private key, used to sign the JWT. We get to remove our token handlers, as we no longer can revoke tokens, so there's no purpose in getting information about it or listing them. Our tokenStore revokeToken gets to be simplified, as it will only ever be used for refresh tokens now. We also updated our postgres and memstore implementations. We added a helper method for generating the signed "access token" (our JWT) and started using it in the places where we're creating a Token. We get to remove the `revoked` SQL column for the tokens table, and rename the `refresh_revoked` column to just be `revoked`. We shortened our access token expiration to 15 minutes instead of an hour, to deal with the token not being revokable.
| paddy@96 | 1 package auth |
| paddy@96 | 2 |
| paddy@96 | 3 import ( |
| paddy@96 | 4 "errors" |
| paddy@96 | 5 "html/template" |
| paddy@101 | 6 "log" |
| paddy@96 | 7 ) |
| paddy@96 | 8 |
| paddy@96 | 9 var ( |
| paddy@96 | 10 // ErrInvalidLoginURI is returned when a Context is instantiated with a Config object that specifies a LoginURI that can't be parsed as a URL. |
| paddy@96 | 11 ErrInvalidLoginURI = errors.New("invalid login URI") |
| paddy@102 | 12 // ErrConfigNotInitialized is returned when a Context is instantiated with a Config object that hasn't had its Init function called. |
| paddy@102 | 13 ErrConfigNotInitialized = errors.New("config not initialized") |
| paddy@96 | 14 ) |
| paddy@96 | 15 |
| paddy@96 | 16 // Config holds the configuration values necessary to run a server. A Config |
| paddy@96 | 17 // instance is the only way to instantiate a Context variable. |
| paddy@96 | 18 type Config struct { |
| paddy@96 | 19 ClientStore clientStore |
| paddy@96 | 20 AuthCodeStore authorizationCodeStore |
| paddy@96 | 21 ProfileStore profileStore |
| paddy@96 | 22 TokenStore tokenStore |
| paddy@96 | 23 SessionStore sessionStore |
| paddy@134 | 24 ScopeStore scopeStore |
| paddy@96 | 25 Template *template.Template |
| paddy@96 | 26 LoginURI string |
| paddy@168 | 27 JWTPrivateKey []byte |
| paddy@96 | 28 iterations int |
| paddy@132 | 29 secureCookie bool |
| paddy@96 | 30 } |
| paddy@101 | 31 |
| paddy@102 | 32 // Init is a function that preps the Config object to be used for Context creation, setting variables |
| paddy@102 | 33 // that are determined at the beginning of program execution. |
| paddy@101 | 34 func (c *Config) Init() error { |
| paddy@101 | 35 scheme, ok := passphraseSchemes[CurPassphraseScheme] |
| paddy@101 | 36 if !ok { |
| paddy@101 | 37 return ErrInvalidPassphraseScheme |
| paddy@101 | 38 } |
| paddy@101 | 39 var err error |
| paddy@101 | 40 c.iterations, err = scheme.calculateIterations() |
| paddy@101 | 41 if err != nil { |
| paddy@101 | 42 return err |
| paddy@101 | 43 } |
| paddy@101 | 44 log.Printf("Generating passphrases with %d iterations...\n", c.iterations) |
| paddy@101 | 45 return nil |
| paddy@101 | 46 } |