auth

Paddy 2014-09-27 Parent:3a6a65ed380c Child:73a9f7a6af54

47:85f1baf7a9ac Go to Latest

auth/grant.go

Add basic test for retrieving profiles by login. Add a basic test for retrieving profiles from ProfileStores by a passed login. The test is very basic, in that it doesn't test for deleted logins, deleted profiles, etc. But it at least tests that in ordinary circumstances, things work as they're supposed to.

History
1 package auth
3 import (
4 "errors"
5 "time"
7 "code.secondbit.org/uuid"
8 )
10 var (
11 ErrGrantNotFound = errors.New("Grant not found in GrantStore.")
12 ErrGrantAlreadyExists = errors.New("Grant already exists in GrantStore.")
13 )
15 type Grant struct {
16 Code string
17 Created time.Time
18 ExpiresIn int32
19 ClientID uuid.ID
20 Scope string
21 RedirectURI string
22 State string
23 }
25 type GrantStore interface {
26 GetGrant(code string) (Grant, error)
27 SaveGrant(grant Grant) error
28 DeleteGrant(code string) error
29 }
31 func (m *Memstore) GetGrant(code string) (Grant, error) {
32 m.grantLock.RLock()
33 defer m.grantLock.RUnlock()
34 grant, ok := m.grants[code]
35 if !ok {
36 return Grant{}, ErrGrantNotFound
37 }
38 return grant, nil
39 }
41 func (m *Memstore) SaveGrant(grant Grant) error {
42 m.grantLock.Lock()
43 defer m.grantLock.Unlock()
44 _, ok := m.grants[grant.Code]
45 if ok {
46 return ErrGrantAlreadyExists
47 }
48 m.grants[grant.Code] = grant
49 return nil
50 }
52 func (m *Memstore) DeleteGrant(code string) error {
53 m.grantLock.Lock()
54 defer m.grantLock.Unlock()
55 _, ok := m.grants[code]
56 if !ok {
57 return ErrGrantNotFound
58 }
59 delete(m.grants, code)
60 return nil
61 }