auth
auth/config.go
Support email verification. The bulk of this commit is auto-modifying files to export variables (mostly our request error types and our response type) so that they can be reused in a Go client for that API. We also implement the beginnings of a Go client for that API, implementing the bare minimum we need for our immediate purposes: the ability to retrieve information about a Login. This, of course, means we need an API endpoint that will return information about a Login, which in turn required us to implement a GetLogin method in our profileStore. Which got in-memory and postgres implementations. That done, we could add the Verification field and Verified field to the Login type, to keep track of whether we've verified the user's ownership of those communication methods (if the Login is, in fact, a communication method). This required us to update sql/postgres_init.sql to account for the new fields we're tracking. It also means that when creating a Login, we had to generate a UUID to use as the Verification field. To make things complete, we needed a verifyLogin method on the profileStore to mark a Login as verified. That, in turn, required an endpoint to control this through the API. While doing so, I lumped things together in an UpdateLogin handler just so we could reuse the endpoint and logic when resending a verification email that may have never reached the user, for whatever reason (the quintessential "send again" button). Finally, we implemented an email_verification listener that will pull email_verification events off NSQ, check for the requisite data integrity, and use mailgun to email out a verification/welcome email.
| 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@171 | 14 |
| paddy@171 | 15 // Version is used to keep track of what version of the build this is |
| paddy@171 | 16 Version string |
| paddy@96 | 17 ) |
| paddy@96 | 18 |
| paddy@96 | 19 // Config holds the configuration values necessary to run a server. A Config |
| paddy@96 | 20 // instance is the only way to instantiate a Context variable. |
| paddy@96 | 21 type Config struct { |
| paddy@171 | 22 ClientStore clientStore |
| paddy@171 | 23 AuthCodeStore authorizationCodeStore |
| paddy@171 | 24 ProfileStore profileStore |
| paddy@171 | 25 TokenStore tokenStore |
| paddy@171 | 26 SessionStore sessionStore |
| paddy@171 | 27 ScopeStore scopeStore |
| paddy@171 | 28 LoginVerificationNotifier loginVerificationNotifier |
| paddy@171 | 29 Template *template.Template |
| paddy@171 | 30 LoginURI string |
| paddy@171 | 31 JWTPrivateKey []byte |
| paddy@171 | 32 iterations int |
| paddy@171 | 33 secureCookie bool |
| paddy@96 | 34 } |
| paddy@101 | 35 |
| paddy@102 | 36 // Init is a function that preps the Config object to be used for Context creation, setting variables |
| paddy@102 | 37 // that are determined at the beginning of program execution. |
| paddy@101 | 38 func (c *Config) Init() error { |
| paddy@101 | 39 scheme, ok := passphraseSchemes[CurPassphraseScheme] |
| paddy@101 | 40 if !ok { |
| paddy@101 | 41 return ErrInvalidPassphraseScheme |
| paddy@101 | 42 } |
| paddy@101 | 43 var err error |
| paddy@101 | 44 c.iterations, err = scheme.calculateIterations() |
| paddy@101 | 45 if err != nil { |
| paddy@101 | 46 return err |
| paddy@101 | 47 } |
| paddy@101 | 48 log.Printf("Generating passphrases with %d iterations...\n", c.iterations) |
| paddy@101 | 49 return nil |
| paddy@101 | 50 } |