@@ -6,6 +6,7 @@ | |||||
package models | package models | ||||
import ( | import ( | ||||
"code.gitea.io/gitea/modules/auth/cloudbrain" | |||||
"crypto/tls" | "crypto/tls" | ||||
"encoding/json" | "encoding/json" | ||||
"errors" | "errors" | ||||
@@ -14,7 +15,6 @@ 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" | ||||
@@ -22,7 +22,6 @@ 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" | ||||
@@ -41,6 +40,7 @@ const ( | |||||
LoginDLDAP // 5 | LoginDLDAP // 5 | ||||
LoginOAuth2 // 6 | LoginOAuth2 // 6 | ||||
LoginSSPI // 7 | LoginSSPI // 7 | ||||
LoginCloubBrain // 8 | |||||
) | ) | ||||
// LoginNames contains the name of LoginType values. | // LoginNames contains the name of LoginType values. | ||||
@@ -51,6 +51,7 @@ var LoginNames = map[LoginType]string{ | |||||
LoginPAM: "PAM", | LoginPAM: "PAM", | ||||
LoginOAuth2: "OAuth2", | LoginOAuth2: "OAuth2", | ||||
LoginSSPI: "SPNEGO with SSPI", | LoginSSPI: "SPNEGO with SSPI", | ||||
LoginCloubBrain: "Cloud Brain", | |||||
} | } | ||||
// SecurityProtocolNames contains the name of SecurityProtocol values. | // SecurityProtocolNames contains the name of SecurityProtocol values. | ||||
@@ -716,6 +717,8 @@ func ExternalUserLogin(user *User, login, password string, source *LoginSource) | |||||
user, err = LoginViaSMTP(user, login, password, source.ID, source.Cfg.(*SMTPConfig)) | user, err = LoginViaSMTP(user, login, password, source.ID, source.Cfg.(*SMTPConfig)) | ||||
case LoginPAM: | case LoginPAM: | ||||
user, err = LoginViaPAM(user, login, password, source.ID, source.Cfg.(*PAMConfig)) | user, err = LoginViaPAM(user, login, password, source.ID, source.Cfg.(*PAMConfig)) | ||||
case LoginCloubBrain: | |||||
user, err = LoginViaCloudBrain(user, login, password, source) | |||||
default: | default: | ||||
return nil, ErrUnsupportedLoginType | return nil, ErrUnsupportedLoginType | ||||
} | } | ||||
@@ -763,16 +766,6 @@ 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) { | ||||
@@ -807,32 +800,6 @@ 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) | ||||
@@ -856,6 +823,34 @@ 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" | |||||
func LoginViaCloudBrain(user *User, login, password string, source *LoginSource) (*User, error) { | |||||
token, err := cloudbrain.UserValidate(login, password) | |||||
if err != nil { | |||||
log.Error("UserValidate(%s) failed: %v", login, err) | |||||
return nil, err | |||||
} | |||||
cloudBrainUser, err := cloudbrain.GetUserInfo(token, login) | |||||
if len(cloudBrainUser.Email) == 0 { | |||||
cloudBrainUser.Email = fmt.Sprintf("%s@cloudbrain", login) | |||||
} | |||||
user = &User{ | |||||
LowerName: strings.ToLower(login), | |||||
Name: login, | |||||
Email: cloudBrainUser.Email, | |||||
LoginType: source.Type, | |||||
LoginSource: source.ID, | |||||
LoginName: login, | |||||
IsActive: true, | |||||
} | |||||
err = CreateUser(user) | |||||
if err != nil { | |||||
log.Error("CreateUser(%s) failed: %v", login, err) | |||||
return nil, err | |||||
} | |||||
return user, err | |||||
} | } |
@@ -26,7 +26,12 @@ type RespAuth struct { | |||||
ErrorDescription string `json:"error_description"` | ErrorDescription string `json:"error_description"` | ||||
} | } | ||||
func UserValidate(username string, password string) (string, string, error) { | |||||
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 + | reqHttp := "client_id=" + setting.ClientID + "&client_secret=" + setting.ClientSecret + | ||||
"&grant_type=" + GrantTypePassword + "&scope=" + ScopeRead + "&username=" + username + | "&grant_type=" + GrantTypePassword + "&scope=" + ScopeRead + "&username=" + username + | ||||
"&password=" + password | "&password=" + password | ||||
@@ -35,29 +40,31 @@ func UserValidate(username string, password string) (string, string, error) { | |||||
strings.NewReader(reqHttp)) | strings.NewReader(reqHttp)) | ||||
if err != nil { | if err != nil { | ||||
log.Error("req user center failed:" + err.Error()) | log.Error("req user center failed:" + err.Error()) | ||||
return "", "", err | |||||
return "", err | |||||
} | } | ||||
body,err := ioutil.ReadAll(resp.Body) | body,err := ioutil.ReadAll(resp.Body) | ||||
if err != nil { | if err != nil { | ||||
log.Error("read resp body failed:" + err.Error()) | log.Error("read resp body failed:" + err.Error()) | ||||
return "", "", err | |||||
return "", err | |||||
} | } | ||||
var respAuth RespAuth | var respAuth RespAuth | ||||
err = json.Unmarshal(body, &respAuth) | err = json.Unmarshal(body, &respAuth) | ||||
if err != nil { | if err != nil { | ||||
log.Error("unmarshal resp failed:" + err.Error()) | log.Error("unmarshal resp failed:" + err.Error()) | ||||
return "", "", err | |||||
return "", err | |||||
} | } | ||||
if respAuth.Error != "" { | if respAuth.Error != "" { | ||||
/*enc := mahonia.NewEncoder("GBK") | |||||
output := enc.ConvertString(respAuth.ErrorDescription)*/ | |||||
log.Error("req user_center for token failed:" + respAuth.Error + ":" + respAuth.ErrorDescription) | log.Error("req user_center for token failed:" + respAuth.Error + ":" + respAuth.ErrorDescription) | ||||
return "", "", errors.New(respAuth.ErrorDescription) | |||||
return "", errors.New(respAuth.ErrorDescription) | |||||
} | } | ||||
//todo: get email | |||||
return "", respAuth.AccessToken, nil | |||||
return respAuth.AccessToken, nil | |||||
} | |||||
func GetUserInfo(username string, token string) (*CloudBrainUser, error) { | |||||
user := &CloudBrainUser{} | |||||
return user, nil | |||||
} | } |
@@ -5,13 +5,6 @@ | |||||
package repo | package repo | ||||
import ( | import ( | ||||
contexExt "context" | |||||
"encoding/json" | |||||
"fmt" | |||||
"net/http" | |||||
"strconv" | |||||
"strings" | |||||
"code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
"code.gitea.io/gitea/modules/context" | "code.gitea.io/gitea/modules/context" | ||||
"code.gitea.io/gitea/modules/log" | "code.gitea.io/gitea/modules/log" | ||||
@@ -20,6 +13,12 @@ import ( | |||||
"code.gitea.io/gitea/modules/storage" | "code.gitea.io/gitea/modules/storage" | ||||
"code.gitea.io/gitea/modules/upload" | "code.gitea.io/gitea/modules/upload" | ||||
"code.gitea.io/gitea/modules/worker" | "code.gitea.io/gitea/modules/worker" | ||||
contexExt "context" | |||||
"encoding/json" | |||||
"fmt" | |||||
"net/http" | |||||
"strconv" | |||||
"strings" | |||||
gouuid "github.com/satori/go.uuid" | gouuid "github.com/satori/go.uuid" | ||||
) | ) | ||||
@@ -31,8 +30,11 @@ const ( | |||||
) | ) | ||||
type PublicDataset struct { | type PublicDataset struct { | ||||
UUID string `json:"id"` | |||||
Name string `json:"name"` | Name string `json:"name"` | ||||
Path string `json:"path"` | |||||
Path string `json:"place"` | |||||
UserName string `json:"provider"` | |||||
CreateTime string `json:"created_at"` | |||||
} | } | ||||
func RenderAttachmentSettings(ctx *context.Context) { | func RenderAttachmentSettings(ctx *context.Context) { | ||||
@@ -635,13 +637,20 @@ func QueryAllPublicDataset(ctx *context.Context){ | |||||
var publicDatasets []PublicDataset | var publicDatasets []PublicDataset | ||||
for _, attch := range attachs { | for _, attch := range attachs { | ||||
//todo: if path not exist ,do not return | |||||
publicDatasets = append(publicDatasets, PublicDataset{attch.Name, | |||||
has,err := storage.Attachments.HasObject(models.AttachmentRelativePath(attch.UUID)) | |||||
if err != nil || !has { | |||||
continue | |||||
} | |||||
publicDatasets = append(publicDatasets, PublicDataset{attch.UUID, | |||||
attch.Name, | |||||
setting.Attachment.Minio.RealPath + | setting.Attachment.Minio.RealPath + | ||||
setting.Attachment.Minio.Bucket + "/" + | setting.Attachment.Minio.Bucket + "/" + | ||||
setting.Attachment.Minio.BasePath + | setting.Attachment.Minio.BasePath + | ||||
models.AttachmentRelativePath(attch.UUID) + | models.AttachmentRelativePath(attch.UUID) + | ||||
attch.UUID}) | |||||
attch.UUID, | |||||
"admin", | |||||
attch.CreatedUnix.Format("2006-01-02 03:04:05")}) | |||||
} | } | ||||
data,err := json.Marshal(publicDatasets) | data,err := json.Marshal(publicDatasets) | ||||