-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_any_account.go
228 lines (184 loc) · 5.41 KB
/
rest_any_account.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
package main
import (
"github.com/gin-gonic/gin"
ua "github.com/mileusna/useragent"
"github.com/pkg/errors"
"knowlgraph.com/ent"
)
// Terminal is user's device Terminal
type Terminal struct {
Code string
Name string
UA string
Cred *ent.Credential
ClientState string // 一个不可猜测的随机字符串。它用于防止跨站点请求伪造攻击。
State int
UserID int
OnlyOnce bool // 是否仅此一次授权
}
func (t *Terminal) authorized() bool {
return t.State == TokenStateAuthorized
}
func getChallenge(c *Context) error {
state := c.Query("state")
challenge := New16BitID()
t := Terminal{
State: TokenStateIdle,
ClientState: state,
}
if userID, ok := c.Get(GinKeyUserID); ok {
t.UserID = userID.(int)
} else {
t.UA = c.GetHeader(HeaderUserAgent)
t.Name, _ = getTerminalName(c.Context)
}
if err := SetV2Redis(RChallenge(challenge), &t, ExpireTimeChallenge); err != nil {
return c.InternalServerError(err.Error())
}
return c.Ok(challenge)
}
// 如果已有账号,则该凭据关联账号,若无,则创建一个新账号
// func makeCredential(c *Context) error {
// var form struct {
// State string `form:"state" binding:"required"`
// Challenge string `form:"challenge" binding:"required"`
// }
// if err := c.ShouldBindQuery(&form); err != nil {
// return c.BadRequest(err.Error())
// }
// var t Terminal
// if err := GetV4Redis(RChallenge(form.Challenge), &t); err != nil {
// return c.BadRequest(err.Error())
// }
// if form.State != t.ClientState {
// return c.BadRequest("Bad state")
// }
// if 0 != t.UserID && !t.authorized() {
// // 如果已绑定认证账号,但认证账号未授权,则返回“没有权限”
// return c.Unauthorized("Permission denied")
// }
// id := 0
// token := New64BitID()
// d := ExpireTimeToken
// if t.OnlyOnce {
// d = ExpireTimeTokenOnce
// }
// terminalMap := make(map[int]string)
// err := WithTx(ctx, client, func(tx *ent.Tx) error {
// if 0 == t.UserID {
// user, err1 := tx.User.Create().Save(ctx)
// if err1 != nil {
// return err1
// }
// t.UserID = user.ID
// } else {
// GetV4Redis(RUser(t.UserID), &terminalMap)
// }
// terminal, err1 := tx.Terminal.
// Create().
// SetCode(New16bitID()).SetName(t.Name).SetUa(t.UA).SetUserID(t.UserID).
// Save(ctx)
// if err1 != nil {
// return err1
// }
// id = terminal.ID
// terminalMap[id] = token
// _, err1 = rdb.Pipelined(ctx, func(pipe redis.Pipeliner) error {
// if err1 = SetV2RedisPipe(pipe, RUser(t.UserID), &terminalMap, d); err1 != nil {
// return err1
// }
// pipe.Set(ctx, RToken(token), t.UserID, d)
// pipe.Del(ctx, RChallenge(form.Challenge))
// return nil
// })
// return err1
// })
// if err != nil {
// return c.InternalServerError(err.Error())
// }
// return c.Ok(gin.H{"id": id, "token": token, "onlyOnce": t.OnlyOnce})
// }
func activateTerminal(c *Context) error {
challenge := c.Query("challenge")
var t Terminal
if err := GetV4Redis(RChallenge(challenge), &t); err != nil {
return c.BadRequest(err.Error())
}
if userID, ok := c.Get(GinKeyUserID); ok {
if 0 != t.UserID {
// 认证用户不得激活已关联认证用户的 challenge
return c.Unauthorized("Invalid user")
}
t.UserID = userID.(int)
} else if 0 == t.UserID {
return c.Unauthorized("Invalid user")
} else {
t.UA = c.GetHeader(HeaderUserAgent)
t.Name, _ = getTerminalName(c.Context)
}
t.State = TokenStateActivated
if err := SetV2Redis(RChallenge(challenge), &t, ExpireTimeChallengeConfirm); err != nil {
return c.InternalServerError(err.Error())
}
return c.Ok(gin.H{"name": t.Name, "state": t.State})
}
func authorizeTerminal(c *Context) error {
var form struct {
Challenge string `form:"challenge" binding:"required"`
OnlyOnce bool `form:"onlyOnce"`
}
if err := c.ShouldBindQuery(&form); err != nil {
return c.BadRequest(err.Error())
}
var t Terminal
if err := GetV4Redis(RChallenge(form.Challenge), &t); err != nil {
return c.BadRequest(err.Error())
}
if t.State < TokenStateActivated {
return c.PreconditionFailed("Please activate the terminal first")
}
if userID, _ := c.Get(GinKeyUserID); userID != t.UserID {
return c.Unauthorized("Invalid user")
}
t.OnlyOnce = form.OnlyOnce
t.State = TokenStateAuthorized
if err := SetV2Redis(RChallenge(form.Challenge), &t, ExpireTimeChallengeConfirm); err != nil {
return c.InternalServerError(err.Error())
}
return c.Ok(gin.H{"name": t.Name, "state": t.State})
}
func cancelActivateTerminal(c *Context) error {
challenge := c.Query("challenge")
if err := rdb.Del(ctx, RChallenge(challenge)).Err(); err != nil {
return c.InternalServerError(err.Error())
}
return c.Ok(true)
}
func scanChallenge(c *Context) error {
challenge := c.Query("challenge")
var t Terminal
if err := GetV4Redis(RChallenge(challenge), &t); err != nil {
return c.BadRequest(err.Error())
}
return c.Ok(gin.H{"name": t.Name, "state": t.State, "onlyOnce": t.OnlyOnce})
}
func getTerminalName(c *gin.Context) (string, error) {
ua := ua.Parse(c.GetHeader("User-Agent"))
if 0 == len(ua.OS) {
return "Unknown", errors.New("Invalid User-Agent")
}
name := ua.Device
if 0 == len(name) {
if ua.Mobile {
name = "Mobile"
} else if ua.Tablet {
name = "Tablet"
} else if ua.Desktop {
name = "Desktop"
} else {
name = "Unknown"
}
name += " " + New4BitNumber()
}
return name, nil
}