Repository navigation
Expand file tree
/
Copy pathoauth.go
More file actions
110 lines (100 loc) · 4.33 KB
/
Copy pathoauth.go
File metadata and controls
110 lines (100 loc) · 4.33 KB
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
package main
import (
"context"
"errors"
"strings"
internetdata "github.com/internetdata/sdk-go/v2"
)
// Browser-based sign-in, via the OAuth 2.0 device authorization grant
// (RFC 8628), through the SDK's client.Oauth.
//
// WHY THE DEVICE FLOW AND NOT A LOOPBACK REDIRECT: the other option for a
// command-line tool is to open a browser at a redirect pointing back to
// 127.0.0.1 on a port this process is listening on (RFC 8252). That is slightly
// nicer on a desktop and useless everywhere else - over SSH, in a container, on
// a build agent - which is a large share of where this tool actually runs.
// Shipping both would mean two flows to keep correct and one that fails exactly
// where it is hardest to debug. The device flow still opens a browser when there
// is one, so the desktop case loses almost nothing.
//
// What comes back is an ordinary API KEY, which is what every later request
// uses. The OAuth tokens are kept only so `logout` can revoke server-side
// rather than just deleting a local file.
// clientID identifies this program to the authorization server. Public,
// hardcoded and not a secret: it ships in a binary anyone can read, and a person
// still approves every sign-in in the browser.
const clientID = "internetdata-cli"
// The scopes login asks for. Narrow on purpose: everything here is read-only
// except for the one act of handing over a key the user themselves selects.
// Notably absent is anything that could CREATE a credential.
const loginScopes = "account.read apikeys.read apikeys.reveal"
// authBaseURL is the API base URL the sign-in client is built with, which is
// not where the authorization server lives: that is mounted on the APEX, and the
// SDK's client.Oauth derives it from this, `api.` dropped and `api-<env>.`
// becoming `<env>.`, and any other host used as given. So the derivation is the
// SDK's alone, and a session pointed at another deployment authenticates
// against that deployment without this program rewriting a host of its own.
func authBaseURL() string {
if base := gConfig.ResolveBaseURL(); base != "" {
return strings.TrimSuffix(base, "/")
}
return internetdata.DefaultBaseURL
}
// startDeviceAuth asks the authorization server to begin a flow.
func startDeviceAuth(ctx context.Context) (*internetdata.DeviceAuthorization, error) {
oauth, err := signInAPI()
if err != nil {
return nil, err
}
dev, err := oauth.DeviceAuthorization(ctx, clientID,
internetdata.DeviceAuthorizationOptions{Scope: loginScopes})
var refused *internetdata.OauthError
if errors.As(err, &refused) && refused.ErrorCode == "slow_down" {
return nil, errors.New("too many sign-in attempts from this address; wait a minute and try again")
}
return dev, err
}
// pollForToken waits for the human to approve, then returns the tokens. The SDK
// keeps the server's interval, widened for good by each slow_down.
func pollForToken(
ctx context.Context, dev *internetdata.DeviceAuthorization,
) (*internetdata.TokenResponse, error) {
oauth, err := signInAPI()
if err != nil {
return nil, err
}
tok, err := oauth.PollDeviceToken(ctx, clientID, dev)
switch {
case errors.Is(err, internetdata.ErrOauthAccessDenied):
return nil, errors.New("the request was denied in the browser")
case errors.Is(err, internetdata.ErrOauthExpiredToken):
return nil, errors.New("the code expired before it was approved; run the command again")
}
return tok, err
}
// revokeToken tells the server to forget a credential.
//
// Best-effort by design, and the caller ignores the error: `logout` must always
// succeed locally. A machine that is offline, or whose token has already
// expired, still needs its stored credential gone - refusing to log out because
// the network is down would be the wrong answer to "remove this from my laptop".
func revokeToken(ctx context.Context, token string) error {
oauth, err := signInAPI()
if err != nil {
return err
}
return oauth.Revoke(ctx, clientID, token)
}
// signInAPI is a keyless client aimed at this session's deployment. The SDK
// keeps any key off these requests regardless; there is simply none to give.
func signInAPI() (*internetdata.OauthAPI, error) {
client, err := internetdata.New(
internetdata.WithHTTPClient(namedHTTPClient()),
internetdata.WithBaseURL(authBaseURL()),
internetdata.WithRetries(resolveRetries()),
)
if err != nil {
return nil, err
}
return client.Oauth, nil
}