-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdevice_auth.go
48 lines (41 loc) · 1.06 KB
/
device_auth.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
package githubcopilotapi
import (
"context"
"fmt"
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"
)
func DeviceLogin(ctx context.Context, clientID string) (string, error) {
config := &oauth2.Config{
ClientID: clientID,
Endpoint: github.Endpoint,
Scopes: []string{"read:user"},
}
resp, err := config.DeviceAuth(ctx)
if err != nil {
return "", err
}
if resp.VerificationURIComplete != "" {
fmt.Printf("Please visit %s to authenticate.\n", resp.VerificationURIComplete)
} else {
fmt.Printf("Please take this code %q to authenticate at %s.\n", resp.UserCode, resp.VerificationURI)
}
fmt.Println("Press 'y' to continue, or any other key to abort.")
var input string
_, err = fmt.Scanln(&input)
if err != nil {
return "", err
}
if input != "y" && input != "Y" {
return "", fmt.Errorf("aborted")
}
fmt.Println("Authenticating, please wait...")
token, err := config.DeviceAccessToken(ctx, resp)
if err != nil {
return "", err
}
if token.AccessToken != "" {
return token.AccessToken, nil
}
return "", fmt.Errorf("failed to get token")
}