E5BotForSQLite/model/client.go

148 lines
3.9 KiB
Go
Raw Normal View History

2021-06-14 21:37:58 +08:00
package model
2021-03-12 12:54:32 +08:00
2021-03-13 13:15:31 +08:00
import (
2021-06-19 13:21:11 +08:00
"github.com/guonaihong/gout"
2021-03-13 13:15:31 +08:00
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"net/url"
)
2021-03-12 12:54:32 +08:00
type Client struct {
2021-06-15 18:44:51 +08:00
ID int `gorm:"unique;primaryKey;not null"`
2021-06-15 09:59:22 +08:00
TgId int64 `gorm:"not null"`
2021-06-14 21:37:58 +08:00
RefreshToken string `gorm:"not null"`
2021-06-15 18:27:28 +08:00
MsId string `gorm:"not null"`
2021-06-14 21:37:58 +08:00
Uptime int64 `gorm:"autoUpdateTime;not null"`
Alias string `gorm:"not null"`
ClientId string `gorm:"not null"`
ClientSecret string `gorm:"not null"`
Other string
2021-03-12 12:54:32 +08:00
}
2021-06-15 18:27:28 +08:00
type ErrClient struct {
*Client
Err error
}
2021-03-13 13:15:31 +08:00
const (
msApiUrl string = "https://login.microsoftonline.com"
msGraUrl string = "https://graph.microsoft.com"
redirectUri string = "http://localhost/e5sub"
scope string = "openid offline_access mail.read user.read"
)
func NewClient(clientId string, clientSecret string) *Client {
return &Client{
ClientId: clientId,
ClientSecret: clientSecret,
}
}
func GetMSAuthUrl(clientId string) string {
return "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=" + clientId + "&response_type=code&redirect_uri=" + url.QueryEscape(redirectUri) + "&response_mode=query&scope=" + url.QueryEscape(scope)
}
func GetMSRegisterAppUrl() string {
ru := "https://developer.microsoft.com/en-us/graph/quick-start?appID=_appId_&appName=_appName_&redirectUrl=http://localhost:8000&platform=option-windowsuniversal"
deeplink := "/quickstart/graphIO?publicClientSupport=false&appName=e5sub&redirectUrl=http://localhost/e5sub&allowImplicitFlow=false&ru=" + url.QueryEscape(ru)
appUrl := "https://apps.dev.microsoft.com/?deepLink=" + url.QueryEscape(deeplink)
return appUrl
}
2021-06-19 13:21:11 +08:00
func (c *Client) GetTokenWithCode(code string) error {
var content string
err := gout.POST(msApiUrl + "/common/oauth2/v2.0/token").
SetWWWForm(gout.H{
"client_id": c.ClientId,
"client_secret": c.ClientSecret,
"grant_type": "authorization_code",
"scope": scope,
"code": code,
"redirect_uri": redirectUri,
}).
BindBody(&content).Do()
2021-06-15 18:27:28 +08:00
2021-03-13 13:15:31 +08:00
if err != nil {
return err
}
2021-06-19 13:21:11 +08:00
if gjson.Get(content, "token_type").String() == "Bearer" {
c.RefreshToken = gjson.Get(content, "refresh_token").String()
2021-03-13 13:15:31 +08:00
return nil
}
2021-06-19 13:21:11 +08:00
return errors.New(content)
2021-03-13 13:15:31 +08:00
}
2021-06-19 13:21:11 +08:00
//getToken return accessToken and error
func (c *Client) getToken() (string, error) {
var content string
err := gout.POST(msApiUrl + "/common/oauth2/v2.0/token").
SetWWWForm(gout.H{
"client_id": c.ClientId,
"client_secret": c.ClientSecret,
"grant_type": "refresh_token",
"scope": scope,
"refresh_token": c.RefreshToken,
"redirect_uri": redirectUri,
}).
BindBody(&content).
Do()
2021-03-13 13:15:31 +08:00
if err != nil {
2021-06-17 20:20:09 +08:00
return "", err
2021-03-13 13:15:31 +08:00
}
2021-06-19 13:21:11 +08:00
if gjson.Get(content, "token_type").String() == "Bearer" {
c.RefreshToken = gjson.Get(content, "refresh_token").String()
return gjson.Get(content, "access_token").String(), nil
2021-03-13 13:15:31 +08:00
}
2021-06-19 13:21:11 +08:00
return "", errors.New(gjson.Get(content, "error").String())
2021-03-13 13:15:31 +08:00
}
2021-06-19 13:21:11 +08:00
// GetUserInfo return infoJSON and error
func (c *Client) GetUserInfo() (string, error) {
var (
content string
err error
accessToken string
)
if accessToken, err = c.getToken(); err != nil {
2021-06-17 20:20:09 +08:00
return "", err
}
2021-06-19 13:21:11 +08:00
err = gout.GET(msGraUrl + "/v1.0/me/messages").
SetHeader(gout.H{
"Authorization": accessToken,
}).
BindBody(&content).
Do()
2021-03-13 13:15:31 +08:00
if err != nil {
return "", err
}
2021-06-15 18:27:28 +08:00
2021-06-19 13:21:11 +08:00
if gjson.Get(content, "id").String() != "" {
return content, nil
2021-03-13 13:15:31 +08:00
}
2021-06-19 13:21:11 +08:00
return "", errors.New(content)
2021-03-13 13:15:31 +08:00
}
func (c *Client) GetOutlookMails() error {
2021-06-19 13:21:11 +08:00
var content string
var err error
2021-06-17 20:20:09 +08:00
var accessToken string
2021-06-19 13:21:11 +08:00
if accessToken, err = c.getToken(); err != nil {
2021-03-13 13:15:31 +08:00
return err
}
2021-06-19 13:21:11 +08:00
err = gout.GET(msGraUrl + "/v1.0/me/messages").
SetHeader(gout.H{
"Authorization": accessToken,
}).
BindBody(&content).
Do()
2021-06-17 20:20:09 +08:00
if err != nil {
return err
}
2021-03-13 13:15:31 +08:00
//这里的.需要转义,否则会按路径的方式解析
2021-06-19 13:21:11 +08:00
if gjson.Get(content, "@odata\\.context").String() != "" {
2021-03-13 13:15:31 +08:00
return nil
}
2021-06-19 13:21:11 +08:00
return errors.New(gjson.Get(content, "error").String())
2021-03-13 13:15:31 +08:00
}