/
jumpcloud_protect.go
108 lines (88 loc) · 3.01 KB
/
jumpcloud_protect.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
package jumpcloud
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"path"
"time"
"github.com/pkg/errors"
)
type JumpCloudPushResponse struct {
ID string `json:"id"`
ExpiresAt time.Time `json:"expiresAt"`
InitiatedAt time.Time `json:"initiatedAt"`
Status string `json:"status"`
UserId string `json:"userId"`
}
func (jc *Client) jumpCloudProtectAuth(submitUrl string, xsrfToken string) (*http.Response, error) {
jumpCloudParsedURL, err := url.Parse(submitUrl)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("unable to parse submit url, url=%s", jumpCloudProtectSubmitURL))
}
req, err := http.NewRequest("POST", jumpCloudParsedURL.String(), emptyJSONIOReader())
if err != nil {
return nil, errors.Wrap(err, "error building jumpcloud protect auth request")
}
ensureHeaders(xsrfToken, req)
res, err := jc.client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "error retrieving JumpCloud PUSH payload")
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, errors.New("error retrieving JumpCloud PUSH payload, non 200 status returned")
}
jpResp, err := io.ReadAll(res.Body)
if err != nil {
return nil, errors.Wrap(err, "error retrieving JumpCloud PUSH payload")
}
jp := JumpCloudPushResponse{}
if err := json.Unmarshal(jpResp, &jp); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal JumpCloud PUSH payload to struct")
}
jumpCloudParsedURL.Path = path.Join(jumpCloudParsedURL.Path, jp.ID)
req, err = http.NewRequest("GET", jumpCloudParsedURL.String(), nil)
ensureHeaders(xsrfToken, req)
if err != nil {
return nil, errors.Wrap(err, "failed to build JumpCoud PUSH polling request")
}
// Stay in the loop until we get something else other than "pending".
// jp.Status can be:
// * accepted
// * expired
// * denied
for jp.Status == "pending" {
if time.Now().UTC().After(jp.ExpiresAt) {
return nil, errors.New("the session is expired try again")
}
resp, err := jc.client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "error retrieving verify response")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.New(fmt.Sprintf("received non 200 http code, http code = %d", resp.StatusCode))
}
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal JumpCloud PUSH body")
}
if err := json.Unmarshal(bytes, &jp); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal poll result json into struct")
}
// sleep for 500ms before next request
time.Sleep(500 * time.Millisecond)
}
if jp.Status != "accepted" {
return nil, errors.New(fmt.Sprintf("didn't receive accepted, status=%s", jp.Status))
}
jumpCloudParsedURL.Path = path.Join(jumpCloudParsedURL.Path, "login")
req, err = http.NewRequest("POST", jumpCloudParsedURL.String(), emptyJSONIOReader())
if err != nil {
return nil, errors.Wrap(err, "failed to build JumpCoud login request")
}
ensureHeaders(xsrfToken, req)
return jc.client.Do(req)
}