|
- 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"`
- }
-
- func UserValidate(username string, password string) (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 != "" {
- /*enc := mahonia.NewEncoder("GBK")
- output := enc.ConvertString(respAuth.ErrorDescription)*/
- log.Error("req user_center for token failed:" + respAuth.Error + ":" + respAuth.ErrorDescription)
- return "", "", errors.New(respAuth.ErrorDescription)
- }
-
- //todo: get email
- return "", respAuth.AccessToken, nil
- }
|