The great documentation and exported interface cleanup.
Modify all our *Store interfaces to be unexported, as there's no real good
reason they need to be exported, especially as they can be implemented without
being exported. The interfaces shouldn't matter to 99% of users of the package,
so let's not pollute our package API.
Further, all methods of the interfaces are now unexported, for pretty much the
same reasoning.
Add a doc.go file with documentation explaining the choices the package is
making and what it provides.
Implement documentation on all our exported types and methods and functions,
which makes golint happy. The only remaining golint warning is about
NewMemstore, which will stay the way it is. The memstore type is useful outside
tests for things like standing up a server quickly when we don't care about the
storage, and because the type is unexported, we _need_ a New function to create
an instance that can be passed to the Context.
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)