auth
auth/grant.go
Added validation for clients, split endpoints out. Split endpoints out into their own type and added associated methods to the ClientStores, so now each client can have more than one redirect endpoint. Added unit testing for endpoint methods. Added validation code to validate client changes.
1 package auth
3 import (
4 "errors"
5 "time"
7 "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 }