forked from OpenListTeam/auth
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdropbox.go
More file actions
112 lines (103 loc) · 2.94 KB
/
dropbox.go
File metadata and controls
112 lines (103 loc) · 2.94 KB
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
package auth
import (
"github.com/gin-gonic/gin"
"github.com/twoonefour/alist-auth/common"
"github.com/twoonefour/alist-auth/utils"
)
var (
dropBoxAppSecret string
dropBoxAppId string
)
const (
DropboxAuthUrl = "https://api.dropboxapi.com/oauth2/token"
)
type AccessTokenReqData struct {
Code string `json:"code" form:"code"`
ClientId string `json:"client_id" form:"client_id"`
ClientSecret string `json:"client_secret" form:"client_secret"`
RefreshToken string `json:"refresh_token" form:"refresh_token"`
GrantType string `json:"grant_type" form:"grant_type"`
RedirectUri string `json:"redirect_uri" form:"redirect_uri"`
}
type AccessTokenResData struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
AccountId string `json:"account_id"`
Uid string `json:"uid"`
}
func getDropBoxToken(g *gin.Context) {
var accessTokenReqData AccessTokenReqData
err := g.Bind(&accessTokenReqData)
if err != nil {
common.Error(g, err)
return
}
if accessTokenReqData.GrantType == "refresh_token" {
refreshDropBoxToken(g, accessTokenReqData)
return
}
client := utils.RestyClient.R()
client.SetFormData(map[string]string{
"code": accessTokenReqData.Code,
"client_id": func() string {
if accessTokenReqData.ClientSecret == "" {
return dropBoxAppId
}
return accessTokenReqData.ClientId
}(),
"client_secret": func() string {
if accessTokenReqData.ClientSecret == "" {
return dropBoxAppSecret
}
return accessTokenReqData.ClientSecret
}(),
"grant_type": accessTokenReqData.GrantType,
"redirect_uri": accessTokenReqData.RedirectUri,
}).SetHeaders(map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
})
res, err := client.Execute("POST", DropboxAuthUrl)
if err != nil {
common.Error(g, err)
return
}
if err := common.JsonBytes(g, res.Body()); err != nil {
utils.GetLogger(g).Error(err)
return
}
}
func refreshDropBoxToken(g *gin.Context, accessTokenReqData AccessTokenReqData) {
if accessTokenReqData.GrantType != "refresh_token" {
common.ErrorStr(g, "Invalid grant type")
return
}
client := utils.RestyClient.R()
client.SetFormData(map[string]string{
"refresh_token": accessTokenReqData.RefreshToken,
"client_id": func() string {
if accessTokenReqData.ClientId == "" {
return dropBoxAppId
}
return accessTokenReqData.ClientId
}(),
"client_secret": func() string {
if accessTokenReqData.ClientSecret == "" {
return dropBoxAppSecret
}
return accessTokenReqData.ClientSecret
}(),
"grant_type": accessTokenReqData.GrantType,
}).SetHeaders(map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
})
res, err := client.Execute("POST", DropboxAuthUrl)
if err != nil {
common.Error(g, err)
return
}
common.JsonBytes(g, res.Body())
}