-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth2.go
138 lines (119 loc) · 3.16 KB
/
oauth2.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
package gadwords
import (
"database/sql/driver"
"encoding/json"
"io/ioutil"
"time"
"golang.org/x/net/context"
"golang.org/x/oauth2"
)
type AuthConfig struct {
file string `json:"-"`
OAuth2Config *oauth2.Config `json:"oauth2.Config"`
OAuth2Token *oauth2.Token `json:"oauth2.Token"`
tokenSource oauth2.TokenSource `json:"-"`
Auth Auth `json:"gads.Auth"`
}
type OAuthConfigArgs struct {
ClientID string
ClientSecret string
}
type OAuthTokenArgs struct {
AccessToken string
RefreshToken string
}
type Credentials struct {
Config OAuthConfigArgs
Token OAuthTokenArgs
Auth Auth
}
func (w AuthConfig) Value() (driver.Value, error) {
bs, _ := json.Marshal(w)
return string(bs), nil
}
// Scan 数据库
func (w *AuthConfig) Scan(src interface{}) error {
switch src := src.(type) {
case []byte:
json.Unmarshal(src, w)
return nil
case string:
json.Unmarshal([]byte(src), w)
return nil
case nil:
*w = AuthConfig{}
return nil
}
return nil
}
func NewCredentialsFromFile(pathToFile string) (ac AuthConfig, err error) {
ctx := context.TODO()
data, err := ioutil.ReadFile(pathToFile)
if err != nil {
return ac, err
}
if err := json.Unmarshal(data, &ac); err != nil {
return ac, err
}
ac.file = pathToFile
ac.tokenSource = ac.OAuth2Config.TokenSource(ctx, ac.OAuth2Token)
ac.Auth.Client = ac.OAuth2Config.Client(ctx, ac.OAuth2Token)
return ac, err
}
func (ac *AuthConfig) Init() {
ctx := context.TODO()
ac.tokenSource = ac.OAuth2Config.TokenSource(ctx, ac.OAuth2Token)
ac.Auth.Client = ac.OAuth2Config.Client(ctx, ac.OAuth2Token)
}
func NewCredentialsFromParams(creds Credentials) (config AuthConfig, err error) {
var gcfg AuthConfig
expiresAt, _ := time.Parse(time.RFC3339, "2015-07-28T14:51:53.543430418-04:00")
// Create a token
gcfg.OAuth2Token = &oauth2.Token{
AccessToken: creds.Token.AccessToken,
TokenType: "Bearer",
RefreshToken: creds.Token.RefreshToken,
Expiry: expiresAt,
}
ctx := context.TODO()
gcfg.OAuth2Config = &oauth2.Config{
ClientID: creds.Config.ClientID,
ClientSecret: creds.Config.ClientSecret,
Scopes: []string{
"https://adwords.google.com/api/adwords",
},
Endpoint: oauth2.Endpoint{
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
},
}
gcfg.Auth = Auth{
CustomerId: creds.Auth.CustomerId,
DeveloperToken: creds.Auth.DeveloperToken,
Client: gcfg.OAuth2Config.Client(ctx, gcfg.OAuth2Token),
}
return gcfg, nil
}
// Save writes the contents of AuthConfig back to the JSON file it was
// loaded from.
func (c AuthConfig) Save() error {
configData, err := json.MarshalIndent(&c, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(c.file, configData, 0600)
}
// Token implements oauth2.TokenSource interface and store updates to
// config file.
func (c AuthConfig) Token() (token *oauth2.Token, err error) {
// use cached token
if c.OAuth2Token.Valid() {
return c.OAuth2Token, err
}
// get new token from tokens source and store
c.OAuth2Token, err = c.tokenSource.Token()
if err != nil {
return nil, err
}
return token, c.Save()
}