forked from requilence/integram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.go
248 lines (204 loc) · 6.78 KB
/
oauth.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package integram
import (
"fmt"
"net/http"
"strings"
"time"
"golang.org/x/oauth2"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type OAuthTokenSource struct {
user *User
last oauth2.Token
}
func (tsw *OAuthTokenSource) Token() (*oauth2.Token, error) {
lastToken := tsw.last
ts := tsw.user.ctx.OAuthProvider().OAuth2Client(tsw.user.ctx).TokenSource(oauth2.NoContext, &lastToken)
token, err := ts.Token()
if err != nil {
if strings.Contains(err.Error(), "revoked") || strings.Contains(err.Error(), "invalid_grant") {
_ = tsw.user.saveProtectedSetting("OAuthValid", false)
//todo: provide revoked callback
}
tsw.user.ctx.Log().Errorf("OAuth token refresh failed, token OAuthValid set to false: %s", err.Error())
return nil, err
}
if token != nil {
if token.AccessToken != lastToken.AccessToken || !token.Expiry.Equal(lastToken.Expiry) {
ps, _ := tsw.user.protectedSettings()
if ps != nil && !ps.OAuthValid {
ps.OAuthValid = true
_ = tsw.user.saveProtectedSetting("OAuthValid", true)
}
err = oauthTokenStore.SetOAuthAccessToken(tsw.user, token.AccessToken, &token.Expiry)
if err != nil {
tsw.user.ctx.Log().Errorf("failed to set OAuth Access token in store: %s", err.Error())
}
}
if token.RefreshToken != "" && token.RefreshToken != lastToken.RefreshToken {
err = oauthTokenStore.SetOAuthRefreshToken(tsw.user, token.RefreshToken)
if err != nil {
tsw.user.ctx.Log().Errorf("failed to set OAuth Access token in store: %s", err.Error())
}
}
}
return token, nil
}
// OAuthHTTPClient returns HTTP client with Bearer authorization headers
func (user *User) OAuthHTTPClient() *http.Client {
if user.ctx.Service().DefaultOAuth2 != nil {
ts, err := user.OAuthTokenSource()
if err != nil {
user.ctx.Log().Errorf("OAuthTokenSource got error: %s", err.Error())
return nil
}
return oauth2.NewClient(oauth2.NoContext, ts)
} else if user.ctx.Service().DefaultOAuth1 != nil {
//todo make a correct httpclient
return http.DefaultClient
}
return nil
}
// OAuthValid checks if OAuthToken for service is set
func (user *User) OAuthValid() bool {
token, _, _ := oauthTokenStore.GetOAuthAccessToken(user)
return token != ""
}
// OAuthTokenStore returns current OAuthTokenStore name used to get/set access and refresh tokens
func (user *User) OAuthTokenStore() string {
ps, _ := user.protectedSettings()
if ps == nil {
return ""
}
return ps.OAuthStore
}
// SetOAuthTokenStore stores the new name for OAuth Store to get/set access and refresh tokens
func (user *User) SetOAuthTokenStore(storeName string) error {
return user.saveProtectedSetting("OAuthStore", storeName)
}
// OAuthTokenSource returns OAuthTokenSource to use within http client to get OAuthToken
func (user *User) OAuthTokenSource() (oauth2.TokenSource, error) {
if user.ctx.Service().DefaultOAuth2 == nil {
return nil, fmt.Errorf("DefaultOAuth2 config not set for the service")
}
accessToken, expireDate, err := oauthTokenStore.GetOAuthAccessToken(user)
if err != nil {
user.ctx.Log().Errorf("can't create OAuthTokenSource: oauthTokenStore.GetOAuthAccessToken got error: %s", err.Error())
return nil, err
}
refreshToken, err := oauthTokenStore.GetOAuthRefreshToken(user)
if err != nil {
user.ctx.Log().Errorf("can't create OAuthTokenSource: oauthTokenStore.GetOAuthRefreshToken got error: %s", err.Error())
return nil, err
}
otoken := oauth2.Token{AccessToken: accessToken, RefreshToken: refreshToken, TokenType: "Bearer"}
if expireDate != nil {
otoken.Expiry = *expireDate
}
return &OAuthTokenSource{
user: user,
last: otoken,
}, nil
}
// OAuthToken returns OAuthToken for service
func (user *User) OAuthToken() string {
// todo: oauthtoken per host?
/*
host := user.ctx.ServiceBaseURL.Host
if host == "" {
host = user.ctx.Service().DefaultBaseURL.Host
}
*/
ts, err := user.OAuthTokenSource()
if err != nil {
user.ctx.Log().Errorf("OAuthTokenSource got error: %s", err.Error())
return ""
}
token, err := ts.Token()
if err != nil {
user.ctx.Log().Errorf("OAuthToken got tokensource error: %s", err.Error())
return ""
}
return token.AccessToken
}
// ResetOAuthToken reset OAuthToken for service
func (user *User) ResetOAuthToken() error {
err := oauthTokenStore.SetOAuthAccessToken(user, "", nil)
if err != nil {
user.ctx.Log().WithError(err).Error("ResetOAuthToken error")
}
return err
}
// OauthRedirectURL used in OAuth process as returning URL
func (user *User) OauthRedirectURL() string {
providerID := user.ctx.OAuthProvider().internalID()
if providerID == user.ctx.ServiceName {
return fmt.Sprintf("%s/auth/%s", Config.BaseURL, user.ctx.ServiceName)
}
return fmt.Sprintf("%s/auth/%s/%s", Config.BaseURL, user.ctx.ServiceName, providerID)
}
// OauthInitURL used in OAuth process as returning URL
func (user *User) OauthInitURL() string {
authTempToken := user.AuthTempToken()
s := user.ctx.Service()
if authTempToken == "" {
user.ctx.Log().Error("authTempToken is empty")
return ""
}
if s.DefaultOAuth2 != nil {
provider := user.ctx.OAuthProvider()
return provider.OAuth2Client(user.ctx).AuthCodeURL(authTempToken, oauth2.AccessTypeOffline)
}
if s.DefaultOAuth1 != nil {
return fmt.Sprintf("%s/oauth1/%s/%s", Config.BaseURL, s.Name, authTempToken)
}
return ""
}
// AuthTempToken returns Auth token used in OAuth process to associate TG user with OAuth creditianals
func (user *User) AuthTempToken() string {
host := user.ctx.ServiceBaseURL.Host
if host == "" {
host = user.ctx.Service().DefaultBaseURL.Host
}
serviceBaseURL := user.ctx.ServiceBaseURL.String()
if serviceBaseURL == "" {
serviceBaseURL = user.ctx.Service().DefaultBaseURL.String()
}
ps, _ := user.protectedSettings()
cacheTime := user.ctx.Service().DefaultOAuth2.AuthTempTokenCacheTime
if cacheTime == 0 {
cacheTime = time.Hour * 24 * 30
}
if ps.AuthTempToken != "" {
oAuthIDCacheFound := oAuthIDCacheVal{BaseURL: serviceBaseURL}
user.SetCache("auth_"+ps.AuthTempToken, oAuthIDCacheFound, cacheTime)
return ps.AuthTempToken
}
rnd := strings.ToLower(rndStr.Get(16))
user.SetCache("auth_"+rnd, oAuthIDCacheVal{BaseURL: serviceBaseURL}, cacheTime)
err := user.saveProtectedSetting("AuthTempToken", rnd)
if err != nil {
user.ctx.Log().WithError(err).Error("Error saving AuthTempToken")
}
return rnd
}
func findOauthProviderByID(db *mgo.Database, id string) (*OAuthProvider, error) {
oap := OAuthProvider{}
if s, _ := serviceByName(id); s != nil {
return s.DefaultOAuthProvider(), nil
}
err := db.C("oauth_providers").FindId(id).One(&oap)
if err != nil {
return nil, err
}
return &oap, nil
}
func findOauthProviderByHost(db *mgo.Database, host string) (*OAuthProvider, error) {
oap := OAuthProvider{}
err := db.C("oauth_providers").Find(bson.M{"baseurl.host": strings.ToLower(host)}).One(&oap)
if err != nil {
return nil, err
}
return &oap, nil
}