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.3 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package sso
  2. import (
  3. "fmt"
  4. "gitlink.org.cn/cloudream/common/utils/http2"
  5. "gitlink.org.cn/cloudream/common/utils/serder"
  6. "net/url"
  7. "strings"
  8. )
  9. type SSOInfoResp struct {
  10. NickName string `json:"nickName"`
  11. Roles []string `json:"roles"`
  12. ID string `json:"id"`
  13. Username string `json:"username"`
  14. Menus []any `json:"menus"`
  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. //all, err := io.ReadAll(resp.Body)
  32. //println("output: ")
  33. //str := string(all)
  34. //println(str)
  35. contType := resp.Header.Get("Content-Type")
  36. if strings.Contains(contType, http2.ContentTypeJSON) {
  37. var codeResp response[SSOInfoResp]
  38. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  39. return nil, fmt.Errorf("parsing response: %w", err)
  40. }
  41. if codeResp.Code == ResponseCodeOK {
  42. return &codeResp.Data, nil
  43. }
  44. return nil, codeResp.ToError()
  45. }
  46. return nil, fmt.Errorf("unknow response content type: %s", contType)
  47. }