/
login.go
171 lines (150 loc) · 4.79 KB
/
login.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
package auth
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/armory/armory-cli/pkg/util"
"github.com/lestrrat-go/jwx/jwt"
log "github.com/sirupsen/logrus"
"net/http"
"time"
)
type DeviceTokenData struct {
DeviceCode string `json:"device_code"`
UserCode string `json:"user_code"`
VerificationUri string `json:"verification_uri"`
ExpiresIn int `json:"expires_in"`
Interval int `json:"interval"`
VerificationUriComplete string `json:"verification_uri_complete"`
}
type ErrorResponse struct {
Error string `json:"error"`
Description string `json:"error_description"`
}
type SuccessfulResponse struct {
// AccessToken Encoded JWT / Bearer Token
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
// SecondsUtilTokenExpires the number of seconds until the JWT expires, from when it was created by the Auth Server.
// The JWT has the exact expiration date time
SecondsUtilTokenExpires int `json:"expires_in"`
}
var timeout = 5 * time.Second
var httpClient = http.Client{
Timeout: timeout,
}
func GetDeviceCodeFromAuthorizationServer(clientId, scope, audience, authUrl string) (*DeviceTokenData, error) {
requestBody, err := json.Marshal(map[string]string{
"client_id": clientId,
"scope": scope,
"audience": audience,
})
if err != nil {
return nil, errors.New("failed to create request body for Armory authorization server")
}
getDeviceCodeRequest, err := http.NewRequest(
"POST",
authUrl + "/device/code",
bytes.NewBuffer(requestBody),
)
if err != nil {
return nil, errors.New("failed to create request for Armory authorization server")
}
getDeviceCodeRequest.Header.Set("Content-Type", "application/json")
resp, err := httpClient.Do(getDeviceCodeRequest)
if err != nil {
return nil, err
}
dec := json.NewDecoder(resp.Body)
var deviceTokenResponse DeviceTokenData
err = dec.Decode(&deviceTokenResponse)
if err != nil {
return nil, err
}
return &deviceTokenResponse, nil
}
func PollAuthorizationServerForResponse(cliClientId string, authUrl string, deviceTokenResponse *DeviceTokenData, authStartedAt time.Time) (*SuccessfulResponse, error) {
var secondsAfterAuthStartedAtWhenDeviceFlowExpires = deviceTokenResponse.ExpiresIn * 1000 - 5000
deviceFlowExpiresTime := authStartedAt.Add(time.Duration(secondsAfterAuthStartedAtWhenDeviceFlowExpires) * time.Second)
log.Infof("Waiting for user to login")
for {
if time.Now().After(deviceFlowExpiresTime) {
log.Infof("%d", secondsAfterAuthStartedAtWhenDeviceFlowExpires)
log.Infof(authStartedAt.Local().String())
log.Infof(deviceFlowExpiresTime.Local().String())
return nil, errors.New("the device flow request has expired")
}
fmt.Print(".")
time.Sleep(time.Duration(deviceTokenResponse.Interval) * time.Second)
errorResponse, response, err := getAuthToken(
authUrl,
map[string]string{
"client_id": cliClientId,
"device_code": deviceTokenResponse.DeviceCode,
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
})
if err != nil {
return nil, err
}
if response != nil {
return response, nil
}
if errorResponse.Error != "authorization_pending" {
return nil, fmt.Errorf("there was an error polling for user auth. Err: %s, Desc: %s", errorResponse.Error, errorResponse.Description)
}
}
}
func RefreshAuthToken(cliClientId string, authUrl string, refreshToken string, environmentId string) (*SuccessfulResponse, error){
errorResponse, response, err := getAuthToken(
authUrl,
map[string]string{
"client_id": cliClientId,
"refresh_token": refreshToken,
"grant_type": "refresh_token",
"requestedEnvId": environmentId,
})
if err != nil {
return nil, err
}
if response != nil {
return response, nil
}
return nil, fmt.Errorf("there was an error authorizing for the requested environment. Err: %s, Desc: %s", errorResponse.Error, errorResponse.Description)
}
func getAuthToken(authUrl string, body map[string]string) (*ErrorResponse, *SuccessfulResponse, error) {
getAuthTokenRequest := util.NewHttpRequest(
"POST",
authUrl+"/token",
body,
nil,
5)
resp, err := getAuthTokenRequest.Execute()
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
if resp.StatusCode == 200 {
fmt.Print("\n")
var authSuccessfulResponse *SuccessfulResponse
err = dec.Decode(&authSuccessfulResponse)
if err != nil {
return nil, nil, err
}
return nil, authSuccessfulResponse, nil
}
var errorResponse *ErrorResponse
err = dec.Decode(&errorResponse)
if err != nil {
return nil, nil, err
}
return errorResponse, nil, nil
}
func ValidateJwt(encodedJwt string) (jwt.Token, error) {
token, err := jwt.Parse([]byte(encodedJwt), jwt.WithValidate(true))
if err != nil {
return nil, err
}
return token, nil
}