From 9710c9ddd7602149e72597fb275312eeb8fee6b0 Mon Sep 17 00:00:00 2001 From: yuxh Date: Thu, 23 Feb 2023 18:07:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=81=E8=A3=85sso=E7=99=BB=E5=85=A5?= =?UTF-8?q?=E7=99=BB=E5=87=BA=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/sso/client.go | 34 ++++++++++++++++++++++++++++++++++ services/sso/client_test.go | 11 +++++++++++ 2 files changed, 45 insertions(+) create mode 100644 services/sso/client_test.go diff --git a/services/sso/client.go b/services/sso/client.go index c3b6cc5..b39c8ed 100644 --- a/services/sso/client.go +++ b/services/sso/client.go @@ -1,8 +1,11 @@ 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 ( @@ -27,6 +30,7 @@ var HOST = requests.Host{ type Client struct { sdk.Client + ident string } func (c *Client) CodeAuth(req *CodeAuthRequest) (response *CodeAuthResponse, err error) { @@ -53,8 +57,38 @@ func (c *Client) SearchUser(req *SearchUserRequest) (response *SearchUserRespons 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 } diff --git a/services/sso/client_test.go b/services/sso/client_test.go new file mode 100644 index 0000000..aa8e2aa --- /dev/null +++ b/services/sso/client_test.go @@ -0,0 +1,11 @@ +package sso + +import ( + "golib.gaore.com/GaoreGo/haiwai-common-sdk-go/sdk/requests" + "testing" +) + +func TestLoginUrl(t *testing.T) { + c, _ := NewClientWithAccessKey("stat.gaore.com.hk", "", "stat_gaore_com_hk") + t.Log(c.GetLoginUrl("http://stat.gaore.com.hk/web/user/login", requests.TEST)) +}