Start to support deleting profiles through the API.
Create a removeLoginsByProfile method on the profileStore, to allow an easy way
to bulk-delete logins associated with a Profile after the Profile has been
deleted.
Create postgres and memstore implementations of the removeLoginsByProfile
method.
Create a cleanUpAfterProfileDeletion helper method that will clean up the child
objects of a Profile (its Sessions, Tokens, Clients, etc.). The intended usage
is to call this in a goroutine after a Profile has been deleted, to try and get
things back in order.
Detect when the UpdateProfileHandler API is used to set the Deleted flag of a
Profile to true, and clean up after the Profile when that's the case.
Add a DeleteProfileHandler API endpoint that is a shortcut to setting the
Deleted flag of a Profile to true and cleaning up after the Profile.
The problem with our approach thus far is that some of it is reversible and some
is not. If a Profile is maliciously/accidentally deleted, it's simple enough to
use the API as a superuser to restore the Profile. But doing that will not (and
cannot) restore the Logins associated with that Profile, for example. While it
would be nice to add a Deleted flag to our Logins that we could simply toggle,
that would wreak havoc with our database constraints and ensuring uniqueness of
Login values. I still don't have a solution for this, outside the superuser
manually restoring a Login for the Profile, after which the user can
authenticate themselves and add more Logins as desired. But there has to be a
better way.
I suppose since the passphrase is being stored with the Profile and not the
Login, we could offer an endpoint that would automate this, but... well, that
would be tricky. It would require the user remembering their Profile ID, and
let's be honest, nobody's going to remember a UUID.
Maybe such an endpoint would help from a customer service standpoint: we
identify their Profile manually, then send them to /profiles/ID/restorelogin or
something, and that lets them add a Login back to the Profile.
I'll figure it out later. For now, we know we at least have enough information
to identify a user is who they say they are and resolve the situation manually.
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: []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.AccessToken, false)
85 if err != ErrTokenNotFound {
86 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err)
88 err = context.RevokeToken(token.RefreshToken, true)
89 if err != ErrTokenNotFound {
90 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err)
92 err = context.SaveToken(token)
94 t.Errorf("Error saving token to %T: %s", store, err)
96 err = context.SaveToken(token)
97 if err != ErrTokenAlreadyExists {
98 t.Errorf("Expected ErrTokenAlreadyExists from %T, got %s", store, err)
100 retrievedAccess, err = context.GetToken(token.AccessToken, false)
102 t.Errorf("Error retrieving token from %T: %s", store, err)
104 success, field, expectation, result := compareTokens(token, retrievedAccess)
106 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
108 retrievedRefresh, err = context.GetToken(token.RefreshToken, true)
110 t.Errorf("Error retrieving refresh token from %T: %s", store, err)
112 success, field, expectation, result = compareTokens(token, retrievedRefresh)
114 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
116 retrievedProfile, err := context.GetTokensByProfileID(token.ProfileID, 25, 0)
118 t.Errorf("Error retrieving token by profile from %T: %s", store, err)
120 if len(retrievedProfile) != 1 {
121 t.Errorf("Expected 1 token retrieved by profile ID from %T, got %+v", store, retrievedProfile)
123 success, field, expectation, result = compareTokens(token, retrievedProfile[0])
125 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
127 err = context.RevokeToken(token.AccessToken, false)
129 t.Errorf("Error revoking token in %T: %s", store, err)
131 retrievedRevoked, err := context.GetToken(token.AccessToken, false)
133 t.Errorf("Error retrieving token from %T: %s", store, err)
136 success, field, expectation, result = compareTokens(token, retrievedRevoked)
138 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
140 err = context.RevokeToken(token.RefreshToken, true)
142 t.Errorf("Error revoking token in %T: %s", store, err)
144 retrievedRevoked, err = context.GetToken(token.RefreshToken, true)
146 t.Errorf("Error retrieving token from %T: %s", store, err)
148 token.RefreshRevoked = true
149 success, field, expectation, result = compareTokens(token, retrievedRevoked)
151 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
156 // BUG(paddy): We need to test the refreshTokenValidate function.
157 // BUG(paddy): We need to test the refreshTokenInvalidate function.