auth

Paddy 2014-10-22 Parent:b620d32d9903 Child:e45bfa2abc00

55:cfab12566289 Go to Latest

auth/context.go

Update Context with new CheckEndpoint and CountEndpoints. Update the Context.CheckEndpoint method to pass through the new "strict" parameter that controls how URLs are checked for validation. Add a Context.CountEndpoints method to safely access the number of endpoints in stored for the client in the associent ClientStore. Finally, remove the error return from Context.Render, as we'd only ever want to log that, anyways. So we may as well just log it from the central function.

History
1 package auth
3 import (
4 "errors"
5 "html/template"
6 "io"
7 "log"
8 "time"
10 "code.secondbit.org/uuid"
11 )
13 var (
14 ErrNoTemplate = errors.New("no Template specified for the Context")
15 )
17 type Context struct {
18 template *template.Template
19 clients ClientStore
20 grants GrantStore
21 profiles ProfileStore
22 tokens TokenStore
23 }
25 func (c Context) Render(out io.Writer, name string, data interface{}) {
26 if c.template == nil {
27 log.Println(ErrNoTemplate)
28 }
29 err := c.template.ExecuteTemplate(out, name, data)
30 if err != nil {
31 log.Println("Error executing template", name, ":", err)
32 }
33 }
35 func (c Context) GetClient(id uuid.ID) (Client, error) {
36 if c.clients == nil {
37 return Client{}, ErrNoClientStore
38 }
39 return c.clients.GetClient(id)
40 }
42 func (c Context) SaveClient(client Client) error {
43 if c.clients == nil {
44 return ErrNoClientStore
45 }
46 return c.clients.SaveClient(client)
47 }
49 func (c Context) UpdateClient(id uuid.ID, change ClientChange) error {
50 if c.clients == nil {
51 return ErrNoClientStore
52 }
53 return c.clients.UpdateClient(id, change)
54 }
56 func (c Context) DeleteClient(id uuid.ID) error {
57 if c.clients == nil {
58 return ErrNoClientStore
59 }
60 return c.clients.DeleteClient(id)
61 }
63 func (c Context) ListClientsByOwner(ownerID uuid.ID, num, offset int) ([]Client, error) {
64 if c.clients == nil {
65 return []Client{}, ErrNoClientStore
66 }
67 return c.clients.ListClientsByOwner(ownerID, num, offset)
68 }
70 func (c Context) AddEndpoint(client uuid.ID, endpoint Endpoint) error {
71 if c.clients == nil {
72 return ErrNoClientStore
73 }
74 return c.clients.AddEndpoint(client, endpoint)
75 }
77 func (c Context) RemoveEndpoint(client, endpoint uuid.ID) error {
78 if c.clients == nil {
79 return ErrNoClientStore
80 }
81 return c.clients.RemoveEndpoint(client, endpoint)
82 }
84 func (c Context) CheckEndpoint(client uuid.ID, URI string, strict bool) (bool, error) {
85 if c.clients == nil {
86 return false, ErrNoClientStore
87 }
88 return c.clients.CheckEndpoint(client, URI, strict)
89 }
91 func (c Context) ListEndpoints(client uuid.ID, num, offset int) ([]Endpoint, error) {
92 if c.clients == nil {
93 return []Endpoint{}, ErrNoClientStore
94 }
95 return c.clients.ListEndpoints(client, num, offset)
96 }
98 func (c Context) CountEndpoints(client uuid.ID) (int64, error) {
99 if c.clients == nil {
100 return 0, ErrNoClientStore
101 }
102 return c.clients.CountEndpoints(client)
103 }
105 func (c Context) GetGrant(code string) (Grant, error) {
106 if c.grants == nil {
107 return Grant{}, ErrNoGrantStore
108 }
109 return c.grants.GetGrant(code)
110 }
112 func (c Context) SaveGrant(grant Grant) error {
113 if c.grants == nil {
114 return ErrNoGrantStore
115 }
116 return c.grants.SaveGrant(grant)
117 }
119 func (c Context) DeleteGrant(code string) error {
120 if c.grants == nil {
121 return ErrNoGrantStore
122 }
123 return c.grants.DeleteGrant(code)
124 }
126 func (c Context) GetProfileByID(id uuid.ID) (Profile, error) {
127 if c.profiles == nil {
128 return Profile{}, ErrNoProfileStore
129 }
130 return c.profiles.GetProfileByID(id)
131 }
133 func (c Context) GetProfileByLogin(loginType, value string) (Profile, error) {
134 if c.profiles == nil {
135 return Profile{}, ErrNoProfileStore
136 }
137 return c.profiles.GetProfileByLogin(loginType, value)
138 }
140 func (c Context) SaveProfile(profile Profile) error {
141 if c.profiles == nil {
142 return ErrNoProfileStore
143 }
144 return c.profiles.SaveProfile(profile)
145 }
147 func (c Context) UpdateProfile(id uuid.ID, change ProfileChange) error {
148 if c.profiles == nil {
149 return ErrNoProfileStore
150 }
151 return c.profiles.UpdateProfile(id, change)
152 }
154 func (c Context) UpdateProfiles(ids []uuid.ID, change BulkProfileChange) error {
155 if c.profiles == nil {
156 return ErrNoProfileStore
157 }
158 return c.profiles.UpdateProfiles(ids, change)
159 }
161 func (c Context) DeleteProfile(id uuid.ID) error {
162 if c.profiles == nil {
163 return ErrNoProfileStore
164 }
165 return c.profiles.DeleteProfile(id)
166 }
168 func (c Context) AddLogin(login Login) error {
169 if c.profiles == nil {
170 return ErrNoProfileStore
171 }
172 return c.profiles.AddLogin(login)
173 }
175 func (c Context) RemoveLogin(loginType, value string, profile uuid.ID) error {
176 if c.profiles == nil {
177 return ErrNoProfileStore
178 }
179 return c.profiles.RemoveLogin(loginType, value, profile)
180 }
182 func (c Context) RecordLoginUse(loginType, value string, when time.Time) error {
183 if c.profiles == nil {
184 return ErrNoProfileStore
185 }
186 return c.profiles.RecordLoginUse(loginType, value, when)
187 }
189 func (c Context) ListLogins(profile uuid.ID, num, offset int) ([]Login, error) {
190 if c.profiles == nil {
191 return []Login{}, ErrNoProfileStore
192 }
193 return c.profiles.ListLogins(profile, num, offset)
194 }
196 func (c Context) GetToken(token string, refresh bool) (Token, error) {
197 if c.tokens == nil {
198 return Token{}, ErrNoTokenStore
199 }
200 return c.tokens.GetToken(token, refresh)
201 }
203 func (c Context) SaveToken(token Token) error {
204 if c.tokens == nil {
205 return ErrNoTokenStore
206 }
207 return c.tokens.SaveToken(token)
208 }
210 func (c Context) RemoveToken(token string) error {
211 if c.tokens == nil {
212 return ErrNoTokenStore
213 }
214 return c.tokens.RemoveToken(token)
215 }
217 func (c Context) GetTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) {
218 if c.tokens == nil {
219 return []Token{}, ErrNoTokenStore
220 }
221 return c.tokens.GetTokensByProfileID(profileID, num, offset)
222 }