mindoc/utils/auth2/auth2.go
LawyZheng 08d0e1613d
重写Auth2.0登录逻辑 (#851)
* go mod update

* feat: change to new wxwork sso login

* fix: can't log in by workwx browser

* fix: workwx auto regist

* fix: change app.conf.example

* fix: workwx account can't be disabled

* fix: workwx account delete

* fix: workwx bind error

* feat: optimize wecom login

* feat: rewrite dingtalk login

* feat: rewrite dingtalk login

* feat: optimize auth2 login
2023-04-20 13:24:28 +08:00

90 lines
1.8 KiB
Go

package auth2
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
type UserInfo struct {
UserId string `json:"userid"` // 企业成员userid
Name string `json:"name"` // 姓名
Avatar string `json:"avatar"` // 头像
Mobile string `json:"mobile"` // 手机号
Mail string `json:"mail"` // 邮箱
}
func NewAccessToken(token IAccessToken) AccessTokenCache {
return AccessTokenCache{
AccessToken: token.GetToken(),
ExpireIn: token.GetExpireIn(),
ExpireTime: token.GetExpireTime(),
}
}
type AccessTokenCache struct {
ExpireIn time.Duration
ExpireTime time.Time
AccessToken string
}
func (a AccessTokenCache) GetToken() string {
return a.AccessToken
}
func (a AccessTokenCache) GetExpireIn() time.Duration {
return a.ExpireIn
}
func (a AccessTokenCache) GetExpireTime() time.Time {
return a.ExpireTime
}
func (a AccessTokenCache) IsExpired() bool {
return time.Now().After(a.ExpireTime)
}
type IAccessToken interface {
GetToken() string
GetExpireIn() time.Duration
GetExpireTime() time.Time
}
type Client interface {
GetAccessToken(ctx context.Context) (IAccessToken, error)
SetAccessToken(token IAccessToken)
BuildURL(callback string, isAppBrowser bool) string
ValidateCallback(state string) error
GetUserInfo(ctx context.Context, code string) (UserInfo, error)
}
type IResponse interface {
AsError() error
}
func Request(req *http.Request, v IResponse) error {
response, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer response.Body.Close()
b, err := io.ReadAll(response.Body)
if err != nil {
return err
}
if response.StatusCode != http.StatusOK {
return fmt.Errorf("status = %d, msg = %s", response.StatusCode, string(b))
}
if err := json.Unmarshal(b, v); err != nil {
return err
}
return v.AsError()
}