-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathassociation.go
186 lines (156 loc) · 3.52 KB
/
association.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
package gopenid
import (
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
"errors"
"fmt"
"hash"
"strings"
"time"
)
const (
AssociationLifetime = 24 * time.Hour
)
var (
ErrGeneratingAssociationFailed = errors.New("generating association failed")
ErrAssociationNotFound = errors.New("association not found")
ErrUnknownSessionType = errors.New("unknown session type")
ErrUnknownAssocType = errors.New("unknown association type")
AssocHmacSha1 = AssocType{
name: "HMAC-SHA1",
hashFunc: sha1.New,
secretSize: sha1.Size,
}
AssocHmacSha256 = AssocType{
name: "HMAC-SHA256",
hashFunc: sha256.New,
secretSize: sha256.Size,
}
SessionDhSha1 = SessionType{
name: "DH-SHA1",
assocTypes: []AssocType{
AssocHmacSha1,
},
}
SessionDhSha256 = SessionType{
name: "DH-SHA256",
assocTypes: []AssocType{
AssocHmacSha256,
},
}
SessionNoEncryption = SessionType{
name: "no-encryption",
assocTypes: []AssocType{
AssocHmacSha1,
AssocHmacSha256,
},
}
DefaultAssoc = AssocHmacSha256
DefaultSession = SessionDhSha256
)
type AssocType struct {
name string
hashFunc func() hash.Hash
secretSize int
}
func (t *AssocType) Name() string {
return t.name
}
func (t *AssocType) Hash() hash.Hash {
return t.hashFunc()
}
func (t *AssocType) GetSecretSize() int {
return t.secretSize
}
func GetAssocTypeByName(name string) (assocType AssocType, err error) {
switch name {
case "HMAC-SHA1":
assocType = AssocHmacSha1
case "HMAC-SHA256":
assocType = AssocHmacSha256
default:
err = ErrUnknownAssocType
}
return
}
type SessionType struct {
name string
assocTypes []AssocType
}
func (t *SessionType) Name() string {
return t.name
}
func GetSessionTypeByName(name string) (sessionType SessionType, err error) {
switch name {
case "no-encryption":
sessionType = SessionNoEncryption
case "DH-SHA1":
sessionType = SessionDhSha1
case "DH-SHA256":
sessionType = SessionDhSha256
default:
err = ErrUnknownSessionType
}
return
}
type Association struct {
assocType AssocType
handle string
secret []byte
expires time.Time
isStateless bool
}
func NewAssociation(assocType AssocType, handle string, secret []byte, expires time.Time, isStateless bool) *Association {
return &Association{
assocType: assocType,
handle: handle,
secret: secret,
expires: expires,
isStateless: isStateless,
}
}
func (assoc *Association) GetAssocType() AssocType {
return assoc.assocType
}
func (assoc *Association) GetHandle() string {
return assoc.handle
}
func (assoc *Association) GetSecret() []byte {
return assoc.secret
}
func (assoc *Association) GetExpires() time.Time {
return assoc.expires
}
func (assoc *Association) IsValid() bool {
return time.Now().Before(assoc.GetExpires())
}
func (assoc *Association) IsStateless() bool {
return assoc.isStateless
}
func (assoc *Association) Sign(msg Message, signed []string) (err error) {
order := make([]string, len(signed))
for i, key := range signed {
order[i] = fmt.Sprintf("openid.%s", key)
}
msg.AddArg(
NewMessageKey(msg.GetOpenIDNamespace(), "assoc_handle"),
MessageValue(assoc.GetHandle()),
)
kv, err := msg.ToKeyValue(order)
if err != nil {
return
}
mac := hmac.New(assoc.assocType.hashFunc, assoc.secret)
mac.Write(kv)
sig := EncodeBase64(mac.Sum(nil))
msg.AddArg(
NewMessageKey(msg.GetOpenIDNamespace(), "signed"),
MessageValue(strings.Join(signed, ",")),
)
msg.AddArg(
NewMessageKey(msg.GetOpenIDNamespace(), "sig"),
MessageValue(sig),
)
return
}