auth

Paddy 2014-09-01 Child:5bf0a5fd1d01

28:75cf37088852 Go to Latest

auth/memstore.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/memstore.go	Mon Sep 01 09:21:31 2014 -0400
     1.3 @@ -0,0 +1,34 @@
     1.4 +package auth
     1.5 +
     1.6 +import "sync"
     1.7 +
     1.8 +type Memstore struct {
     1.9 +	tokens             map[string]Token
    1.10 +	refreshTokenLookup map[string]string
    1.11 +	profileTokenLookup map[string][]string
    1.12 +	tokenLock          sync.RWMutex
    1.13 +}
    1.14 +
    1.15 +func NewMemstore() *Memstore {
    1.16 +	return &Memstore{
    1.17 +		tokens:             map[string]Token{},
    1.18 +		refreshTokenLookup: map[string]string{},
    1.19 +		profileTokenLookup: map[string][]string{},
    1.20 +	}
    1.21 +}
    1.22 +
    1.23 +func (m *Memstore) lookupTokenByRefresh(token string) (string, error) {
    1.24 +	m.tokenLock.RLock()
    1.25 +	defer m.tokenLock.RUnlock()
    1.26 +	t, ok := m.refreshTokenLookup[token]
    1.27 +	if !ok {
    1.28 +		return "", ErrTokenNotFound
    1.29 +	}
    1.30 +	return t, nil
    1.31 +}
    1.32 +
    1.33 +func (m *Memstore) lookupTokensByProfileID(id string) ([]string, error) {
    1.34 +	m.tokenLock.RLock()
    1.35 +	defer m.tokenLock.RUnlock()
    1.36 +	return m.profileTokenLookup[id], nil
    1.37 +}