auth
auth/context.go
Update CheckEndpoints for strict checking, add CountEndpoints. Create a "strict" mode for CheckEndpoints that will only return true on an exact match, and update the memstore implementation accordingly. Add tests to make sure that the strict mode is adhered to. We need this mode because in certain situations (e.g., the client has more than one endpoint registered), the spec demands a full-string comparison. Add a CountEndpoints method to the ClientStore that will return the number of endpoints registered for a specific client. As we just mentioned, the rules for how a redirect URI is validated depend upon the number of endpoints a client has registered, so we need to be able to get at that number.
1 package auth
3 import (
4 "errors"
5 "html/template"
6 "io"
7 "time"
9 "code.secondbit.org/uuid"
10 )
12 var (
13 ErrNoTemplate = errors.New("no Template specified for the Context")
14 )
16 type Context struct {
17 template *template.Template
18 clients ClientStore
19 grants GrantStore
20 profiles ProfileStore
21 tokens TokenStore
22 }
24 func (c Context) Render(out io.Writer, name string, data interface{}) error {
25 if c.template == nil {
26 return ErrNoTemplate
27 }
28 return c.template.ExecuteTemplate(out, name, data)
29 }
31 func (c Context) GetClient(id uuid.ID) (Client, error) {
32 if c.clients == nil {
33 return Client{}, ErrNoClientStore
34 }
35 return c.clients.GetClient(id)
36 }
38 func (c Context) SaveClient(client Client) error {
39 if c.clients == nil {
40 return ErrNoClientStore
41 }
42 return c.clients.SaveClient(client)
43 }
45 func (c Context) UpdateClient(id uuid.ID, change ClientChange) error {
46 if c.clients == nil {
47 return ErrNoClientStore
48 }
49 return c.clients.UpdateClient(id, change)
50 }
52 func (c Context) DeleteClient(id uuid.ID) error {
53 if c.clients == nil {
54 return ErrNoClientStore
55 }
56 return c.clients.DeleteClient(id)
57 }
59 func (c Context) ListClientsByOwner(ownerID uuid.ID, num, offset int) ([]Client, error) {
60 if c.clients == nil {
61 return []Client{}, ErrNoClientStore
62 }
63 return c.clients.ListClientsByOwner(ownerID, num, offset)
64 }
66 func (c Context) AddEndpoint(client uuid.ID, endpoint Endpoint) error {
67 if c.clients == nil {
68 return ErrNoClientStore
69 }
70 return c.clients.AddEndpoint(client, endpoint)
71 }
73 func (c Context) RemoveEndpoint(client, endpoint uuid.ID) error {
74 if c.clients == nil {
75 return ErrNoClientStore
76 }
77 return c.clients.RemoveEndpoint(client, endpoint)
78 }
80 func (c Context) CheckEndpoint(client uuid.ID, URI string) (bool, error) {
81 if c.clients == nil {
82 return false, ErrNoClientStore
83 }
84 return c.clients.CheckEndpoint(client, URI)
85 }
87 func (c Context) ListEndpoints(client uuid.ID, num, offset int) ([]Endpoint, error) {
88 if c.clients == nil {
89 return []Endpoint{}, ErrNoClientStore
90 }
91 return c.clients.ListEndpoints(client, num, offset)
92 }
94 func (c Context) GetGrant(code string) (Grant, error) {
95 if c.grants == nil {
96 return Grant{}, ErrNoGrantStore
97 }
98 return c.grants.GetGrant(code)
99 }
101 func (c Context) SaveGrant(grant Grant) error {
102 if c.grants == nil {
103 return ErrNoGrantStore
104 }
105 return c.grants.SaveGrant(grant)
106 }
108 func (c Context) DeleteGrant(code string) error {
109 if c.grants == nil {
110 return ErrNoGrantStore
111 }
112 return c.grants.DeleteGrant(code)
113 }
115 func (c Context) GetProfileByID(id uuid.ID) (Profile, error) {
116 if c.profiles == nil {
117 return Profile{}, ErrNoProfileStore
118 }
119 return c.profiles.GetProfileByID(id)
120 }
122 func (c Context) GetProfileByLogin(loginType, value string) (Profile, error) {
123 if c.profiles == nil {
124 return Profile{}, ErrNoProfileStore
125 }
126 return c.profiles.GetProfileByLogin(loginType, value)
127 }
129 func (c Context) SaveProfile(profile Profile) error {
130 if c.profiles == nil {
131 return ErrNoProfileStore
132 }
133 return c.profiles.SaveProfile(profile)
134 }
136 func (c Context) UpdateProfile(id uuid.ID, change ProfileChange) error {
137 if c.profiles == nil {
138 return ErrNoProfileStore
139 }
140 return c.profiles.UpdateProfile(id, change)
141 }
143 func (c Context) UpdateProfiles(ids []uuid.ID, change BulkProfileChange) error {
144 if c.profiles == nil {
145 return ErrNoProfileStore
146 }
147 return c.profiles.UpdateProfiles(ids, change)
148 }
150 func (c Context) DeleteProfile(id uuid.ID) error {
151 if c.profiles == nil {
152 return ErrNoProfileStore
153 }
154 return c.profiles.DeleteProfile(id)
155 }
157 func (c Context) AddLogin(login Login) error {
158 if c.profiles == nil {
159 return ErrNoProfileStore
160 }
161 return c.profiles.AddLogin(login)
162 }
164 func (c Context) RemoveLogin(loginType, value string, profile uuid.ID) error {
165 if c.profiles == nil {
166 return ErrNoProfileStore
167 }
168 return c.profiles.RemoveLogin(loginType, value, profile)
169 }
171 func (c Context) RecordLoginUse(loginType, value string, when time.Time) error {
172 if c.profiles == nil {
173 return ErrNoProfileStore
174 }
175 return c.profiles.RecordLoginUse(loginType, value, when)
176 }
178 func (c Context) ListLogins(profile uuid.ID, num, offset int) ([]Login, error) {
179 if c.profiles == nil {
180 return []Login{}, ErrNoProfileStore
181 }
182 return c.profiles.ListLogins(profile, num, offset)
183 }
185 func (c Context) GetToken(token string, refresh bool) (Token, error) {
186 if c.tokens == nil {
187 return Token{}, ErrNoTokenStore
188 }
189 return c.tokens.GetToken(token, refresh)
190 }
192 func (c Context) SaveToken(token Token) error {
193 if c.tokens == nil {
194 return ErrNoTokenStore
195 }
196 return c.tokens.SaveToken(token)
197 }
199 func (c Context) RemoveToken(token string) error {
200 if c.tokens == nil {
201 return ErrNoTokenStore
202 }
203 return c.tokens.RemoveToken(token)
204 }
206 func (c Context) GetTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) {
207 if c.tokens == nil {
208 return []Token{}, ErrNoTokenStore
209 }
210 return c.tokens.GetTokensByProfileID(profileID, num, offset)
211 }