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.
8 "code.secondbit.org/uuid.hg"
12 if os.Getenv("PG_TEST_DB") != "" {
13 p, err := NewPostgres(os.Getenv("PG_TEST_DB"))
17 tokenStores = append(tokenStores, &p)
21 var tokenStores = []tokenStore{NewMemstore()}
23 func compareTokens(token1, token2 Token) (success bool, field string, val1, val2 interface{}) {
24 if token1.AccessToken != token2.AccessToken {
25 return false, "access token", token1.AccessToken, token2.AccessToken
27 if token1.RefreshToken != token2.RefreshToken {
28 return false, "refresh token", token1.RefreshToken, token2.RefreshToken
30 if !token1.Created.Equal(token2.Created) {
31 return false, "created", token1.Created, token2.Created
33 if token1.CreatedFrom != token2.CreatedFrom {
34 return false, "created from", token1.CreatedFrom, token2.CreatedFrom
36 if token1.ExpiresIn != token2.ExpiresIn {
37 return false, "expires in", token1.ExpiresIn, token2.ExpiresIn
39 if token1.TokenType != token2.TokenType {
40 return false, "token type", token1.TokenType, token2.TokenType
42 if len(token1.Scopes) != len(token2.Scopes) {
43 return false, "scopes", token1.Scopes, token2.Scopes
45 for pos, scope := range token1.Scopes {
46 if scope != token2.Scopes[pos] {
47 return false, "scopes", token1.Scopes, token2.Scopes
50 if !token1.ProfileID.Equal(token2.ProfileID) {
51 return false, "profile ID", token1.ProfileID, token2.ProfileID
53 if token1.Revoked != token2.Revoked {
54 return false, "revoked", token1.Revoked, token2.Revoked
56 return true, "", nil, nil
59 func TestTokenStoreSuccess(t *testing.T) {
62 AccessToken: "access",
63 RefreshToken: "refresh",
64 Created: time.Now().Round(time.Millisecond),
67 Scopes: stringsToScopes([]string{"scope"}),
68 ProfileID: uuid.NewID(),
70 for _, store := range tokenStores {
71 context := Context{tokens: store}
72 retrievedAccess, err := context.GetToken(token.AccessToken, false)
74 t.Errorf("Expected ErrTokenNotFound from %T, got %+v", store, retrievedAccess)
75 } else if err != ErrTokenNotFound {
76 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err)
78 retrievedRefresh, err := context.GetToken(token.RefreshToken, true)
80 t.Errorf("Expected ErrTokenNotFound from %T, got %+v", store, retrievedRefresh)
81 } else if err != ErrTokenNotFound {
82 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err)
84 err = context.RevokeToken(token.RefreshToken)
85 if err != ErrTokenNotFound {
86 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err)
88 err = context.SaveToken(token)
90 t.Errorf("Error saving token to %T: %s", store, err)
92 err = context.SaveToken(token)
93 if err != ErrTokenAlreadyExists {
94 t.Errorf("Expected ErrTokenAlreadyExists from %T, got %s", store, err)
96 retrievedAccess, err = context.GetToken(token.AccessToken, false)
98 t.Errorf("Error retrieving token from %T: %s", store, err)
100 success, field, expectation, result := compareTokens(token, retrievedAccess)
102 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
104 retrievedRefresh, err = context.GetToken(token.RefreshToken, true)
106 t.Errorf("Error retrieving refresh token from %T: %s", store, err)
108 success, field, expectation, result = compareTokens(token, retrievedRefresh)
110 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
112 retrievedProfile, err := context.GetTokensByProfileID(token.ProfileID, 25, 0)
114 t.Errorf("Error retrieving token by profile from %T: %s", store, err)
116 if len(retrievedProfile) != 1 {
117 t.Errorf("Expected 1 token retrieved by profile ID from %T, got %+v", store, retrievedProfile)
119 success, field, expectation, result = compareTokens(token, retrievedProfile[0])
121 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
123 err = context.RevokeToken(token.RefreshToken)
125 t.Errorf("Error revoking token in %T: %s", store, err)
127 retrievedRevoked, err := context.GetToken(token.AccessToken, false)
129 t.Errorf("Error retrieving token from %T: %s", store, err)
132 success, field, expectation, result = compareTokens(token, retrievedRevoked)
134 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
139 // BUG(paddy): We need to test the refreshTokenValidate function.
140 // BUG(paddy): We need to test the refreshTokenInvalidate function.