-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
104 lines (97 loc) · 2.85 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/int128/oauth2cli"
"github.com/int128/oauth2cli/oauth2params"
"github.com/pkg/browser"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"golang.org/x/sync/errgroup"
)
func init() {
log.SetFlags(log.Lshortfile | log.Lmicroseconds)
}
type cmdOptions struct {
authURL string
tokenURL string
clientID string
clientSecret string
scopes string
localServerCert string
localServerKey string
}
func main() {
var o cmdOptions
flag.StringVar(&o.authURL, "auth-url", google.Endpoint.AuthURL, "Authorization URL of the endpoint")
flag.StringVar(&o.tokenURL, "token-url", google.Endpoint.TokenURL, "Authorization URL of the endpoint")
flag.StringVar(&o.clientID, "client-id", "", "OAuth Client ID")
flag.StringVar(&o.clientSecret, "client-secret", "", "OAuth Client Secret (optional)")
flag.StringVar(&o.scopes, "scopes", "email", "Scopes to request, comma separated")
flag.StringVar(&o.localServerCert, "local-server-cert", "", "Path to a certificate file for the local server (optional)")
flag.StringVar(&o.localServerKey, "local-server-key", "", "Path to a key file for the local server (optional)")
flag.Parse()
if o.clientID == "" {
log.Printf(`You need to set oauth2 credentials.
Open https://console.cloud.google.com/apis/credentials and create a client.
Then set the following options:`)
flag.PrintDefaults()
os.Exit(1)
return
}
if o.localServerCert != "" {
log.Printf("Using the TLS certificate: %s", o.localServerCert)
}
pkce, err := oauth2params.NewPKCE()
if err != nil {
log.Fatalf("error: %s", err)
}
ready := make(chan string, 1)
defer close(ready)
cfg := oauth2cli.Config{
OAuth2Config: oauth2.Config{
ClientID: o.clientID,
ClientSecret: o.clientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: o.authURL,
TokenURL: o.tokenURL,
},
Scopes: strings.Split(o.scopes, ","),
},
AuthCodeOptions: pkce.AuthCodeOptions(),
TokenRequestOptions: pkce.TokenRequestOptions(),
LocalServerReadyChan: ready,
LocalServerCertFile: o.localServerCert,
LocalServerKeyFile: o.localServerKey,
Logf: log.Printf,
}
ctx := context.Background()
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
select {
case url := <-ready:
log.Printf("Open %s", url)
if err := browser.OpenURL(url); err != nil {
log.Printf("could not open the browser: %s", err)
}
return nil
case <-ctx.Done():
return fmt.Errorf("context done while waiting for authorization: %w", ctx.Err())
}
})
eg.Go(func() error {
token, err := oauth2cli.GetToken(ctx, cfg)
if err != nil {
return fmt.Errorf("could not get a token: %w", err)
}
log.Printf("You got a valid token until %s", token.Expiry)
return nil
})
if err := eg.Wait(); err != nil {
log.Fatalf("authorization error: %s", err)
}
}