-
Notifications
You must be signed in to change notification settings - Fork 34
/
duo.go
214 lines (187 loc) · 6.38 KB
/
duo.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
package duo
import (
"encoding/json"
"errors"
"fmt"
"html"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
"time"
"github.com/riotgames/key-conjurer/api/consts"
)
var (
ErrorDuoCommunication = errors.New("unable to communicate with Duo")
ErrorDuoMfaNotAllow = errors.New("MFA was not allowed")
ErrorDuoArgsError = errors.New("there was an error parsing arguments for Duo push request")
ErrorDuoPushError = errors.New("there was an error sending a Duo push request")
ErrorCannotFindSid = errors.New("Cannot find sid")
ErrorHTTPBodyError = errors.New("Unable to read http response body")
ErrorJSONMarshalError = errors.New("Unable to marshal json")
ErrorJSONUnmarshalError = errors.New("Unable to unmarshal json")
)
// Duo scripts the Duo Web API interaction
type Duo struct {
httpClient *http.Client
}
type duoPromptResponse struct {
Stat string `json:"stat"`
Response struct {
TxID string `json:"txid"`
} `json:"response"`
}
type pushResponse struct {
StatusCode string `json:"status_code"`
Parent string `json:"parent"`
Result string `json:"result"`
Cookie string `json:"cookie"`
ResultURL string `json:"result_url"`
}
type duoPushResponse struct {
Stat string `json:"stat"`
Response pushResponse `json:"response"`
}
// New returns a new Duo client that uses the provided logger
func New() Duo {
duoHTTPClient := &http.Client{
Timeout: time.Second * consts.HttpTimeoutInSeconds,
}
return Duo{httpClient: duoHTTPClient}
}
// SendPush emulates the workflow of the Duo WebAPI and sends the
// requesting user a push to the device set as "phone1"
func (d *Duo) SendPush(txSignature, stateToken, callbackURL, apiHostName string) (string, error) {
sid, err := d.getSid(txSignature, stateToken, callbackURL, apiHostName)
if err != nil {
return "", err
}
d.prepareForPush(sid, txSignature, callbackURL, apiHostName)
txid, err := d.sendMfaPush(sid, txSignature, callbackURL, apiHostName)
if err != nil {
return "", err
}
mfaStatus, err := d.checkMfaStatus(sid, txid, apiHostName)
if err != nil {
return "", err
}
if strings.ToLower(mfaStatus.Stat) != "ok" {
return "", ErrorDuoMfaNotAllow
}
return mfaStatus.Response.Cookie, nil
}
func (d *Duo) getSid(txSignature, stateToken, callbackURL, apiHostName string) (string, error) {
reqURL := fmt.Sprintf("https://%v/frame/web/v1/auth?%v",
apiHostName,
url.Values{
"parent": {callbackURL},
"tx": {txSignature}}.Encode())
req, _ := http.NewRequest("POST", reqURL, strings.NewReader(url.Values{"parent": {callbackURL}}.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := d.httpClient.Do(req)
if err != nil {
return "", ErrorDuoCommunication
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", ErrorHTTPBodyError
}
// View duo_test.go "Test_Duo_getSid" to see the expected cases that this
// regex will match
data := regexp.MustCompile("<input.*name=['\"]?sid['\"]?.*?value=['\"]?(.*?)['\"]?>").FindSubmatch(body)
if len(data) != 2 {
return "", ErrorCannotFindSid
}
sid := html.UnescapeString(string(data[1]))
return sid, nil
}
func (d *Duo) prepareForPush(sid, txSignature, callbackURL, apiHostName string) error {
const certsURL = "https://certs-duo1.duosecurity.com/frame/client_cert"
reqURL := fmt.Sprintf("https://%v/frame/web/v1/auth?%v",
apiHostName,
url.Values{
"parent": {callbackURL},
"tx": {txSignature}}.Encode())
req, _ := http.NewRequest("POST", reqURL, strings.NewReader(url.Values{
"sid": []string{sid},
"certs_url": []string{certsURL}}.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := d.httpClient.Do(req)
if err != nil {
return ErrorDuoCommunication
}
defer resp.Body.Close()
return nil
}
func (d *Duo) sendMfaPush(sid, txSignature, callbackURL, apiHostName string) (string, error) {
reqURL := fmt.Sprintf("https://%v/frame/prompt?%v",
apiHostName,
url.Values{
"parent": {callbackURL},
"tx": {txSignature}}.Encode())
req, _ := http.NewRequest("POST", reqURL, strings.NewReader(url.Values{
"sid": []string{sid},
"device": []string{"phone1"},
"factor": []string{"Duo Push"}}.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := d.httpClient.Do(req)
if err != nil {
return "", ErrorDuoCommunication
}
dec := json.NewDecoder(resp.Body)
defer resp.Body.Close()
duoPromptResponse := duoPromptResponse{}
if err := dec.Decode(&duoPromptResponse); err != nil {
return "", ErrorJSONMarshalError
}
// Checking MFA status does not block on the first attempt
// to check the status after pushing. The response just says
// that a push was sent.
mfaResponse, err := d.checkMfaStatus(sid, duoPromptResponse.Response.TxID, apiHostName)
if err != nil {
return "", ErrorDuoPushError
}
if strings.ToLower(mfaResponse.Response.StatusCode) != "pushed" {
return "", ErrorDuoPushError
}
return duoPromptResponse.Response.TxID, nil
}
func (d *Duo) checkMfaStatus(sid, txid, apiHostName string) (*duoPushResponse, error) {
reqURL := fmt.Sprintf("https://%v/frame/status", apiHostName)
req, _ := http.NewRequest("POST", reqURL, strings.NewReader(url.Values{
"sid": []string{sid},
"txid": []string{txid}}.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := d.httpClient.Do(req)
if err != nil {
return nil, ErrorDuoCommunication
}
defer resp.Body.Close()
pushResponse := duoPushResponse{}
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&pushResponse); err != nil {
return nil, ErrorJSONUnmarshalError
}
// Duo changed their workflow so actual needed response is one more
// request after verifying the push. This handles the complete
// flow to retrieve the proper response.
if pushResponse.Response.ResultURL != "" {
reqURL := fmt.Sprintf("https://%v%v", apiHostName, pushResponse.Response.ResultURL)
req, _ := http.NewRequest("POST", reqURL, strings.NewReader(url.Values{
"sid": []string{sid}}.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := d.httpClient.Do(req)
if err != nil {
return nil, ErrorDuoCommunication
}
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
pushResponse = duoPushResponse{}
if err := dec.Decode(&pushResponse); err != nil {
return nil, ErrorJSONUnmarshalError
}
}
return &pushResponse, nil
}