auth

Paddy 2014-09-01 Child:3a6a65ed380c

28:75cf37088852 Go to Latest

auth/token.go

Rough out tokens and begin the memstore. Rough out the Token type for working with OAuth2 access and refresh tokens. Rough out the TokenStore interface that dictates how Tokens will be stored and retrieved. Write tests for the successful (in the working-as-intended sense) calls to TokenStore. Begin a Memstore type that stores data in memory. Implement the TokenStore interface for Memstore.

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/token.go	Mon Sep 01 09:21:31 2014 -0400
     1.3 @@ -0,0 +1,113 @@
     1.4 +package auth
     1.5 +
     1.6 +import (
     1.7 +	"errors"
     1.8 +	"time"
     1.9 +
    1.10 +	"secondbit.org/uuid"
    1.11 +)
    1.12 +
    1.13 +var (
    1.14 +	ErrTokenNotFound      = errors.New("Token not found in TokenStore.")
    1.15 +	ErrTokenAlreadyExists = errors.New("Token already exists in TokenStore.")
    1.16 +)
    1.17 +
    1.18 +type Token struct {
    1.19 +	AccessToken  string
    1.20 +	RefreshToken string
    1.21 +	Created      time.Time
    1.22 +	ExpiresIn    int32
    1.23 +	TokenType    string
    1.24 +	Scope        string
    1.25 +	ProfileID    uuid.ID
    1.26 +}
    1.27 +
    1.28 +type TokenStore interface {
    1.29 +	GetToken(token string, refresh bool) (Token, error)
    1.30 +	SaveToken(token Token) error
    1.31 +	RemoveToken(token string) error
    1.32 +	GetTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error)
    1.33 +}
    1.34 +
    1.35 +func (m *Memstore) GetToken(token string, refresh bool) (Token, error) {
    1.36 +	if refresh {
    1.37 +		t, err := m.lookupTokenByRefresh(token)
    1.38 +		if err != nil {
    1.39 +			return Token{}, err
    1.40 +		}
    1.41 +		token = t
    1.42 +	}
    1.43 +	m.tokenLock.RLock()
    1.44 +	defer m.tokenLock.RUnlock()
    1.45 +	result, ok := m.tokens[token]
    1.46 +	if !ok {
    1.47 +		return Token{}, ErrTokenNotFound
    1.48 +	}
    1.49 +	return result, nil
    1.50 +}
    1.51 +
    1.52 +func (m *Memstore) SaveToken(token Token) error {
    1.53 +	m.tokenLock.Lock()
    1.54 +	defer m.tokenLock.Unlock()
    1.55 +	_, ok := m.tokens[token.AccessToken]
    1.56 +	if ok {
    1.57 +		return ErrTokenAlreadyExists
    1.58 +	}
    1.59 +	m.tokens[token.AccessToken] = token
    1.60 +	if token.RefreshToken != "" {
    1.61 +		m.refreshTokenLookup[token.RefreshToken] = token.AccessToken
    1.62 +	}
    1.63 +	if _, ok = m.profileTokenLookup[token.ProfileID.String()]; ok {
    1.64 +		m.profileTokenLookup[token.ProfileID.String()] = append(m.profileTokenLookup[token.ProfileID.String()], token.AccessToken)
    1.65 +	} else {
    1.66 +		m.profileTokenLookup[token.ProfileID.String()] = []string{token.AccessToken}
    1.67 +	}
    1.68 +	return nil
    1.69 +}
    1.70 +
    1.71 +func (m *Memstore) RemoveToken(token string) error {
    1.72 +	m.tokenLock.Lock()
    1.73 +	defer m.tokenLock.Unlock()
    1.74 +	t, ok := m.tokens[token]
    1.75 +	if !ok {
    1.76 +		return ErrTokenNotFound
    1.77 +	}
    1.78 +	delete(m.tokens, token)
    1.79 +	if t.RefreshToken != "" {
    1.80 +		delete(m.refreshTokenLookup, t.RefreshToken)
    1.81 +	}
    1.82 +	pos := -1
    1.83 +	for p, item := range m.profileTokenLookup[t.ProfileID.String()] {
    1.84 +		if item == token {
    1.85 +			pos = p
    1.86 +			break
    1.87 +		}
    1.88 +	}
    1.89 +	if pos >= 0 {
    1.90 +		m.profileTokenLookup[t.ProfileID.String()] = append(m.profileTokenLookup[t.ProfileID.String()][:pos], m.profileTokenLookup[t.ProfileID.String()][pos+1:]...)
    1.91 +	}
    1.92 +	return nil
    1.93 +}
    1.94 +
    1.95 +func (m *Memstore) GetTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) {
    1.96 +	ids, err := m.lookupTokensByProfileID(profileID.String())
    1.97 +	if err != nil {
    1.98 +		return []Token{}, err
    1.99 +	}
   1.100 +	if len(ids) > num+offset {
   1.101 +		ids = ids[offset : num+offset]
   1.102 +	} else if len(ids) > offset {
   1.103 +		ids = ids[offset:]
   1.104 +	} else {
   1.105 +		return []Token{}, nil
   1.106 +	}
   1.107 +	tokens := []Token{}
   1.108 +	for _, id := range ids {
   1.109 +		token, err := m.GetToken(id, false)
   1.110 +		if err != nil {
   1.111 +			return []Token{}, err
   1.112 +		}
   1.113 +		tokens = append(tokens, token)
   1.114 +	}
   1.115 +	return tokens, nil
   1.116 +}