This repository was archived by the owner on Nov 16, 2022. It is now read-only.
forked from googlecodelabs/tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.go
202 lines (183 loc) · 5.01 KB
/
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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2016 Google Inc. All Rights Reserved.
//
// 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 main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"sync"
"golang.org/x/net/context"
"golang.org/x/oauth2"
)
const (
// auth scopes needed by the program
scopeDriveReadOnly = "https://www.googleapis.com/auth/drive.readonly"
// program credentials for installed apps
googClient = "183908478743-e8rth9fbo7juk9eeivgp23asnt791g63.apps.googleusercontent.com"
googSecret = "ljELuf5jUrzcOxZGL7OQfkIC"
// token providers
providerGoogle = "goog"
)
var (
// OAuth2 configs for OOB flow
authConfig = map[string]*oauth2.Config{
providerGoogle: {
ClientID: googClient,
ClientSecret: googSecret,
Scopes: []string{scopeDriveReadOnly},
RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
Endpoint: oauth2.Endpoint{
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
},
},
}
// reusable HTTP clients
clientsMu sync.Mutex // guards clients
clients map[string]*http.Client
)
func init() {
clients = make(map[string]*http.Client)
}
// driveClient returns an HTTP client which knows how to perform authenticated
// requests to Google Drive API.
func driveClient() (*http.Client, error) {
clientsMu.Lock()
defer clientsMu.Unlock()
if hc, ok := clients[providerGoogle]; ok {
return hc, nil
}
ts, err := tokenSource(providerGoogle)
if err != nil {
return nil, err
}
t := &oauth2.Transport{
Source: ts,
Base: http.DefaultTransport,
}
hc := &http.Client{Transport: t}
clients[providerGoogle] = hc
return hc, nil
}
// tokenSource creates a new oauth2.TokenSource backed by tokenRefresher,
// using previously stored user credentials if available.
func tokenSource(provider string) (oauth2.TokenSource, error) {
// Ignore provider if *authToken is given.
if *authToken != "" {
tok := &oauth2.Token{AccessToken: *authToken}
return oauth2.StaticTokenSource(tok), nil
}
conf := authConfig[provider]
if conf == nil {
return nil, fmt.Errorf("no auth config for %q", provider)
}
t, err := readToken(provider)
if err != nil {
t, err = authorize(conf)
}
if err != nil {
return nil, fmt.Errorf("unable to obtain access token for %q", provider)
}
cache := &cachedTokenSource{
src: conf.TokenSource(context.Background(), t),
provider: provider,
config: conf,
}
return oauth2.ReuseTokenSource(nil, cache), nil
}
// authorize performs user authorization flow, asking for permissions grant.
func authorize(conf *oauth2.Config) (*oauth2.Token, error) {
aurl := conf.AuthCodeURL("unused", oauth2.AccessTypeOffline)
fmt.Printf("Authorize me at following URL, please:\n\n%s\n\nCode: ", aurl)
var code string
if _, err := fmt.Scan(&code); err != nil {
return nil, err
}
return conf.Exchange(context.Background(), code)
}
// cachedTokenSource stores tokens returned from src on local disk.
// It is usually combined with oauth2.ReuseTokenSource.
type cachedTokenSource struct {
src oauth2.TokenSource
provider string
config *oauth2.Config
}
func (c *cachedTokenSource) Token() (*oauth2.Token, error) {
t, err := c.src.Token()
if err != nil {
t, err = authorize(c.config)
}
if err != nil {
return nil, err
}
writeToken(c.provider, t)
return t, nil
}
// readToken deserializes token from local disk.
func readToken(provider string) (*oauth2.Token, error) {
l, err := tokenLocation(provider)
if err != nil {
return nil, err
}
b, err := ioutil.ReadFile(l)
if err != nil {
return nil, err
}
t := &oauth2.Token{}
return t, json.Unmarshal(b, t)
}
// writeToken serializes token tok to local disk.
func writeToken(provider string, tok *oauth2.Token) error {
l, err := tokenLocation(provider)
if err != nil {
return err
}
w, err := os.Create(l)
if err != nil {
return err
}
defer w.Close()
b, err := json.MarshalIndent(tok, "", " ")
if err != nil {
return err
}
_, err = w.Write(b)
return err
}
// tokenLocation returns a local file path, suitable for storing user credentials.
func tokenLocation(provider string) (string, error) {
d := homedir()
if d == "" {
log.Printf("WARNING: unable to identify user home dir")
}
d = path.Join(d, ".config", "claat")
if err := os.MkdirAll(d, 0700); err != nil {
return "", err
}
return path.Join(d, provider+"-cred.json"), nil
}
func homedir() string {
if v := os.Getenv("HOME"); v != "" {
return v
}
d, p := os.Getenv("HOMEDRIVE"), os.Getenv("HOMEPATH")
if d != "" && p != "" {
return d + p
}
return os.Getenv("USERPROFILE")
}