Implement postgres clientStore.
Stop requiring the client ID be passed to clientStore.addEndpoints and
context.AddEndpoints. The Endpoints themselves contain the client ID.
When using the authd server, set the log flags to include the file path and line
number.
Add an ErrEndpointAlreadyExists error, to return when creating an endpoint and
its ID already exists in the database.
Add a Deleted property to Clients and remove the clientStore.deleteClient and
context.DeleteClient methods. We're not going to actually remove that data, and
we want to be able to restore it, so include it in the ClientChange type and
call it using UpdateClient.
Create a ClientChange.Empty helper method that will return whether the
ClientChange has any changes to perform.
Return ErrClientNotFound from clientStore.getClient if the Client's Deleted
property is set to true. This also requires us to ignore ErrClientNotFound
errors when calling memstore.listClientsByOwner, as they should just be skipped
instead of returning an error.
Add the postgres type methods needed to implement clientStore.
Include postgres as a clientStore if the testing.Short() flag is not set.
Generate a new ID for the Client on every run in the tests, now that we can't
actually remove it from the database/memstore in code. We really just need a
*Store.Reset() function that erases all the data and starts over again, to give
the tests a clean execution environment (and so they can clean up after
themselves).
Add the CREATE TABLE statements for the Clients table and the Endpoints table to
sql/postgres_init.sql.
7 "code.secondbit.org/uuid.hg"
10 var tokenStores = []tokenStore{NewMemstore()}
12 func compareTokens(token1, token2 Token) (success bool, field string, val1, val2 interface{}) {
13 if token1.AccessToken != token2.AccessToken {
14 return false, "access token", token1.AccessToken, token2.AccessToken
16 if token1.RefreshToken != token2.RefreshToken {
17 return false, "refresh token", token1.RefreshToken, token2.RefreshToken
19 if !token1.Created.Equal(token2.Created) {
20 return false, "created", token1.Created, token2.Created
22 if token1.CreatedFrom != token2.CreatedFrom {
23 return false, "created from", token1.CreatedFrom, token2.CreatedFrom
25 if token1.ExpiresIn != token2.ExpiresIn {
26 return false, "expires in", token1.ExpiresIn, token2.ExpiresIn
28 if token1.TokenType != token2.TokenType {
29 return false, "token type", token1.TokenType, token2.TokenType
31 if len(token1.Scopes) != len(token2.Scopes) {
32 return false, "scopes", token1.Scopes, token2.Scopes
34 for pos, scope := range token1.Scopes {
35 if scope != token2.Scopes[pos] {
36 return false, "scopes", token1.Scopes, token2.Scopes
39 if !token1.ProfileID.Equal(token2.ProfileID) {
40 return false, "profile ID", token1.ProfileID, token2.ProfileID
42 if token1.Revoked != token2.Revoked {
43 return false, "revoked", token1.Revoked, token2.Revoked
45 return true, "", nil, nil
48 func TestTokenStoreSuccess(t *testing.T) {
51 AccessToken: "access",
52 RefreshToken: "refresh",
53 Created: time.Now().Round(time.Millisecond),
56 Scopes: []string{"scope"},
57 ProfileID: uuid.NewID(),
59 for _, store := range tokenStores {
60 context := Context{tokens: store}
61 retrievedAccess, err := context.GetToken(token.AccessToken, false)
63 t.Errorf("Expected ErrTokenNotFound from %T, got %+v", store, retrievedAccess)
64 } else if err != ErrTokenNotFound {
65 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err)
67 retrievedRefresh, err := context.GetToken(token.RefreshToken, true)
69 t.Errorf("Expected ErrTokenNotFound from %T, got %+v", store, retrievedRefresh)
70 } else if err != ErrTokenNotFound {
71 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err)
73 err = context.RevokeToken(token.AccessToken, false)
74 if err != ErrTokenNotFound {
75 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err)
77 err = context.RevokeToken(token.RefreshToken, true)
78 if err != ErrTokenNotFound {
79 t.Errorf("Expected ErrTokenNotFound from %T, got %s", store, err)
81 err = context.SaveToken(token)
83 t.Errorf("Error saving token to %T: %s", store, err)
85 err = context.SaveToken(token)
86 if err != ErrTokenAlreadyExists {
87 t.Errorf("Expected ErrTokenAlreadyExists from %T, got %s", store, err)
89 retrievedAccess, err = context.GetToken(token.AccessToken, false)
91 t.Errorf("Error retrieving token from %T: %s", store, err)
93 success, field, expectation, result := compareTokens(token, retrievedAccess)
95 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
97 retrievedRefresh, err = context.GetToken(token.RefreshToken, true)
99 t.Errorf("Error retrieving refresh token from %T: %s", store, err)
101 success, field, expectation, result = compareTokens(token, retrievedRefresh)
103 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
105 retrievedProfile, err := context.GetTokensByProfileID(token.ProfileID, 25, 0)
107 t.Errorf("Error retrieving token by profile from %T: %s", store, err)
109 if len(retrievedProfile) != 1 {
110 t.Errorf("Expected 1 token retrieved by profile ID from %T, got %+v", store, retrievedProfile)
112 success, field, expectation, result = compareTokens(token, retrievedProfile[0])
114 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
116 err = context.RevokeToken(token.AccessToken, false)
118 t.Errorf("Error revoking token in %T: %s", store, err)
120 retrievedRevoked, err := context.GetToken(token.AccessToken, false)
122 t.Errorf("Error retrieving token from %T: %s", store, err)
125 success, field, expectation, result = compareTokens(token, retrievedRevoked)
127 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
129 err = context.RevokeToken(token.RefreshToken, true)
131 t.Errorf("Error revoking token in %T: %s", store, err)
133 retrievedRevoked, err = context.GetToken(token.RefreshToken, true)
135 t.Errorf("Error retrieving token from %T: %s", store, err)
137 token.RefreshRevoked = true
138 success, field, expectation, result = compareTokens(token, retrievedRevoked)
140 t.Errorf("Expected field %s to be %v, but got %v from %T", field, expectation, result, store)
145 // BUG(paddy): We need to test the refreshTokenValidate function.
146 // BUG(paddy): We need to test the refreshTokenInvalidate function.