auth

Paddy 2014-11-03 Parent:e45bfa2abc00 Child:42bc3e44f4fe

66:55d5107e8805 Go to Latest

auth/grant.go

Bugfixes and tests for getting grants. Add tests for the grant confirmation part of the request to get a grant code. When the request is denied, the redirect should have an access_denied error. When the request is approved, the redirect should contain a code. Fix numerous bugs in which the redirect URL didn't contain the parameters we thought it would. Basically, anything that still used req.URL.Query().Set() instead of copying the query, modifying it, and setting req.URL.RawQuery. Fix a bug in which the redirectURL wasn't being properly set. Basically, when we moved the redirectURI processing to the top of the file (c29c7df35905 for those who forgot), we didn't update the reference to it lower in the file, where redirectURI was being updated and we expected that to be reflected in the processing. The HTTP handler for getting grant codes is now completely tested except for returning internal errors, which requires a new test harness be built to provoke internal errors on demand. At this point, however, I'd like to continue on implementing endpoints.

History
1 package auth
3 import (
4 "errors"
5 "time"
7 "code.secondbit.org/uuid"
8 )
10 var (
11 // ErrNoGrantStore is returned when a Context tries to act on a grantStore without setting one first.
12 ErrNoGrantStore = errors.New("no grantStore was specified for the Context")
13 // ErrGrantNotFound is returned when a Grant is requested but not found in the grantStore.
14 ErrGrantNotFound = errors.New("grant not found in grantStore")
15 // ErrGrantAlreadyExists is returned when a Grant is added to a grantStore, but another Grant with the
16 // same Code already exists in the grantStore.
17 ErrGrantAlreadyExists = errors.New("grant already exists in grantStore")
18 )
20 // Grant represents an authorization grant made by a user to a Client, to
21 // access user data within a defined Scope for a limited amount of time.
22 type Grant struct {
23 Code string
24 Created time.Time
25 ExpiresIn int32
26 ClientID uuid.ID
27 Scope string
28 RedirectURI string
29 State string
30 }
32 type grantStore interface {
33 getGrant(code string) (Grant, error)
34 saveGrant(grant Grant) error
35 deleteGrant(code string) error
36 }
38 func (m *memstore) getGrant(code string) (Grant, error) {
39 m.grantLock.RLock()
40 defer m.grantLock.RUnlock()
41 grant, ok := m.grants[code]
42 if !ok {
43 return Grant{}, ErrGrantNotFound
44 }
45 return grant, nil
46 }
48 func (m *memstore) saveGrant(grant Grant) error {
49 m.grantLock.Lock()
50 defer m.grantLock.Unlock()
51 _, ok := m.grants[grant.Code]
52 if ok {
53 return ErrGrantAlreadyExists
54 }
55 m.grants[grant.Code] = grant
56 return nil
57 }
59 func (m *memstore) deleteGrant(code string) error {
60 m.grantLock.Lock()
61 defer m.grantLock.Unlock()
62 _, ok := m.grants[code]
63 if !ok {
64 return ErrGrantNotFound
65 }
66 delete(m.grants, code)
67 return nil
68 }