forked from requilence/integram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth_token_store.go
151 lines (117 loc) · 3.67 KB
/
oauth_token_store.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
package integram
import (
"fmt"
"time"
"gopkg.in/mgo.v2/bson"
)
type OAuthTokenStore interface {
Name() string
GetOAuthAccessToken(user *User) (token string, expireDate *time.Time, err error)
SetOAuthAccessToken(user *User, token string, expireDate *time.Time) error
GetOAuthRefreshToken(user *User) (string, error)
SetOAuthRefreshToken(user *User, token string) error
}
type DefaultOAuthTokenMongoStore struct {
}
var oauthTokenStore OAuthTokenStore = &DefaultOAuthTokenMongoStore{}
func SetOAuthTokenStore(store OAuthTokenStore) {
oauthTokenStore = store
}
func MigrateOAuthFromTo(c *Context, oldTS OAuthTokenStore, newTS OAuthTokenStore, onlyValid bool) (total int, migrated int, expired int, err error) {
keyPrefix := "protected." + c.ServiceName
query := bson.M{
keyPrefix + ".oauthstore": oldTS.Name(),
}
if onlyValid {
query[keyPrefix+".oauthvalid"] = true
}
users, err := c.FindUsers(query)
if err != nil {
return
}
total = len(users)
expiredOlderThan := time.Now().Add((-1) * time.Hour * 24 * 30)
for i, userData := range users {
ctxCopy := *userData.ctx
ctxCopy.User = userData.User
ctxCopy.User.ctx = &ctxCopy
ctxCopy.Chat = Chat{ID: userData.ID, ctx: &ctxCopy}
userData.ctx = &ctxCopy
user := userData.User
user.data = &userData
if i%100 == 0 {
fmt.Printf("MigrateOAuthFromTo: %d/%d users transfered\n", i, len(users))
}
token, expiry, err := oldTS.GetOAuthAccessToken(&user)
if err != nil {
c.Log().Errorf("MigrateOAuthFromTo got error on GetOAuthAccessToken: %s", err.Error())
continue
}
if onlyValid && token == "" {
expired++
continue
}
if onlyValid && expiry != nil && expiry.Before(expiredOlderThan) {
expired++
continue
}
err = newTS.SetOAuthAccessToken(&user, token, expiry)
if err != nil {
c.Log().Errorf("MigrateOAuthFromTo got error on SetOAuthAccessToken: %s", err.Error())
continue
}
refreshToken, err := oldTS.GetOAuthRefreshToken(&user)
if err != nil {
c.Log().Errorf("MigrateOAuthFromTo got error on GetOAuthRefreshToken: %s", err.Error())
continue
}
err = newTS.SetOAuthRefreshToken(&user, refreshToken)
if err != nil {
c.Log().Errorf("MigrateOAuthFromTo got error on SetOAuthRefreshToken: %s", err.Error())
continue
}
err = c.db.C("users").UpdateId(user.ID, bson.M{"$set": bson.M{keyPrefix + ".oauthstore": newTS.Name(), keyPrefix + ".oauthvalid": true}})
if err != nil {
c.Log().Errorf("MigrateOAuthFromTo got error: %s", err.Error())
continue
}
migrated++
}
fmt.Printf("MigrateOAuthFromTo: %d/%d users transfered\n", len(users), len(users))
return
}
func (d *DefaultOAuthTokenMongoStore) GetOAuthAccessToken(user *User) (token string, expireDate *time.Time, err error) {
ps, err := user.protectedSettings()
if err != nil {
return "", nil, err
}
return ps.OAuthToken, ps.OAuthExpireDate, nil
}
func (d *DefaultOAuthTokenMongoStore) GetOAuthRefreshToken(user *User) (string, error) {
ps, err := user.protectedSettings()
if err != nil {
return "", err
}
return ps.OAuthRefreshToken, nil
}
func (d *DefaultOAuthTokenMongoStore) SetOAuthAccessToken(user *User, token string, expireDate *time.Time) error {
ps, err := user.protectedSettings()
if err != nil {
return err
}
ps.OAuthStore = d.Name()
ps.OAuthToken = token
ps.OAuthExpireDate = expireDate
return user.saveProtectedSettings()
}
func (d *DefaultOAuthTokenMongoStore) SetOAuthRefreshToken(user *User, refreshToken string) error {
ps, err := user.protectedSettings()
if err != nil {
return err
}
ps.OAuthRefreshToken = refreshToken
return user.saveProtectedSetting("OAuthRefreshToken", refreshToken)
}
func (d *DefaultOAuthTokenMongoStore) Name() string {
return "default"
}