-
Notifications
You must be signed in to change notification settings - Fork 3
/
auth.go
194 lines (178 loc) · 5 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
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
"code.google.com/p/goauth2/oauth"
"code.google.com/p/goauth2/oauth/jwt"
"encoding/gob"
"encoding/json"
"net/http"
)
const tokensFile = "~tokens.gob"
func authStart() {
args := endpointFs.Args()
if len(args) != 3 {
fmt.Println("Invalid arguments, must provide API and method, e.g.:")
fmt.Println(" gapi auth.start <api> <method>")
return
}
api := loadAPI(args[1])
if api == nil || (len(api.Resources) == 0 && len(api.Methods) == 0) {
fmt.Println("Couldn't load API ", args[1])
return
}
m := findMethod(args[2], *api)
if m.Scopes == nil {
fmt.Println("Method doesn't require auth")
return
}
oauthConfig.Scope = strings.Join(m.Scopes, " ")
fmt.Println("Open a browser and visit the following URL:")
fmt.Println(oauthConfig.AuthCodeURL(""))
fmt.Println("Then run the following command with the resulting auth code:")
fmt.Println("gapi auth.finish <code>")
}
func authFinish() {
args := endpointFs.Args()
if len(args) != 2 {
fmt.Println("Invalid arguments, must provide code, e.g.:")
fmt.Println(" gapi auth.finish <code>")
return
}
code := args[1]
t := &oauth.Transport{Config: oauthConfig}
tok, err := t.Exchange(code)
maybeFatal("error exchanging code", err)
toks, err := loadTokens()
maybeFatal("error loading tokens", err)
inf, err := getTokenInfo(tok.AccessToken)
maybeFatal("error getting tokeninfo", err)
toks.Tok[inf.Scope] = token{
AccessToken: tok.AccessToken,
RefreshToken: tok.RefreshToken,
}
err = toks.save()
maybeFatal("error saving tokens", err)
fmt.Println("Token saved")
}
func authPrint() {
args := endpointFs.Args()
if len(args) != 3 {
fmt.Println("Invalid arguments, must provide API and method, e.g.:")
fmt.Println(" gapi auth.print <api> <method>")
return
}
api := loadAPI(args[1])
if api == nil || (len(api.Resources) == 0 && len(api.Methods) == 0) {
log.Fatal("Couldn't load API ", args[1])
}
m := findMethod(args[2], *api)
toks, err := loadTokens()
maybeFatal("error loading tokens", err)
if tok, found := toks.Tok[strings.Join(m.Scopes, " ")]; found {
// TODO: If necessary, refresh, store, and print the new token.
fmt.Println(tok.AccessToken)
} else {
fmt.Println("No token found. Run the following command to store a token:")
fmt.Println("gapi auth.start", api.Name, m.ID[len(api.Name)+1:])
}
}
func authRevoke() {
args := endpointFs.Args()
if len(args) != 3 {
fmt.Println("Invalid arguments, must provide API and method, e.g.:")
fmt.Println(" gapi auth.print <api> <method>")
return
}
api := loadAPI(args[1])
if api == nil || (len(api.Resources) == 0 && len(api.Methods) == 0) {
log.Fatal("Couldn't load API ", args[1])
}
m := findMethod(args[2], *api)
toks, err := loadTokens()
maybeFatal("error loading tokens", err)
if tok, found := toks.Tok[strings.Join(m.Scopes, " ")]; found {
_, err := http.Get("https://accounts.google.com/o/oauth2/revoke?token=" + tok.AccessToken)
maybeFatal("error revoking token", err)
delete(toks.Tok, strings.Join(m.Scopes, " "))
toks.save()
}
}
type tokenInfo struct {
Scope string
ExpiresIn int `json:"expires_in"`
AccessType string `json:"access_type"`
}
func (inf tokenInfo) expired() bool {
return inf.ExpiresIn < 0
}
func getTokenInfo(tok string) (*tokenInfo, error) {
r, err := http.Post("https://www.googleapis.com/oauth2/v2/tokeninfo?access_token="+tok, "", nil)
if err != nil {
return nil, err
}
defer r.Body.Close()
var info tokenInfo
err = json.NewDecoder(r.Body).Decode(&info)
return &info, err
}
type tokens struct {
Tok map[string]token
}
type token struct {
AccessToken, RefreshToken string
}
func loadTokens() (*tokens, error) {
fi, err := os.Stat(tokensFile)
if err == os.ErrNotExist || err == io.EOF {
return &tokens{make(map[string]token)}, nil
} else if err != nil {
return &tokens{make(map[string]token)}, nil
}
if fi.Size() == 0 {
return &tokens{make(map[string]token)}, nil
}
f, err := os.Open(tokensFile)
if err != nil && err != io.EOF {
return &tokens{make(map[string]token)}, nil
}
defer f.Close()
var t tokens
_ = gob.NewDecoder(f).Decode(&t)
return &t, nil
}
func (t *tokens) save() error {
f, err := os.Create(tokensFile)
if err != nil {
return err
}
defer f.Close()
return gob.NewEncoder(f).Encode(t)
}
func accessTokenFromPemFile(scope string) string {
secretBytes, err := ioutil.ReadFile(*flagSecrets)
maybeFatal("error reading secrets file:", err)
var config struct {
Web struct {
ClientEmail string `json:"client_email"`
TokenURI string `json:"token_uri"`
}
}
err = json.Unmarshal(secretBytes, &config)
maybeFatal("error unmarshalling secrets:", err)
keyBytes, err := ioutil.ReadFile(*flagPem)
maybeFatal("error reading private key file:", err)
// Craft the ClaimSet and JWT token.
t := jwt.NewToken(config.Web.ClientEmail, scope, keyBytes)
t.ClaimSet.Aud = config.Web.TokenURI
// We need to provide a client.
c := &http.Client{}
// Get the access token.
o, err := t.Assert(c)
maybeFatal("assertion error:", err)
return o.AccessToken
}