Bugfixes and tests for getting grants.
Add tests for the grant confirmation part of the request to get a grant code.
When the request is denied, the redirect should have an access_denied error.
When the request is approved, the redirect should contain a code.
Fix numerous bugs in which the redirect URL didn't contain the parameters we
thought it would. Basically, anything that still used req.URL.Query().Set()
instead of copying the query, modifying it, and setting req.URL.RawQuery.
Fix a bug in which the redirectURL wasn't being properly set. Basically, when we
moved the redirectURI processing to the top of the file (c29c7df35905 for those
who forgot), we didn't update the reference to it lower in the file, where
redirectURI was being updated and we expected that to be reflected in the
processing.
The HTTP handler for getting grant codes is now completely tested except for
returning internal errors, which requires a new test harness be built to provoke
internal errors on demand. At this point, however, I'd like to continue on
implementing endpoints.
7 "code.secondbit.org/uuid"
11 // ErrNoTokenStore is returned when a Context tries to act on a tokenStore without setting one first.
12 ErrNoTokenStore = errors.New("no tokenStore was specified for the Context")
13 // ErrTokenNotFound is returned when a Token is requested but not found in a tokenStore.
14 ErrTokenNotFound = errors.New("token not found in tokenStore")
15 // ErrTokenAlreadyExists is returned when a Token is added to a tokenStore, but another Token with
16 // the same AccessToken property already exists in the tokenStore.
17 ErrTokenAlreadyExists = errors.New("token already exists in tokenStore")
20 // Token represents an access and/or refresh token that the Client can use to access user data
21 // or obtain a new access token.
32 type tokenStore interface {
33 getToken(token string, refresh bool) (Token, error)
34 saveToken(token Token) error
35 removeToken(token string) error
36 getTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error)
39 func (m *memstore) getToken(token string, refresh bool) (Token, error) {
41 t, err := m.lookupTokenByRefresh(token)
48 defer m.tokenLock.RUnlock()
49 result, ok := m.tokens[token]
51 return Token{}, ErrTokenNotFound
56 func (m *memstore) saveToken(token Token) error {
58 defer m.tokenLock.Unlock()
59 _, ok := m.tokens[token.AccessToken]
61 return ErrTokenAlreadyExists
63 m.tokens[token.AccessToken] = token
64 if token.RefreshToken != "" {
65 m.refreshTokenLookup[token.RefreshToken] = token.AccessToken
67 if _, ok = m.profileTokenLookup[token.ProfileID.String()]; ok {
68 m.profileTokenLookup[token.ProfileID.String()] = append(m.profileTokenLookup[token.ProfileID.String()], token.AccessToken)
70 m.profileTokenLookup[token.ProfileID.String()] = []string{token.AccessToken}
75 func (m *memstore) removeToken(token string) error {
77 defer m.tokenLock.Unlock()
78 t, ok := m.tokens[token]
80 return ErrTokenNotFound
82 delete(m.tokens, token)
83 if t.RefreshToken != "" {
84 delete(m.refreshTokenLookup, t.RefreshToken)
87 for p, item := range m.profileTokenLookup[t.ProfileID.String()] {
94 m.profileTokenLookup[t.ProfileID.String()] = append(m.profileTokenLookup[t.ProfileID.String()][:pos], m.profileTokenLookup[t.ProfileID.String()][pos+1:]...)
99 func (m *memstore) getTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) {
100 ids, err := m.lookupTokensByProfileID(profileID.String())
102 return []Token{}, err
104 if len(ids) > num+offset {
105 ids = ids[offset : num+offset]
106 } else if len(ids) > offset {
109 return []Token{}, nil
112 for _, id := range ids {
113 token, err := m.getToken(id, false)
115 return []Token{}, err
117 tokens = append(tokens, token)