/
gserviceauth.go
189 lines (169 loc) · 3.88 KB
/
gserviceauth.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
// Copyright 2012 All Rights Reserved.
// Author: dougfritz@gmail.com (Doug Fritz)
/*
For now it can't read p12 files so strip them to just the rsa key with openssl
openssl pkcs12 -in file.p12 -nocerts -out key.pem -nodes
then delete the extra text
*/
package gserviceauth
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
const (
aud = "https://accounts.google.com/o/oauth2/token"
grantType = "urn:ietf:params:oauth:grant-type:jwt-bearer"
)
var (
separator = []byte{'.'}
)
type Auth struct {
Email string
Scope []string
Key *rsa.PrivateKey
token string
refresh chan bool
}
func ReadKey(keyFile string) (*rsa.PrivateKey, error) {
keyPEMBlock, err := ioutil.ReadFile(keyFile)
if err != nil {
return nil, err
}
keyDERBlock, _ := pem.Decode(keyPEMBlock)
if keyDERBlock == nil {
return nil, err
}
key, err := x509.ParsePKCS1PrivateKey(keyDERBlock.Bytes)
if err != nil {
return nil, err
}
return key, nil
}
func b64urlencode(b []byte) []byte {
encoded := []byte(base64.URLEncoding.EncodeToString(b))
var equalIndex = bytes.Index(encoded, []byte{'='})
if equalIndex > -1 {
encoded = encoded[:equalIndex]
}
return encoded
}
func New(email string, scope []string, key *rsa.PrivateKey) (*Auth, error) {
auth := new(Auth)
auth.Email = email
auth.Scope = scope
auth.Key = key
token, err := auth.fetchToken()
if err != nil {
return nil, err
}
auth.token = token
auth.refresh = make(chan bool)
go auth.autoRefresh()
return auth, nil
}
func (auth *Auth) assertion() ([]byte, error) {
header, err := json.Marshal(
map[string]interface{}{
"typ": "JWT",
"alg": "RS256",
})
if err != nil {
return nil, err
}
parts := [3][]byte{}
parts[0] = b64urlencode(header)
now := time.Now()
claims, err := json.Marshal(
map[string]interface{}{
"iss": auth.Email,
"scope": strings.Join(auth.Scope, " "),
"aud": aud,
"exp": now.Add(time.Hour).Unix(),
"iat": now.Unix(),
})
if err != nil {
return nil, err
}
parts[1] = b64urlencode(claims)
sha := sha256.New()
sha.Write(bytes.Join(parts[:2], separator))
signature, err := rsa.SignPKCS1v15(rand.Reader, auth.Key, crypto.SHA256, sha.Sum(nil))
if err != nil {
return nil, err
}
parts[2] = b64urlencode(signature)
return bytes.Join(parts[:], separator), nil
}
type authResp struct {
AccessToken string `json:"access_token,omitempty"`
TokenType string `json:"token_type,omitempty"`
ExpiresIn int `json:"expires_in,omitempty"`
Error string `json:"error,omitempty"`
}
func (auth *Auth) fetchToken() (string, error) {
assertion, err := auth.assertion()
if err != nil {
return "", err
}
values := url.Values{"grant_type": {grantType}, "assertion": {string(assertion)}}
resp, err := http.PostForm(aud, values)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var data authResp
err = json.Unmarshal(body, &data)
if err != nil {
return "", err
}
if len(data.Error) != 0 {
return "", errors.New(data.Error)
}
return data.AccessToken, nil
}
const REFRESH_RATE = time.Minute * 50
func (auth *Auth) autoRefresh() {
var retry = REFRESH_RATE
for {
select {
case <-time.After(retry):
fmt.Printf("Refreshing token\n")
token, err := auth.fetchToken()
if err != nil {
retry = time.Second * 30
fmt.Printf("Refresh token failed: %v\nTrying again in %v\n", err, retry)
} else {
retry = REFRESH_RATE
fmt.Printf("Refresh token success: %v\n", token)
auth.token = token
}
case <-auth.refresh:
retry = time.Second
}
}
}
func (auth *Auth) Refresh() {
auth.refresh <- true
}
// Gets the current token for use, this will autoRefresh so it is valid
func (auth *Auth) Token() string {
return auth.token
}