Debian Codegraph @

openid2/association.go

raw symbols SCIP
1 package openid2
2
3 import (
4 "crypto/hmac"
5 "crypto/rand"
6 "crypto/sha1"
7 "crypto/sha256"
8 "encoding/ascii85"
9 "encoding/base64"
10 "errors"
11 "fmt"
12 "hash"
13 "strings"
14 "time"
15 )
16
17 const (
18 hmacSHA1 = "HMAC-SHA1"
19 hmacSHA256 = "HMAC-SHA256"
20 )
21
22 var ErrDuplicateAssociation = errors.New("duplicate association")
23
24 // Association represents an openid association.
25 type Association struct {
26 // Endpoint is the OP Endpoint for which this association is valid. It might be blank.
27 Endpoint string
28
29 // Handle is used to identify the association with the OP Endpoint.
30 Handle string
31
32 // Secret is the secret established with the OP Endpoint.
33 Secret []byte
34
35 // Type is the type of this association.
36 Type string
37
38 // Expires holds the expiration time of the association.
39 Expires time.Time
40 }
41
42 func (a Association) sign(params map[string]string, signed []string) (string, error) {
43 var h hash.Hash
44 switch a.Type {
45 case hmacSHA1:
46 h = hmac.New(sha1.New, a.Secret)
47 case hmacSHA256:
48 h = hmac.New(sha256.New, a.Secret)
49 default:
50 return "", fmt.Errorf("unsupported association type %q", a.Type)
51 }
52 for _, k := range signed {
53 WriteKeyValuePair(h, k, params[k])
54 }
55 return base64.URLEncoding.EncodeToString(h.Sum(nil)), nil
56 }
57
58 // AssociationStore is used to store associations in both the server and client.
59 type AssociationStore interface {
60 // Add stores a new Association. If the specified Association is already
61 // present in the store then ErrDuplicateAssociation should be returned.
62 Add(a *Association) error
63
64 // Get retrieves the Association with the specified endpoint and handle.
65 // if there is no matching association in the store then ErrAssociationNotFound
66 // should be returned.
67 Get(endpoint, handle string) (*Association, error)
68
69 // Find retrieves all Associations for the specified endpoint.
70 Find(endpoint string) ([]*Association, error)
71
72 // Delete removes the Association with the specified endpoint and handle.
73 Delete(endpoint, handle string) error
74 }
75
76 // MemoryAssociationStore is an in memory implementation of AssociationStore.
77 type MemoryAssociationStore struct {
78 m map[string]map[string]Association
79 }
80
81 // NewMemoryAssociationStore creates a new in memory AssocationStore.
82 func NewMemoryAssociationStore() *MemoryAssociationStore {
83 return &MemoryAssociationStore{map[string]map[string]Association{}}
84 }
85
86 // Add implements AssociationStore.Add.
87 func (s *MemoryAssociationStore) Add(a *Association) error {
88 ass, err := s.Get(a.Endpoint, a.Handle)
89 if err != nil {
90 return err
91 }
92 if ass != nil {
93 return ErrDuplicateAssociation
94 }
95 m := s.m[a.Endpoint]
96 if m == nil {
97 m = make(map[string]Association)
98 }
99 m[a.Handle] = *a
100 s.m[a.Endpoint] = m
101 return nil
102 }
103
104 // Find implements AssociationStore.Find.
105 func (s *MemoryAssociationStore) Find(endpoint string) ([]*Association, error) {
106 var assocs []*Association
107 for _, a := range s.m[endpoint] {
108 assocs = append(assocs, &a)
109 }
110 return assocs, nil
111 }
112
113 // Get implements AssociationStore.Get.
114 func (s *MemoryAssociationStore) Get(endpoint, handle string) (*Association, error) {
115 if s.m[endpoint] == nil {
116 return nil, nil
117 }
118 a, ok := s.m[endpoint][handle]
119 if !ok {
120 return nil, nil
121 }
122 return &a, nil
123 }
124
125 // Delete implements AssociationStore.Delete.
126 func (s *MemoryAssociationStore) Delete(endpoint, handle string) error {
127 a, err := s.Get(endpoint, handle)
128 if err != nil {
129 return err
130 }
131 if a == nil {
132 return nil
133 }
134 delete(s.m[endpoint], handle)
135 return nil
136 }
137
138 // DefaultAssociationStore is the AssociationStore that will be used if no AssociationStore
139 // is specified.
140 var DefaultAssociationStore AssociationStore = NewMemoryAssociationStore()
141
142 func (h *Handler) getAssociation(requestHandle, nonce string) (a *Association, err error) {
143 store := h.Associations
144 if store == nil {
145 store = DefaultAssociationStore
146 }
147 if requestHandle != "" {
148 a, err = store.Get("", requestHandle)
149 if err != nil {
150 return
151 }
152 if a != nil {
153 if time.Now().Before(a.Expires) {
154 return
155 }
156 store.Delete("", requestHandle)
157 }
158 }
159 secret := make([]byte, 128)
160 if _, err = rand.Read(secret); err != nil {
161 return
162 }
163 a = &Association{
164 Secret: secret,
165 Type: hmacSHA256,
166 Expires: time.Now().Add(time.Minute),
167 }
168 err = saveAssociation(store, a)
169 if err != nil {
170 a = nil
171 }
172 return
173 }
174
175 func (h *Handler) associate(params map[string]string) (map[string]string, error) {
176 // store := h.Associations
177 // if store == nil {
178 // assocs = DefaultAssociationStore
179 // }
180
181 switch params["session_type"] {
182 // case "DH-SHA1":
183 // case "DH-SHA256":
184 // case "no-encryption":
185 // return h.associateNoEncryption(params)
186 default:
187 return nil, unsupportedSessionTypeError(params["session_type"])
188 }
189 }
190
191 func (h *Handler) checkAuthentication(params map[string]string) (map[string]string, error) {
192 store := h.Associations
193 if store == nil {
194 store = DefaultAssociationStore
195 }
196 assoc, err := store.Get("", params["assoc_handle"])
197 if err != nil {
198 return nil, err
199 }
200 if assoc == nil {
201 return map[string]string{
202 "ns": Namespace,
203 "is_valid": "false",
204 }, nil
205 }
206 signed := strings.Split(params["signed"], ",")
207 sig, err := assoc.sign(params, signed)
208 if err != nil {
209 return nil, err
210 }
211 if params["sig"] != sig {
212 return map[string]string{
213 "ns": Namespace,
214 "is_valid": "false",
215 }, nil
216 }
217 rparams := map[string]string{
218 "ns": Namespace,
219 "is_valid": "true",
220 }
221 // TODO: deal with invalid_handle
222 store.Delete("", assoc.Handle)
223 return rparams, nil
224 }
225
226 func saveAssociation(store AssociationStore, a *Association) error {
227 for i := 0; i < 10; i++ {
228 var handle [16]byte
229 if _, err := rand.Read(handle[:]); err != nil {
230 return err
231 }
232 ehandle := make([]byte, ascii85.MaxEncodedLen(len(handle)))
233 n := ascii85.Encode(ehandle, handle[:])
234 a.Handle = string(ehandle[:n])
235 err := store.Add(a)
236 if err == nil {
237 return nil
238 }
239 if err != ErrDuplicateAssociation {
240 return err
241 }
242 }
243 return errors.New("cannot store association")
244 }
245
246 type unsupportedSessionTypeError string
247
248 func (e unsupportedSessionTypeError) Error() string {
249 return fmt.Sprintf("session type %q not supported", string(e))
250 }
251
252 func (e unsupportedSessionTypeError) errorParams() map[string]string {
253 return map[string]string{
254 "error-code": "unsupported-type",
255 }
256 }