-
Notifications
You must be signed in to change notification settings - Fork 3
/
message.go
412 lines (348 loc) · 8.87 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package gopenid
import (
"bytes"
"errors"
"fmt"
"net/url"
"sort"
"strings"
"sync"
)
const (
NsOpenID10 NamespaceURI = "http://openid.net/signon/1.0" // Namespace for OpenID 1.0
NsOpenID11 NamespaceURI = "http://openid.net/signon/1.1" // Namespace for OpenID 1.1
NsOpenID20 NamespaceURI = "http://specs.openid.net/auth/2.0" // Namespace for OpenID 2.0
NsIdentifierSelect NamespaceURI = "http://specs.openid.net/auth/2.0/identifier_select"
)
var (
ErrMalformedMessage = errors.New("malformed Message")
ErrUnsupportedVersion = errors.New("unsupported version")
ErrKeyContainsColon = errors.New("key contains colon")
ErrKeyContainsNewLine = errors.New("key contains new line")
ErrValueNotFound = errors.New("value not found")
protocolFields = []string{
"assoc_handle",
"assoc_type",
"claimed_id",
"contact",
"delegate",
"dh_consumer_public",
"dh_gen",
"dh_modulus",
"error",
"identity",
"invalidate_handle",
"mode",
"ns",
"op_endpoint",
"openid",
"realm",
"reference",
"response_nonce",
"return_to",
"server",
"session_type",
"sig",
"signed",
"trust_root",
}
)
// NamespaceURI represents URI for Namespace.
type NamespaceURI string
// String returns as string.
func (ns NamespaceURI) String() string {
return string(ns)
}
// MessageKey is a key of Message.
type MessageKey struct {
namespace NamespaceURI
key string
}
// NewMessageKey returns a new MessageKey given NamespaceURI, and key.
func NewMessageKey(ns NamespaceURI, key string) MessageKey {
return MessageKey{
namespace: ns,
key: key,
}
}
// GetNamespace returns NamespaceURI of k.
func (k *MessageKey) GetNamespace() NamespaceURI {
return k.namespace
}
// GetKey returns key of k.
func (k *MessageKey) GetKey() string {
return k.key
}
// MessageValue is a value of Message.
type MessageValue string
// String returns v as string.
func (v MessageValue) String() string {
return string(v)
}
// Bytes returns v as []byte.
func (v MessageValue) Bytes() []byte {
return []byte(v)
}
// Message represents OpenID protocol message.
type Message struct {
namespace NamespaceURI
nsuri2nsalias map[NamespaceURI]string
nsalias2nsuri map[string]NamespaceURI
args map[MessageKey]MessageValue
sync.Mutex
}
// NewMessage returns a new Message with the given NamespaceURI.
func NewMessage(ns NamespaceURI) Message {
return Message{
namespace: ns,
nsuri2nsalias: make(map[NamespaceURI]string),
nsalias2nsuri: make(map[string]NamespaceURI),
args: make(map[MessageKey]MessageValue),
}
}
// GetOpenIDNamespace returns NamespaceURI of m.
func (m *Message) GetOpenIDNamespace() NamespaceURI {
return m.namespace
}
// GetNamespaceURI returns NamespaceURI mapped to the given alias.
// If NamespaceURI does not exist, GetNamespaceURI returns false as 2nd return value.
func (m *Message) GetNamespaceURI(alias string) (NamespaceURI, bool) {
if alias == "openid" {
return m.GetOpenIDNamespace(), true
}
nsuri, ok := m.nsalias2nsuri[alias]
return nsuri, ok
}
// GetNamespaceAlias returns alias is pointing to the given NamespaceURI.
// If alias does not exist, GetNamespaceURI returns false as 2nd return value.
func (m *Message) GetNamespaceAlias(uri NamespaceURI) (string, bool) {
if uri == m.GetOpenIDNamespace() {
return "", true
}
nsalias, ok := m.nsuri2nsalias[uri]
return nsalias, ok
}
// SetNamespaceAlias is a function to register relationship between alias and NamespaceURI.
func (m *Message) SetNamespaceAlias(alias string, uri NamespaceURI) {
m.nsuri2nsalias[uri] = alias
m.nsalias2nsuri[alias] = uri
}
// GetArg returns value of given k.
// If value does not exist, GetArg returns false as 2nd return value.
func (m *Message) GetArg(k MessageKey) (MessageValue, bool) {
v, ok := m.args[k]
return v, ok
}
// AddArg registers given the v as value of k.
func (m *Message) AddArg(k MessageKey, v MessageValue) {
m.args[k] = v
}
// GetArgs returns the subset of m as map[MessageKey]MessageValue.
// Returned subset contains values related to the given NamespaceURI.
func (m *Message) GetArgs(nsuri NamespaceURI) map[MessageKey]MessageValue {
ret := make(map[MessageKey]MessageValue)
for k, v := range m.args {
if k.GetNamespace() == nsuri {
ret[k] = v
}
}
return ret
}
// ToQuery returns the m as url.Values.
func (m *Message) ToQuery() url.Values {
query := url.Values{
"openid.ns": []string{m.namespace.String()},
}
for nsalias, nsuri := range m.nsalias2nsuri {
query[fmt.Sprintf("openid.ns.%s", nsalias)] = []string{nsuri.String()}
}
for key, value := range m.args {
var queryKey string
if alias, _ := m.GetNamespaceAlias(key.GetNamespace()); alias == "" {
queryKey = fmt.Sprintf("openid.%s", key.GetKey())
} else {
queryKey = fmt.Sprintf("openid.%s.%s", alias, key.GetKey())
}
query[queryKey] = []string{value.String()}
}
return query
}
// Keys returns all of keys m has.
func (m *Message) Keys() []string {
ret := make([]string, 1, len(m.args)+1)
ret[0] = "openid.ns"
for key := range m.args {
parts := make([]string, 0, 3)
parts = append(parts, "openid")
if nsalias, ok := m.GetNamespaceAlias(key.GetNamespace()); !ok {
continue
} else if nsalias != "" {
parts = append(parts, nsalias)
}
parts = append(parts, key.GetKey())
ret = append(
ret,
strings.Join(parts, "."),
)
}
return ret
}
// ToKeyValue returns part of m as KeyValue format.
// Returnd KeyValue follows the given order.
func (m *Message) ToKeyValue(order []string) (b []byte, err error) {
validator := func(str string, isKey bool) error {
if isKey && strings.Index(str, ":") > -1 {
return ErrKeyContainsColon
}
if strings.Index(str, "\n") > -1 {
return ErrKeyContainsNewLine
}
return nil
}
lines := make([][]byte, 0, len(order)+1)
for _, key := range order {
if !strings.HasPrefix(key, "openid.") {
err = ErrValueNotFound
return
}
key = key[7:]
var (
parts = strings.SplitN(key, ".", 2)
value string
)
if len(parts) > 1 {
if parts[0] == "ns" {
v, ok := m.nsalias2nsuri[parts[1]]
if !ok {
err = ErrValueNotFound
return
}
value = v.String()
} else {
v, ok := m.args[NewMessageKey(m.nsalias2nsuri[parts[0]], parts[1])]
if !ok {
err = ErrValueNotFound
return
}
value = v.String()
}
} else if parts[0] == "ns" {
value = m.namespace.String()
} else {
v, ok := m.args[NewMessageKey(m.namespace, parts[0])]
if !ok {
err = ErrValueNotFound
return
}
value = v.String()
}
if err = validator(key, true); err != nil {
return
} else if err = validator(value, false); err != nil {
return
}
lines = append(
lines,
[]byte(fmt.Sprintf("%s:%s", key, value)),
)
}
lines = append(lines, nil)
b = bytes.Join(lines, []byte{'\n'})
return
}
// Copy returns copy of m.
func (m *Message) Copy() Message {
m.Lock()
defer m.Unlock()
msg := NewMessage(m.namespace)
for k, v := range m.nsuri2nsalias {
msg.nsuri2nsalias[k] = v
}
for k, v := range m.nsalias2nsuri {
msg.nsalias2nsuri[k] = v
}
for k, v := range m.args {
msg.args[k] = v
}
return msg
}
// MessageFromQuery returns a new message as a result of parsing the given query.
func MessageFromQuery(req url.Values) (msg Message, err error) {
var (
ns NamespaceURI
nsmap = make(map[string]NamespaceURI)
args = make(map[string]map[string]string)
)
for key, values := range req {
if len(values) > 1 {
// Messages MUST NOT contain multiple parameters with the same name
err = ErrMalformedMessage
return
} else if !strings.HasPrefix(key, "openid.") {
continue
}
var (
parts = strings.SplitN(key[7:], ".", 2)
value = values[0]
nsalias string
key string
)
if len(parts) == 1 {
key = parts[0]
} else {
nsalias = parts[0]
key = parts[1]
}
if nsalias == "" && key == "ns" {
ns = NamespaceURI(value)
} else if nsalias == "ns" {
if strings.Index(key, ".") >= 0 {
// A namespace alias MUST NOT contain a period
err = ErrMalformedMessage
return
} else if idx := sort.SearchStrings(protocolFields, key); protocolFields[idx] == key {
// The namespace alias is not allowed
err = ErrMalformedMessage
return
}
nsmap[key] = NamespaceURI(value)
} else {
if _, ok := args[nsalias]; !ok {
args[nsalias] = make(map[string]string)
}
args[nsalias][key] = value
}
}
switch ns {
case NsOpenID10:
case NsOpenID11:
case NsOpenID20:
case "":
default:
err = ErrUnsupportedVersion
return
}
if ns == "" {
// OpenID Authentication 1.1 Compatibility mode
ns = NsOpenID11
}
msg = NewMessage(ns)
for nsalias, kv := range args {
nsuri, isKnownAlias := nsmap[nsalias]
if isKnownAlias {
msg.SetNamespaceAlias(nsalias, nsuri)
} else {
nsuri = ns
}
for key, value := range kv {
if !isKnownAlias && nsalias != "" {
key = fmt.Sprintf("%s.%s", nsalias, key)
}
msg.AddArg(
NewMessageKey(nsuri, key),
MessageValue(value),
)
}
}
return
}