|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package cloudbrain
-
- import (
- "encoding/json"
- "errors"
- "io/ioutil"
- "net/http"
- "strings"
-
- "code.gitea.io/gitea/modules/log"
- "code.gitea.io/gitea/modules/setting"
- )
-
- const (
- GrantTypePassword = "password"
- ScopeRead = "read"
- TokenUrl = "/oauth/token"
- )
-
- type RespAuth struct {
- AccessToken string `json:"access_token"`
- RefreshToken string `json:"refresh_token"`
- TokenType string `json:"token_type"`
- ExpiresIn int `json:"expires_in"`
- Error string `json:"error"`
- ErrorDescription string `json:"error_description"`
- }
-
- type CloudBrainUser struct {
- UserName string `json:"username"`
- Email string `json:"email"`
- }
-
- func UserValidate(username string, password string) (string, error) {
- reqHttp := "client_id=" + setting.ClientID + "&client_secret=" + setting.ClientSecret +
- "&grant_type=" + GrantTypePassword + "&scope=" + ScopeRead + "&username=" + username +
- "&password=" + password
- resp, err := http.Post(setting.UserCeterHost + TokenUrl,
- "application/x-www-form-urlencoded",
- strings.NewReader(reqHttp))
- if err != nil {
- log.Error("req user center failed:" + err.Error())
- return "", err
- }
-
- body,err := ioutil.ReadAll(resp.Body)
- if err != nil {
- log.Error("read resp body failed:" + err.Error())
- return "", err
- }
-
- var respAuth RespAuth
- err = json.Unmarshal(body, &respAuth)
- if err != nil {
- log.Error("unmarshal resp failed:" + err.Error())
- return "", err
- }
-
- if respAuth.Error != "" {
- log.Error("req user_center for token failed:" + respAuth.Error + ":" + respAuth.ErrorDescription)
- return "", errors.New(respAuth.ErrorDescription)
- }
-
- return respAuth.AccessToken, nil
- }
-
- func GetUserInfo(username string, token string) (*CloudBrainUser, error) {
- user := &CloudBrainUser{}
- return user, nil
- }
|