/
duoweb.go
187 lines (138 loc) · 4.69 KB
/
duoweb.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
// Package duoweb provides implements Duo Security's two-factor authentication for web applications
package duoweb
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"errors"
"strconv"
"strings"
"time"
)
type prefix string
const (
duoPrefix prefix = "TX"
appPrefix = "APP"
authPrefix = "AUTH"
enrollPrefix = "ENROLL"
enrollRequestPrefix = "ENROLL_REQUEST"
)
const duoExpire = 300
const appExpire = 3600
const ikeyLen = 20
const skeyLen = 40
const akeyLen = 40
// ErrUser is returned if the username is invalid
var ErrUSER = errors.New("ERR|The username passed to sign_request() is invalid.")
// ErrIKEY is returned if the integration key is invalid
var ErrIKEY = errors.New("ERR|The Duo integration key passed to sign_request() is invalid.")
// ErrSKEY is returned if the Duo secret key is invalid
var ErrSKEY = errors.New("ERR|The Duo secret key passed to sign_request() is invalid.")
// ErrAKEY is returned if the application secret key is invalid
var ErrAKEY = errors.New("ERR|The application secret key passed to sign_request() must be at least 40 characters.")
// ErrUnknown is returned if an unknown error occurs
var ErrUnknown = errors.New("ERR|An unknown error has occurred.")
// for mocking during tests
var timeNow = time.Now
func signVals(key, username, ikey string, reqPrefix prefix, expire int) string {
exp := timeNow().Add(time.Duration(expire) * time.Second)
val := username + "|" + ikey + "|" + strconv.Itoa(int(exp.Unix()))
cookie := string(reqPrefix) + "|" + base64.StdEncoding.EncodeToString([]byte(val))
h := hmac.New(sha1.New, []byte(key))
h.Write([]byte(cookie))
sig := h.Sum(nil)
return cookie + "|" + hex.EncodeToString(sig)
}
func parseVals(key, val string, reqPrefix prefix, ikey string) string {
ts := int(timeNow().Unix())
parts := strings.Split(val, "|")
if len(parts) != 3 {
return ""
}
vprefix, vb64, vsig := parts[0], parts[1], parts[2]
h := hmac.New(sha1.New, []byte(key))
h.Write([]byte(vprefix + "|" + vb64))
sig := h.Sum(nil)
bsig, err := hex.DecodeString(vsig)
if err != nil || len(bsig) != len(sig) {
return ""
}
// The reference implementations all compare hmac_sha1(sig, key) with
// hmac_sha1(bsig, key) to get around the lack of constant-time
// compares in the different languages.
if !hmac.Equal(sig, bsig) {
return ""
}
if string(reqPrefix) != vprefix {
return ""
}
decoded, err := base64.StdEncoding.DecodeString(vb64)
if err != nil {
return ""
}
cookieParts := strings.Split(string(decoded), "|")
if len(cookieParts) != 3 {
return ""
}
username, uIkey, expire := cookieParts[0], cookieParts[1], cookieParts[2]
if uIkey != ikey {
return ""
}
expireTstamp, err := strconv.Atoi(expire)
if err != nil {
return ""
}
if ts >= expireTstamp {
return ""
}
return username
}
// SignRequest generates a signed request for Duo authentication.
func SignRequest(ikey, skey, akey string, username string) (string, error) {
return signRequest(ikey, skey, akey, duoPrefix, username)
}
// SignEnrollRequest generates a signed enrollment request for Duo authentication.
func SignEnrollRequest(ikey, skey, akey string, username string) (string, error) {
return signRequest(ikey, skey, akey, enrollRequestPrefix, username)
}
func signRequest(ikey, skey, akey string, reqPrefix prefix, username string) (string, error) {
if username == "" || strings.IndexByte(username, '|') != -1 {
return "", ErrUSER
}
if ikey == "" || len(ikey) != ikeyLen {
return "", ErrIKEY
}
if skey == "" || len(skey) != skeyLen {
return "", ErrSKEY
}
if akey == "" || len(akey) < akeyLen {
return "", ErrAKEY
}
duoSig := signVals(skey, username, ikey, reqPrefix, duoExpire)
appSig := signVals(akey, username, ikey, appPrefix, appExpire)
return duoSig + ":" + appSig, nil
}
// VerifyResponse validates the signed response returned from Duo. Returns the
// username of the authenticated user.
func VerifyResponse(ikey, skey, akey, response string) string {
return verifyResponse(ikey, skey, akey, response, authPrefix)
}
// VerifyEnrollResponse validates the signed enrollment response returned from
// Duo. Returns the username of the authenticated user.
func VerifyEnrollResponse(ikey, skey, akey, response string) string {
return verifyResponse(ikey, skey, akey, response, enrollPrefix)
}
func verifyResponse(ikey, skey, akey, response string, reqPrefix prefix) string {
sigs := strings.Split(response, ":")
if len(sigs) != 2 {
return ""
}
authSig, appSig := sigs[0], sigs[1]
user := parseVals(skey, authSig, reqPrefix, ikey)
appUser := parseVals(akey, appSig, appPrefix, ikey)
if user == "" || appUser == "" || user != appUser {
return ""
}
return user
}