auth
auth/grant.go
Create .hgignore. Don't store the cover.out files, but don't make me delete them every time I want to check my test coverage.
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 }