| @@ -1049,6 +1049,9 @@ RESULT_BACKEND = redis://localhost:6379 | |||||
| HOST = http://192.168.204.24 | HOST = http://192.168.204.24 | ||||
| USERNAME = | USERNAME = | ||||
| PASSWORD = | PASSWORD = | ||||
| USER_CENTER_HOST = http://192.168.202.73:31441 | |||||
| CLIENT_ID = 3Z377wcplxeE2qpycpjv | |||||
| CLIENT_SECRET = J5ykfVl2kcxW0H9cawSL | |||||
| ; cloudbrain visit opendata | ; cloudbrain visit opendata | ||||
| USER = cW4cMtH24eoWPE7X | USER = cW4cMtH24eoWPE7X | ||||
| PWD = 4BPmgvK2hb2Eywwyp4YZRY4B7yQf4DAC | PWD = 4BPmgvK2hb2Eywwyp4YZRY4B7yQf4DAC | ||||
| @@ -353,5 +353,5 @@ func GetAllPublicAttachments() ([]*Attachment, error) { | |||||
| func getAllPublicAttachments(e Engine) ([]*Attachment, error) { | func getAllPublicAttachments(e Engine) ([]*Attachment, error) { | ||||
| attachments := make([]*Attachment, 0, 10) | attachments := make([]*Attachment, 0, 10) | ||||
| return attachments, e.Where("is_private = true ").Find(&attachments) | |||||
| return attachments, e.Where("is_private = false ").Find(&attachments) | |||||
| } | } | ||||
| @@ -14,6 +14,7 @@ import ( | |||||
| "net/textproto" | "net/textproto" | ||||
| "strings" | "strings" | ||||
| "code.gitea.io/gitea/modules/auth/cloudbrain" | |||||
| "code.gitea.io/gitea/modules/auth/ldap" | "code.gitea.io/gitea/modules/auth/ldap" | ||||
| "code.gitea.io/gitea/modules/auth/oauth2" | "code.gitea.io/gitea/modules/auth/oauth2" | ||||
| "code.gitea.io/gitea/modules/auth/pam" | "code.gitea.io/gitea/modules/auth/pam" | ||||
| @@ -21,6 +22,7 @@ import ( | |||||
| "code.gitea.io/gitea/modules/setting" | "code.gitea.io/gitea/modules/setting" | ||||
| "code.gitea.io/gitea/modules/timeutil" | "code.gitea.io/gitea/modules/timeutil" | ||||
| gouuid "github.com/satori/go.uuid" | |||||
| "github.com/unknwon/com" | "github.com/unknwon/com" | ||||
| "xorm.io/xorm" | "xorm.io/xorm" | ||||
| "xorm.io/xorm/convert" | "xorm.io/xorm/convert" | ||||
| @@ -761,6 +763,16 @@ func UserSignIn(username, password string) (*User, error) { | |||||
| } | } | ||||
| if hasUser { | if hasUser { | ||||
| if user.CloudBrainValidated { | |||||
| _, _, err := cloudbrain.UserValidate(username, password) | |||||
| if err != nil { | |||||
| log.Error("cloudbrain.UserValidate(%s) failed: %v", username, err) | |||||
| return nil, err | |||||
| } else { | |||||
| return user, nil | |||||
| } | |||||
| } | |||||
| switch user.LoginType { | switch user.LoginType { | ||||
| case LoginNoType, LoginPlain, LoginOAuth2: | case LoginNoType, LoginPlain, LoginOAuth2: | ||||
| if user.IsPasswordSet() && user.ValidatePassword(password) { | if user.IsPasswordSet() && user.ValidatePassword(password) { | ||||
| @@ -795,6 +807,32 @@ func UserSignIn(username, password string) (*User, error) { | |||||
| return ExternalUserLogin(user, user.LoginName, password, &source) | return ExternalUserLogin(user, user.LoginName, password, &source) | ||||
| } | } | ||||
| } else { | |||||
| email, token, err := cloudbrain.UserValidate(username, password) | |||||
| if err == nil { | |||||
| if email == "" { | |||||
| email = genRandEmail() | |||||
| } | |||||
| log.Info(email) | |||||
| u := &User{ | |||||
| Name: username, | |||||
| Email: email, | |||||
| Passwd: password, | |||||
| IsActive: true, | |||||
| CloudBrainValidated: true, | |||||
| Token: token, | |||||
| } | |||||
| if err := CreateUser(u); err != nil { | |||||
| log.Error("CreateUser(%s) failed: %v", username, err) | |||||
| return nil, err | |||||
| } | |||||
| log.Info("Account created: %s", u.Name) | |||||
| return u, nil | |||||
| } | |||||
| log.Info("cloudbrain.UserValidate(%s) failed: %v", username, err) | |||||
| } | } | ||||
| sources := make([]*LoginSource, 0, 5) | sources := make([]*LoginSource, 0, 5) | ||||
| @@ -817,3 +855,7 @@ func UserSignIn(username, password string) (*User, error) { | |||||
| return nil, ErrUserNotExist{user.ID, user.Name, 0} | return nil, ErrUserNotExist{user.ID, user.Name, 0} | ||||
| } | } | ||||
| func genRandEmail() string{ | |||||
| return gouuid.NewV4().String() + "@cloudbrain.com" | |||||
| } | |||||
| @@ -165,6 +165,10 @@ type User struct { | |||||
| // Preferences | // Preferences | ||||
| DiffViewStyle string `xorm:"NOT NULL DEFAULT ''"` | DiffViewStyle string `xorm:"NOT NULL DEFAULT ''"` | ||||
| Theme string `xorm:"NOT NULL DEFAULT ''"` | Theme string `xorm:"NOT NULL DEFAULT ''"` | ||||
| //CloudBrain | |||||
| CloudBrainValidated bool `xorm:"NOT NULL DEFAULT false"` | |||||
| Token string `xorm:"VARCHAR(64)"` | |||||
| } | } | ||||
| // SearchOrganizationsOptions options to filter organizations | // SearchOrganizationsOptions options to filter organizations | ||||
| @@ -0,0 +1,63 @@ | |||||
| 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 | |||||
| } | |||||
| @@ -434,6 +434,9 @@ var ( | |||||
| //cloudbrain config | //cloudbrain config | ||||
| CBAuthUser string | CBAuthUser string | ||||
| CBAuthPassword string | CBAuthPassword string | ||||
| ClientID string | |||||
| ClientSecret string | |||||
| UserCeterHost string | |||||
| ) | ) | ||||
| // DateLang transforms standard language locale name to corresponding value in datetime plugin. | // DateLang transforms standard language locale name to corresponding value in datetime plugin. | ||||
| @@ -1105,6 +1108,9 @@ func NewContext() { | |||||
| sec = Cfg.Section("cloudbrain") | sec = Cfg.Section("cloudbrain") | ||||
| CBAuthUser = sec.Key("USER").MustString("cW4cMtH24eoWPE7X") | CBAuthUser = sec.Key("USER").MustString("cW4cMtH24eoWPE7X") | ||||
| CBAuthPassword = sec.Key("PWD").MustString("4BPmgvK2hb2Eywwyp4YZRY4B7yQf4DAC") | CBAuthPassword = sec.Key("PWD").MustString("4BPmgvK2hb2Eywwyp4YZRY4B7yQf4DAC") | ||||
| ClientID = sec.Key("CLIENT_ID").MustString("3Z377wcplxeE2qpycpjv") | |||||
| ClientSecret = sec.Key("CLIENT_SECRET").MustString("J5ykfVl2kcxW0H9cawSL") | |||||
| UserCeterHost = sec.Key("USER_CENTER_HOST").MustString("http://192.168.202.73:31441") | |||||
| } | } | ||||
| func loadInternalToken(sec *ini.Section) string { | func loadInternalToken(sec *ini.Section) string { | ||||
| @@ -627,6 +627,7 @@ func QueryAllPublicDataset(ctx *context.Context){ | |||||
| if err != nil { | if err != nil { | ||||
| ctx.JSON(200, map[string]string{ | ctx.JSON(200, map[string]string{ | ||||
| "result_code": "-1", | "result_code": "-1", | ||||
| "error_msg": err.Error(), | |||||
| "data": "", | "data": "", | ||||
| }) | }) | ||||
| return | return | ||||
| @@ -643,6 +644,7 @@ func QueryAllPublicDataset(ctx *context.Context){ | |||||
| log.Error("json.Marshal failed:", err.Error()) | log.Error("json.Marshal failed:", err.Error()) | ||||
| ctx.JSON(200, map[string]string{ | ctx.JSON(200, map[string]string{ | ||||
| "result_code": "-1", | "result_code": "-1", | ||||
| "error_msg": err.Error(), | |||||
| "data": "", | "data": "", | ||||
| }) | }) | ||||
| return | return | ||||
| @@ -650,6 +652,7 @@ func QueryAllPublicDataset(ctx *context.Context){ | |||||
| ctx.JSON(200, map[string]string{ | ctx.JSON(200, map[string]string{ | ||||
| "result_code": "0", | "result_code": "0", | ||||
| "error_msg": "", | |||||
| "data": string(data), | "data": string(data), | ||||
| }) | }) | ||||
| } | } | ||||