-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathoauth.go
88 lines (78 loc) · 2.66 KB
/
oauth.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
// Copyright 2024 The OSS Rebuild Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oauth
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/pkg/errors"
"golang.org/x/oauth2"
)
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const tokFile = "/tmp/token.json"
// GetOauthClient retrieves a token, saves the token, then returns the generated client.
func GetOauthClient(ctx context.Context, config *oauth2.Config) (*http.Client, error) {
tok, err := tokenFromFile(tokFile)
if err != nil {
tok, err = promptForWebToken(ctx, config)
if err != nil {
return nil, errors.Wrapf(err, "Failed to fetch token")
}
if err := saveTokenToFile(tokFile, tok); err != nil {
return nil, err
}
}
return config.Client(ctx, tok), nil
}
// promptForWebToken starts a web oauth flow and prompts the user for the returned token.
func promptForWebToken(ctx context.Context, config *oauth2.Config) (*oauth2.Token, error) {
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("Go to the following link in your browser then type the authorization code: \n%v\n", authURL)
var authCode string
if _, err := fmt.Scan(&authCode); err != nil {
return nil, errors.Wrapf(err, "Unable to read authorization code")
}
tok, err := config.Exchange(ctx, authCode)
if err != nil {
return nil, errors.Wrapf(err, "Unable to retrieve token from web")
}
return tok, nil
}
// tokenFromFile retrieves a token from a local file.
func tokenFromFile(file string) (*oauth2.Token, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
tok := &oauth2.Token{}
err = json.NewDecoder(f).Decode(tok)
return tok, err
}
// saveTokenToFile stores an oauth token to a file path.
func saveTokenToFile(path string, token *oauth2.Token) error {
log.Printf("Saving credential file to: %s\n", path)
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return errors.Wrapf(err, "Unable to cache oauth token")
}
defer f.Close()
json.NewEncoder(f).Encode(token)
return nil
}