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 1.7 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package cloudbrain
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/ioutil"
  6. "net/http"
  7. "strings"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/setting"
  10. )
  11. const (
  12. GrantTypePassword = "password"
  13. ScopeRead = "read"
  14. TokenUrl = "/oauth/token"
  15. )
  16. type RespAuth struct {
  17. AccessToken string `json:"access_token"`
  18. RefreshToken string `json:"refresh_token"`
  19. TokenType string `json:"token_type"`
  20. ExpiresIn int `json:"expires_in"`
  21. Error string `json:"error"`
  22. ErrorDescription string `json:"error_description"`
  23. }
  24. type CloudBrainUser struct {
  25. UserName string `json:"username"`
  26. Email string `json:"email"`
  27. }
  28. func UserValidate(username string, password string) (string, error) {
  29. reqHttp := "client_id=" + setting.ClientID + "&client_secret=" + setting.ClientSecret +
  30. "&grant_type=" + GrantTypePassword + "&scope=" + ScopeRead + "&username=" + username +
  31. "&password=" + password
  32. resp, err := http.Post(setting.UserCeterHost + TokenUrl,
  33. "application/x-www-form-urlencoded",
  34. strings.NewReader(reqHttp))
  35. if err != nil {
  36. log.Error("req user center failed:" + err.Error())
  37. return "", err
  38. }
  39. body,err := ioutil.ReadAll(resp.Body)
  40. if err != nil {
  41. log.Error("read resp body failed:" + err.Error())
  42. return "", err
  43. }
  44. var respAuth RespAuth
  45. err = json.Unmarshal(body, &respAuth)
  46. if err != nil {
  47. log.Error("unmarshal resp failed:" + err.Error())
  48. return "", err
  49. }
  50. if respAuth.Error != "" {
  51. log.Error("req user_center for token failed:" + respAuth.Error + ":" + respAuth.ErrorDescription)
  52. return "", errors.New(respAuth.ErrorDescription)
  53. }
  54. return respAuth.AccessToken, nil
  55. }
  56. func GetUserInfo(username string, token string) (*CloudBrainUser, error) {
  57. user := &CloudBrainUser{}
  58. return user, nil
  59. }