This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 230
/
auth_token.go
280 lines (241 loc) · 7.75 KB
/
auth_token.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright 2022 Molecula Corp. (DBA FeatureBase).
// SPDX-License-Identifier: Apache-2.0
package ctl
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
pilosa "github.com/featurebasedb/featurebase/v3"
"github.com/featurebasedb/featurebase/v3/logger"
"github.com/featurebasedb/featurebase/v3/server"
"golang.org/x/oauth2"
)
// AuthTokenCommand represents a command for retrieving an auth-token from a
// FeatureBase node.
type AuthTokenCommand struct { // nolint: maligned
// TLS configuration.
TLS server.TLSConfig
tlsConfig *tls.Config
// Host is the host and port of the FeatureBase node to authenticate with.
Host string `json:"host"`
// Reusable client.
client *pilosa.InternalClient
// Standard input/output.
logDest logger.Logger
}
// Logger returns the command's associated Logger to maintain CommandWithTLSSupport interface compatibility
func (cmd *AuthTokenCommand) Logger() logger.Logger {
return cmd.logDest
}
// NewAuthTokenCommand returns a new instance of AuthTokenCommand.
func NewAuthTokenCommand(logdest logger.Logger) *AuthTokenCommand {
return &AuthTokenCommand{
logDest: logdest,
}
}
type deviceAuthResponse struct {
DeviceCode string `json:"device_code"`
UserCode string `json:"user_code"`
VerificationURI string `json:"verification_uri"`
VerificationURIComplete string `json:"verification_uri_complete"`
ExpiresIn int `json:"expires_in"`
Interval int `json:"interval"`
}
// deviceAuthRequest makes a Device Authorization Request and parses the Device
// Authorization Response as defined in RFC 8628 sections 3.1 and 3.2.
func deviceAuthRequest(cli *http.Client, config oauth2.Config) (rsp *deviceAuthResponse, err error) {
// build the request
req, err := http.NewRequest(
http.MethodPost,
pilosa.ReplaceFirstFromBack(config.Endpoint.AuthURL, "authorize", "devicecode"),
strings.NewReader(
url.Values{
"client_id": {config.ClientID},
"scope": {strings.Join(config.Scopes, " ")},
}.Encode(),
),
)
if err != nil {
return nil, fmt.Errorf("building request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// do the request
dar := &deviceAuthResponse{}
if resp, err := cli.Do(req); err != nil {
return nil, fmt.Errorf("making request with status %d: %v", resp.StatusCode, err)
} else if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
body, _ := io.ReadAll(resp.Body) // ignore the error bc it's ok if there's no body
return nil, fmt.Errorf(
"unsuccessful with status %d: %s",
resp.StatusCode,
strings.TrimSpace(string(body)),
)
} else if err := json.NewDecoder(resp.Body).Decode(dar); err != nil {
return nil, fmt.Errorf("decoding device auth response body: %w", err)
}
return dar, nil
}
// successResponse represents a successful device auth token reponse.
type successResponse struct {
Access string `json:"access_token"`
Refresh string `json:"refresh_token"`
Type string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
}
// errorResponse represents a device auth token error reponse.
type errorResponse struct {
Err error
Error string `json:"error"`
Description string `json:"error_description"`
URI string `json:"error_uri"`
}
// waitResponse represents a device auth token reponse that indicates the client
// should continue polling.
type waitResponse struct {
SlowDown bool
}
// parseResponse parses a device auth grant response from the /token endpoint. It
// returns a successResponse on success, an errorResponse on error, or a
// waitResponse if the authorization is still pending or we are requested to slow
// down.
func parseResponse(resp *http.Response) interface{} {
if s := resp.StatusCode; s >= 200 && s < 300 {
rsp := &successResponse{}
if err := json.NewDecoder(resp.Body).Decode(&rsp); err != nil {
return errorResponse{Err: fmt.Errorf("reading response body: %w", err)}
}
return *rsp
}
rsp := &errorResponse{}
if err := json.NewDecoder(resp.Body).Decode(&rsp); err != nil {
return errorResponse{Err: fmt.Errorf("reading response body: %w", err)}
}
switch rsp.Error {
case "slow_down":
return waitResponse{SlowDown: true}
case "authorization_pending":
return waitResponse{}
default:
rsp.Err = fmt.Errorf("error: %s, description: %s, uri: %s", rsp.Error, rsp.Description, rsp.URI)
return *rsp
}
}
// Run executes the main program execution.
func (cmd *AuthTokenCommand) Run(ctx context.Context) (err error) {
// Parse TLS configuration for node-specific clients.
tls := cmd.TLSConfiguration()
if cmd.tlsConfig, err = server.GetTLSConfig(&tls, cmd.Logger()); err != nil {
return fmt.Errorf("parsing tls config: %w", err)
}
// Create an internal client.
client, err := commandClient(cmd)
if err != nil {
return fmt.Errorf("creating client: %w", err)
}
// Get the OAuth config.
var access, refresh string
config, err := client.OAuthConfig()
if err != nil {
return fmt.Errorf("getting oauth config: %w", err)
}
cli := &http.Client{}
// Make the device authorization request.
dar, err := deviceAuthRequest(cli, config)
if err != nil {
return fmt.Errorf("making device auth request: %w", err)
}
// Prompt the user to visit verification_uri and enter code.
fmt.Print(formatPromptBox(dar.VerificationURI, dar.UserCode))
// Request a token until success or error response, slowing down if requested.
interval := dar.Interval
var exit bool
for !exit {
time.Sleep(time.Duration(interval) * time.Second)
// Make a device access token request to the token endpoint.
req, err := http.NewRequest(
http.MethodPost,
config.Endpoint.TokenURL,
strings.NewReader(
url.Values{
"grant_type": {"urn:ietf:params:oauth:grant-type:device_code"},
"client_id": {config.ClientID},
"device_code": {dar.DeviceCode},
}.Encode(),
),
)
if err != nil {
return fmt.Errorf("building request to %s: %w", config.Endpoint.TokenURL, err)
}
resp, err := cli.Do(req)
if err != nil {
return fmt.Errorf("making request to %s: %w", config.Endpoint.TokenURL, err)
}
output := parseResponse(resp)
switch o := output.(type) {
case successResponse:
access = o.Access
refresh = o.Refresh
exit = true
case errorResponse:
return o.Err
case waitResponse:
if o.SlowDown {
interval += 5
}
}
}
// Convey the response to the user.
fmt.Printf("\nauth-token: %s\n", access)
fmt.Printf("\nrefresh-token: %s\n", refresh)
return nil
}
// TLSHost implements the CommandWithTLSSupport interface.
func (cmd *AuthTokenCommand) TLSHost() string { return cmd.Host }
// TLSConfiguration implements the CommandWithTLSSupport interface.
func (cmd *AuthTokenCommand) TLSConfiguration() server.TLSConfig { return cmd.TLS }
// formatPromptBox makes a nice little prompt box to hold the url and code.
func formatPromptBox(url, code string) string {
top := "+-----------------------------------------------"
buffer := "| "
width := len(top) + 1
if len(url) >= width {
width = len(url) + 5
}
if len(code) >= width {
width = len(code) + 5
}
urlStr := fmt.Sprintf("| %s", url)
for i := len(urlStr); i < width-1; i++ {
urlStr += " "
}
urlStr += "|"
codeStr := fmt.Sprintf("| %s", code)
for i := len(codeStr); i < width-1; i++ {
codeStr += " "
}
codeStr += "|"
for i := len(top); i < width-1; i++ {
top += "-"
buffer += " "
}
top += "+\n"
buffer += "|\n"
pls := "| Please visit:"
for i := len(pls); i < width-1; i++ {
pls += " "
}
pls += "|\n"
and := "| And enter the code:"
for i := len(and); i < width-1; i++ {
and += " "
}
and += "|\n"
return fmt.Sprintf("%s%s%s%s\n%s%s%s\n%s%s", top, buffer, pls, urlStr, buffer, and, codeStr, buffer, top)
}