-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
221 lines (190 loc) · 5.54 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package auth
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"runtime"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
var (
credential string = "client_secret.json"
cacheFile string = "youtube.token.json"
errGetUser error = errors.New("unable to get current user")
errCreateSvc error = errors.New("unable to create YouTube service")
errReadPrompt error = errors.New("unable to read prompt")
errExchange error = errors.New("unable retrieve token from web or prompt")
errStartWeb error = errors.New("unable to start web server")
errCacheToken error = errors.New("unable to cache token")
errReadSecret error = errors.New("unable to read client secret file")
errParseSecret error = errors.New("unable to parse client secret to config")
)
const missingClientSecretsMessage string = `
Please configure OAuth 2.0
To make this sample run, you need to populate the client_secrets.json file
found at:
%v
with information from the {{ Google Cloud Console }}
{{ https://cloud.google.com/console }}
For more information about the client_secrets.json file format, please visit:
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
`
func NewY2BService() *youtube.Service {
ctx := context.Background()
scope := []string{
youtube.YoutubeScope,
youtube.YoutubeForceSslScope,
youtube.YoutubeChannelMembershipsCreatorScope,
}
client := getClient(ctx, scope...)
service, err := youtube.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Fatalln(errors.Join(errCreateSvc, err))
}
return service
}
func getClient(ctx context.Context, scope ...string) *http.Client {
config := getConfig(scope...)
token, err := tokenFromFile(cacheFile)
if err != nil {
return newClient(ctx, config, cacheFile)
} else if !token.Valid() {
tokenSource := config.TokenSource(ctx, token)
token, err = tokenSource.Token()
if token != nil {
saveToken(cacheFile, token)
}
if err != nil {
return newClient(ctx, config, cacheFile)
}
}
return config.Client(ctx, token)
}
func getConfig(scope ...string) *oauth2.Config {
cred, err := os.ReadFile(credential)
if err != nil {
fmt.Printf(missingClientSecretsMessage, credential)
log.Fatalln(errors.Join(errReadSecret, err))
}
config, err := google.ConfigFromJSON(cred, scope...)
if err != nil {
log.Fatalln(errors.Join(errParseSecret, err))
}
return config
}
func newClient(ctx context.Context, config *oauth2.Config, cacheFile string) *http.Client {
var token *oauth2.Token
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
token = getTokenFromWeb(config, authURL)
saveToken(cacheFile, token)
return config.Client(ctx, token)
}
func startWebServer(redirectURL string) (codeCh chan string, err error) {
u, err := url.Parse(redirectURL)
if err != nil {
return nil, err
}
listener, err := net.Listen("tcp", u.Host)
if err != nil {
return nil, err
}
codeCh = make(chan string)
go http.Serve(
listener, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
code := r.FormValue("code")
codeCh <- code
listener.Close()
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(
w, "Received code: %v\r\nYou can now safely close this window.", code,
)
},
),
)
return codeCh, err
}
func openURL(url string) error {
var err error
switch runtime.GOOS {
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "linux":
err = exec.Command("xdg-open", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("cannot open URL %s on this platform", url)
}
return err
}
func getCodeFromPrompt(authURL string) string {
var code string
fmt.Printf(
"It seems that your browser is not open. Go to the following "+
"link in your browser:\n%v\n", authURL,
)
fmt.Print(
"After completing the authorization flow, enter the authorization " +
"code on command line. \nIf you end up in an error page after completing " +
"the authorization flow, and the url in the address bar is in the form of " +
"\n'localhost:8216/?state=DONOT-COPY&code=COPY-THIS&scope=DONOT-COPY'\n" +
"ONLY 'COPY-THIS' is the code you need to enter on command line.\n",
)
_, err := fmt.Scan(&code)
if err != nil {
log.Fatalln(errors.Join(errReadPrompt, err))
}
return code
}
func getTokenFromWeb(config *oauth2.Config, authURL string) *oauth2.Token {
codeCh, err := startWebServer(config.RedirectURL)
if err != nil {
log.Fatalln(errors.Join(errStartWeb, err))
}
var code string
if err := openURL(authURL); err == nil {
fmt.Printf(
"Your browser has been opened to an authorization URL. This "+
"program will resume once authorization has been provided.\n%v\n",
authURL,
)
code = <-codeCh
}
if code == "" {
code = getCodeFromPrompt(authURL)
}
fmt.Printf("Authorization code generated: %v\n", code)
token, err := config.Exchange(context.TODO(), code)
if err != nil {
log.Fatalln(errors.Join(errExchange, err))
}
return token
}
func tokenFromFile(file string) (*oauth2.Token, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
t := &oauth2.Token{}
err = json.NewDecoder(f).Decode(t)
defer f.Close()
return t, err
}
func saveToken(file string, token *oauth2.Token) {
f, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Fatalln(errors.Join(errCacheToken, err))
}
defer f.Close()
json.NewEncoder(f).Encode(token)
}