File size: 2,412 Bytes
9f6f5a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfd51fa
9f6f5a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 api

import (
	"alist/utils"
	"encoding/base64"
	"net/url"
	"strings"

	"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 {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}
	data, err := base64.StdEncoding.DecodeString(req.Client)
	if err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}
	clients := strings.Split(string(data), "::")
	if len(clients) < 3 {
		c.JSON(400, gin.H{"error": "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":  clients[3],
			}).
			Post(zone.Oauth + "/common/oauth2/v2.0/token")
		if err != nil {
			c.JSON(400, gin.H{"error": err.Error()})
			return
		}
		c.Data(res.StatusCode(), "application/json", res.Body())
		return
	}
	c.JSON(400, gin.H{"error": "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 {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}
	u, err := url.Parse(req.SiteUrl)
	if err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		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 {
			c.JSON(400, gin.H{"error": err.Error()})
			return
		}
		c.Data(res.StatusCode(), "application/json", res.Body())
		return
	}
	c.JSON(400, gin.H{"error": "zone doesn't exist"})
	return
}