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.

cloudbrain.go 3.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package cloudbrain
  2. import (
  3. "bytes"
  4. "code.gitea.io/gitea/modules/log"
  5. "code.gitea.io/gitea/modules/setting"
  6. "encoding/json"
  7. "errors"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. )
  12. const (
  13. UrlToken = "/rest-server/api/v1/token/"
  14. UrlGetUserInfo = "/rest-server/api/v1/user/"
  15. TokenTypeBear = "Bearer "
  16. SuccessCode = "S000"
  17. )
  18. type RespAuth struct {
  19. AccessToken string `json:"access_token"`
  20. RefreshToken string `json:"refresh_token"`
  21. TokenType string `json:"token_type"`
  22. ExpiresIn int `json:"expires_in"`
  23. Error string `json:"error"`
  24. ErrorDescription string `json:"error_description"`
  25. }
  26. type RespToken struct {
  27. Code string `json:"code"`
  28. Message string `json:"message"`
  29. Payload PayloadToken `json:"payload"`
  30. }
  31. type PayloadToken struct {
  32. Username string `json:"username"`
  33. Token string `json:"token"`
  34. IsAdmin bool `json:"admin"`
  35. }
  36. type RespUserInfo struct {
  37. Code string `json:"code"`
  38. Message string `json:"message"`
  39. Payload PayloadUserInfo `json:"payload"`
  40. }
  41. type PayloadUserInfo struct {
  42. UserInfo StUserInfo `json:"userInfo"`
  43. }
  44. type StUserInfo struct {
  45. Email string `json:"email"`
  46. }
  47. type CloudBrainUser struct {
  48. UserName string `json:"username"`
  49. Email string `json:"email"`
  50. }
  51. func UserValidate(username string, password string) (string, error) {
  52. values := map[string]string{"username": username, "password": password}
  53. jsonValue, _ := json.Marshal(values)
  54. resp, err := http.Post(setting.RestServerHost + UrlToken,
  55. "application/json",
  56. bytes.NewBuffer(jsonValue))
  57. if err != nil {
  58. log.Error("req user center failed:" + err.Error())
  59. return "", err
  60. }
  61. body,err := ioutil.ReadAll(resp.Body)
  62. if err != nil {
  63. log.Error("read resp body failed:" + err.Error())
  64. return "", err
  65. }
  66. var res RespToken
  67. err = json.Unmarshal(body, &res)
  68. if err != nil {
  69. log.Error("unmarshal res failed:" + err.Error())
  70. return "", err
  71. }
  72. if res.Code != SuccessCode {
  73. log.Error("req rest-server for token failed:", res.Message)
  74. return "", errors.New(res.Message)
  75. }
  76. return res.Payload.Token, nil
  77. }
  78. func GetUserInfo(username string, token string) (*CloudBrainUser, error) {
  79. user := &CloudBrainUser{}
  80. client := &http.Client{}
  81. reqHttp,err := http.NewRequest("GET", setting.RestServerHost + UrlGetUserInfo + username, strings.NewReader(""))
  82. if err != nil {
  83. log.Error("new req failed:", err.Error())
  84. return nil, err
  85. }
  86. reqHttp.Header.Set("Authorization", TokenTypeBear + token)
  87. resp,err := client.Do(reqHttp)
  88. if err != nil {
  89. log.Error("req rest-server failed:", err.Error())
  90. return nil, err
  91. }
  92. body,err := ioutil.ReadAll(resp.Body)
  93. if err != nil {
  94. log.Error("read resp body failed:", err.Error())
  95. return nil, err
  96. }
  97. var res RespUserInfo
  98. err = json.Unmarshal(body, &res)
  99. if err != nil {
  100. log.Error("unmarshal resp failed:", err.Error())
  101. return nil, err
  102. }
  103. if res.Code != SuccessCode {
  104. log.Error("get userInfo failed:", err.Error())
  105. return nil, err
  106. }
  107. user.Email = res.Payload.UserInfo.Email
  108. return user, nil
  109. }