-
Notifications
You must be signed in to change notification settings - Fork 4
/
onedrive.go
108 lines (102 loc) · 2.23 KB
/
onedrive.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
package alist
import (
"encoding/base64"
"net/url"
"strings"
"api.nn.ci/apps/common"
"api.nn.ci/utils"
"github.com/gin-gonic/gin"
)
type Zone struct {
Oauth string
Api string
}
var zones = map[string]Zone{
"global": {
Oauth: "https://login.microsoftonline.com",
Api: "https://graph.microsoft.com",
},
"cn": {
Oauth: "https://login.chinacloudapi.cn",
Api: "https://microsoftgraph.chinacloudapi.cn",
},
"us": {
Oauth: "https://login.microsoftonline.us",
Api: "https://graph.microsoft.us",
},
"de": {
Oauth: "https://login.microsoftonline.de",
Api: "https://graph.microsoft.de",
},
}
func onedriveToken(c *gin.Context) {
req := struct {
Code string `json:"code"`
Client string `json:"client"`
}{}
err := c.ShouldBind(&req)
if err != nil {
common.Error(c, err)
return
}
data, err := base64.StdEncoding.DecodeString(req.Client)
if err != nil {
common.Error(c, err)
return
}
clients := strings.Split(string(data), "::")
if len(clients) < 3 {
common.ErrorStr(c, "client error")
return
}
if zone, ok := zones[clients[2]]; ok {
res, err := utils.RestyClient.R().
SetFormData(map[string]string{
"client_id": clients[0],
"client_secret": clients[1],
"code": req.Code,
"grant_type": "authorization_code",
"redirect_uri": "https://alist.nn.ci/tool/onedrive/callback",
}).
Post(zone.Oauth + "/common/oauth2/v2.0/token")
if err != nil {
common.Error(c, err)
return
}
common.JsonBytes(c, res.Body())
return
}
common.ErrorStr(c, "zone doesn't exist")
return
}
func spSiteID(c *gin.Context) {
req := struct {
AccessToken string `json:"access_token"`
SiteUrl string `json:"site_url"`
Zone string `json:"zone"`
}{}
err := c.ShouldBind(&req)
if err != nil {
common.Error(c, err)
return
}
u, err := url.Parse(req.SiteUrl)
if err != nil {
common.Error(c, err)
return
}
siteName := u.Path
if zone, ok := zones[req.Zone]; ok {
res, err := utils.RestyClient.R().
SetHeader("Authorization", "Bearer "+req.AccessToken).
Get(zone.Api + "/v1.0/sites/root:/" + siteName)
if err != nil {
common.Error(c, err)
return
}
common.JsonBytes(c, res.Body())
return
}
common.ErrorStr(c, "zone doesn't exist")
return
}