-
Notifications
You must be signed in to change notification settings - Fork 2
/
general.go
151 lines (137 loc) · 4.45 KB
/
general.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
/*
Copyright © 2020-2023 Hannes Hayashi
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Package gsmauth provides the authentication mechanisms for Google APIs
package gsmauth
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"cloud.google.com/go/compute/metadata"
"github.com/hanneshayashi/gsm/gsmconfig"
"github.com/skratchdot/open-golang/open"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/impersonate"
)
var ctx context.Context
// Retrieves a token from a local file.
func tokenFromFile(tokenPath string) (*oauth2.Token, error) {
f, err := os.Open(tokenPath)
if err != nil {
return nil, err
}
defer f.Close()
tok := &oauth2.Token{}
err = json.NewDecoder(f).Decode(tok)
return tok, err
}
// Saves a token to a file path.
func saveToken(path string, token *oauth2.Token) error {
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("Unable to cache OAuth token: %v", err)
}
defer f.Close()
err = json.NewEncoder(f).Encode(token)
if err != nil {
return fmt.Errorf("Unable to save OAuth token: %v", err)
}
return nil
}
// GetClientUser does user-based authentication via OAuth and returns an *http.Client
func GetClientUser(credentials []byte, tokenName string, redirectPort int, scope ...string) (client *http.Client, err error) {
// If modifying these scopes, delete your previously saved token.json.
config, err := google.ConfigFromJSON(credentials, scope...)
config.RedirectURL = fmt.Sprintf("http://127.0.0.1:%d/oauth/callback", redirectPort)
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
tokenPath := fmt.Sprintf("%s/%s", gsmconfig.CfgDir, tokenName)
// 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.
tok, err := tokenFromFile(tokenPath)
if err != nil {
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
srv := &http.Server{Addr: fmt.Sprintf(":%d", redirectPort)}
done := make(chan bool, 1)
http.HandleFunc("/oauth/callback", func(w http.ResponseWriter, r *http.Request) {
queryParts, _ := url.ParseQuery(r.URL.RawQuery)
code := queryParts["code"][0]
tok, err = config.Exchange(ctx, code)
if err != nil {
log.Fatal(err)
}
err = saveToken(tokenPath, tok)
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(w, "You can close this window now")
done <- true
close(done)
})
err = open.Run(authURL)
if err != nil {
log.Fatal(err)
}
go func() {
if <-done {
errShutdown := srv.Shutdown(ctx)
if errShutdown != nil {
log.Fatal(errShutdown)
}
}
}()
err = srv.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}
return config.Client(ctx, tok), nil
}
// GetClientADC returns a client to be used for API services
func GetClientADC(subject, serviceAccountEmail string, scope ...string) (client *http.Client, err error) {
if serviceAccountEmail == "" {
serviceAccountEmail, err = metadata.Email("")
if err != nil {
return nil, fmt.Errorf("Error getting Service Account email: %v", err)
}
}
ts, err := impersonate.CredentialsTokenSource(ctx, impersonate.CredentialsConfig{
TargetPrincipal: serviceAccountEmail,
Scopes: scope,
Subject: subject,
})
if err != nil {
return nil, fmt.Errorf("Error getting token source: %v", err)
}
client = oauth2.NewClient(ctx, ts)
return
}
// GetClient returns a client to be used for API services
func GetClient(subject string, credentials []byte, scope ...string) (client *http.Client, err error) {
config, err := google.JWTConfigFromJSON(credentials, scope...)
if err != nil {
return nil, fmt.Errorf("Error parsing Service Account credential file to config: %v", err)
}
config.Subject = subject
return config.Client(ctx), nil
}
func init() {
ctx = context.Background()
}