-
Notifications
You must be signed in to change notification settings - Fork 16
/
approval_request.go
66 lines (52 loc) · 1.75 KB
/
approval_request.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
package authy
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// OneTouchStatus is the type of the OneTouch statuses.
type OneTouchStatus string
var (
// OneTouchStatusApproved is the approved status of an approval request
OneTouchStatusApproved OneTouchStatus = "approved"
// OneTouchStatusPending is the pending status of an approval request
OneTouchStatusPending OneTouchStatus = "pending"
// OneTouchStatusDenied is the denied status of an approval request
OneTouchStatusDenied OneTouchStatus = "denied"
// OneTouchStatusExpired is the expired status of an approval request
OneTouchStatusExpired OneTouchStatus = "expired"
)
// ApprovalRequest is the approval request response.
type ApprovalRequest struct {
HTTPResponse *http.Response
Status OneTouchStatus `json:"status"`
UUID string `json:"uuid"`
Notified bool `json:"notified"`
}
// NewApprovalRequest returns an instance of ApprovalRequest.
func NewApprovalRequest(response *http.Response) (*ApprovalRequest, error) {
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
jsonResponse := struct {
Success bool `json:"success"`
ApprovalRequest *ApprovalRequest `json:"approval_request"`
Message string `json:"message"`
}{}
err = json.Unmarshal(body, &jsonResponse)
if err != nil {
return nil, err
}
if !jsonResponse.Success {
return nil, fmt.Errorf("invalid approval request response: %s", jsonResponse.Message)
}
approvalRequest := jsonResponse.ApprovalRequest
approvalRequest.HTTPResponse = response
return approvalRequest, nil
}
// Valid returns true if the approval request was valid.
func (request *ApprovalRequest) Valid() bool {
return request.HTTPResponse.StatusCode == 200
}