-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
78 lines (65 loc) · 1.91 KB
/
auth.go
File metadata and controls
78 lines (65 loc) · 1.91 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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/gmail/v1"
)
func getOAuth2Client(ctx context.Context, credPath, tokPath string) (*http.Client, error) {
b, err := os.ReadFile(credPath)
if err != nil {
return nil, fmt.Errorf("reading credentials.json: %w\nDownload it from https://console.cloud.google.com/apis/credentials", err)
}
config, err := google.ConfigFromJSON(b, gmail.GmailReadonlyScope)
if err != nil {
return nil, fmt.Errorf("parsing credentials.json: %w", err)
}
tok, err := loadToken(tokPath)
if err != nil {
tok, err = getTokenFromWeb(ctx, config)
if err != nil {
return nil, err
}
if err := saveToken(tokPath, tok); err != nil {
return nil, err
}
}
return config.Client(ctx, tok), nil
}
func loadToken(path string) (*oauth2.Token, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer func() { _ = f.Close() }()
var tok oauth2.Token
if err := json.NewDecoder(f).Decode(&tok); err != nil {
return nil, err
}
return &tok, nil
}
func getTokenFromWeb(ctx context.Context, config *oauth2.Config) (*oauth2.Token, error) {
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("Open the following URL in your browser and authorize the application:\n\n%s\n\nPaste the authorization code here: ", authURL)
var code string
if _, err := fmt.Scan(&code); err != nil {
return nil, fmt.Errorf("reading authorization code: %w", err)
}
tok, err := config.Exchange(ctx, code)
if err != nil {
return nil, fmt.Errorf("exchanging authorization code: %w", err)
}
return tok, nil
}
func saveToken(path string, tok *oauth2.Token) error {
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
return fmt.Errorf("saving token: %w", err)
}
defer func() { _ = f.Close() }()
return json.NewEncoder(f).Encode(tok)
}