You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sso.go 1.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package sso
  2. import (
  3. "fmt"
  4. "gitlink.org.cn/cloudream/common/consts/errorcode"
  5. "gitlink.org.cn/cloudream/common/utils/http2"
  6. "gitlink.org.cn/cloudream/common/utils/serder"
  7. "net/url"
  8. "strings"
  9. )
  10. type SSOInfoResp struct {
  11. NickName string `json:"nickName"`
  12. Roles []string `json:"roles"`
  13. ID string `json:"id"`
  14. Username string `json:"username"`
  15. }
  16. func (c *Client) GetSSOInfo(token string) (*SSOInfoResp, error) {
  17. targetUrl, err := url.JoinPath(c.baseURL, "/admin/info")
  18. if err != nil {
  19. return nil, err
  20. }
  21. resp, err := http2.GetJSON(targetUrl, http2.RequestParam{
  22. Header: map[string]string{
  23. "Authorization": token,
  24. "Host": "ai4m.jointcloud.net",
  25. "Connection": "keep-alive",
  26. },
  27. })
  28. if err != nil {
  29. return nil, err
  30. }
  31. contType := resp.Header.Get("Content-Type")
  32. if strings.Contains(contType, http2.ContentTypeJSON) {
  33. var codeResp response[SSOInfoResp]
  34. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  35. return nil, fmt.Errorf("parsing response: %w", err)
  36. }
  37. if codeResp.Code == errorcode.OK {
  38. return &codeResp.Data, nil
  39. }
  40. return nil, codeResp.ToError()
  41. }
  42. return nil, fmt.Errorf("unknow response content type: %s", contType)
  43. }