auth

Paddy 2015-05-17 Parent:581c60f8dd23 Child:0a2c3d677161

172:8ecb60d29b0d Go to Latest

auth/context.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.

History
     1.1 --- a/context.go	Sun May 17 02:18:07 2015 -0400
     1.2 +++ b/context.go	Sun May 17 02:27:36 2015 -0400
     1.3 @@ -14,15 +14,16 @@
     1.4  // be used as the main point of interaction for the data storage
     1.5  // layer.
     1.6  type Context struct {
     1.7 -	template  *template.Template
     1.8 -	loginURI  *url.URL
     1.9 -	clients   clientStore
    1.10 -	authCodes authorizationCodeStore
    1.11 -	profiles  profileStore
    1.12 -	tokens    tokenStore
    1.13 -	sessions  sessionStore
    1.14 -	scopes    scopeStore
    1.15 -	config    Config
    1.16 +	template                  *template.Template
    1.17 +	loginURI                  *url.URL
    1.18 +	clients                   clientStore
    1.19 +	authCodes                 authorizationCodeStore
    1.20 +	profiles                  profileStore
    1.21 +	tokens                    tokenStore
    1.22 +	sessions                  sessionStore
    1.23 +	scopes                    scopeStore
    1.24 +	loginVerificationNotifier loginVerificationNotifier
    1.25 +	config                    Config
    1.26  }
    1.27  
    1.28  // NewContext takes a Config instance and uses it to bootstrap a Context
    1.29 @@ -38,8 +39,9 @@
    1.30  		tokens:    config.TokenStore,
    1.31  		sessions:  config.SessionStore,
    1.32  		scopes:    config.ScopeStore,
    1.33 -		template:  config.Template,
    1.34 -		config:    config,
    1.35 +		loginVerificationNotifier: config.LoginVerificationNotifier,
    1.36 +		template:                  config.Template,
    1.37 +		config:                    config,
    1.38  	}
    1.39  	var err error
    1.40  	context.loginURI, err = url.Parse(config.LoginURI)
    1.41 @@ -293,6 +295,14 @@
    1.42  	return c.profiles.addLogin(login)
    1.43  }
    1.44  
    1.45 +// GetLogin returns the Login specified from the profileStore associated with the Context.
    1.46 +func (c Context) GetLogin(value string) (Login, error) {
    1.47 +	if c.profiles == nil {
    1.48 +		return Login{}, ErrNoProfileStore
    1.49 +	}
    1.50 +	return c.profiles.getLogin(value)
    1.51 +}
    1.52 +
    1.53  // RemoveLogin removes the specified Login from the profileStore associated with the Context, provided
    1.54  // the Login has a ProfileID property that matches the profile ID passed in. It also disassociates the
    1.55  // deleted Login from the Profile in login.ProfileID.
    1.56 @@ -321,6 +331,16 @@
    1.57  	return c.profiles.recordLoginUse(value, when)
    1.58  }
    1.59  
    1.60 +// VerifyLogin sets the Verified property of the Login specied to true in the profileStore associated with
    1.61 +// the Context, assuming the Verification property of the Login in the profileStore matches the verification
    1.62 +// passed. If the verifications do not match, an ErrLoginVerificationInvalid error is returned.
    1.63 +func (c Context) VerifyLogin(value, verification string) error {
    1.64 +	if c.profiles == nil {
    1.65 +		return ErrNoProfileStore
    1.66 +	}
    1.67 +	return c.profiles.verifyLogin(value, verification)
    1.68 +}
    1.69 +
    1.70  // ListLogins returns a slice of up to num Logins associated with the specified Profile from the profileStore
    1.71  // associated with the Context, skipping offset Profiles.
    1.72  func (c Context) ListLogins(profile uuid.ID, num, offset int) ([]Login, error) {
    1.73 @@ -470,3 +490,10 @@
    1.74  	}
    1.75  	return c.scopes.listScopes()
    1.76  }
    1.77 +
    1.78 +func (c Context) SendLoginVerification(login Login) {
    1.79 +	if c.loginVerificationNotifier == nil {
    1.80 +		log.Println("Login verification notifier not set!")
    1.81 +	}
    1.82 +	c.loginVerificationNotifier.SendLoginVerification(login)
    1.83 +}