haiwai-common-sdk-go/services/sso/client.go
2023-03-16 20:15:17 +08:00

95 lines
2.3 KiB
Go

package sso
import (
"fmt"
"golib.gaore.com/GaoreGo/haiwai-common-sdk-go/sdk"
"golib.gaore.com/GaoreGo/haiwai-common-sdk-go/sdk/requests"
"net/url"
"strings"
)
const (
VERSION = "2020-08-07"
)
var HOST = requests.Host{
Default: "sso",
Func: func(s string, area string) string {
host := map[string]string{
requests.RELEASE: "sso.gaore.com.hk",
requests.PRE: "sso.gaore.com",
requests.TEST: "sso.gaore.com",
}
if s, ok := host[s]; ok {
return s
} else {
return "sso.gaore.com.hk"
}
},
}
type Client struct {
sdk.Client
ident string
}
func (c *Client) CodeAuth(req *CodeAuthRequest) (response *CodeAuthResponse, err error) {
response = CreateCodeAuthResponse()
err = c.DoAction(req, response)
return
}
func (c *Client) GetUserInfo(req *GetUserInfoRequest) (response *GetUserInfoResponse, err error) {
response = CreateGetUserInfoResponse()
err = c.DoAction(req, response)
return
}
func (c *Client) RefreshToken(req *RefreshTokenRequest) (response *RefreshTokenResponse, err error) {
response = CreateRefreshTokenResponse()
err = c.DoAction(req, response)
return
}
func (c *Client) SearchUser(req *SearchUserRequest) (response *SearchUserResponse, err error) {
response = CreateSearchUserResponse()
err = c.DoAction(req, response)
return
}
func (c *Client) GetLoginUrl(redirectUrl string, env ...string) string {
uri := fmt.Sprintf("/admin/main/login?ident=%s&redirectUrl=%s", c.ident, url.QueryEscape(redirectUrl))
scheme, host := c.getSchemeAndHost(env...)
return fmt.Sprintf("%s://%s%s", scheme, host, uri)
}
func (c *Client) GetLogout(env ...string) string {
uri := fmt.Sprintf("/admin/main/logout?ident=%s&redirectUrl=", c.ident)
scheme, host := c.getSchemeAndHost(env...)
return fmt.Sprintf("%s://%s%s", scheme, host, uri)
}
func (c Client) getSchemeAndHost(env ...string) (scheme, host string) {
rEnv := requests.RELEASE
if len(env) >= 1 {
rEnv = env[0]
}
host = HOST.Func(rEnv, "")
if rEnv == requests.RELEASE {
scheme = requests.HTTPS
} else {
scheme = requests.HTTP
}
scheme = strings.ToLower(scheme)
return
}
func NewClientWithAccessKey(accesskey, secrect, source string) (client *Client, err error) {
client = &Client{}
client.ident = source
err = client.InitWithAccessKey(accesskey, secrect, source)
return
}