diff --git a/models/login_source.go b/models/login_source.go index 89bacd48b..e8d3d0670 100755 --- a/models/login_source.go +++ b/models/login_source.go @@ -6,6 +6,7 @@ package models import ( + "code.gitea.io/gitea/modules/auth/cloudbrain" "crypto/tls" "encoding/json" "errors" @@ -14,7 +15,6 @@ import ( "net/textproto" "strings" - "code.gitea.io/gitea/modules/auth/cloudbrain" "code.gitea.io/gitea/modules/auth/ldap" "code.gitea.io/gitea/modules/auth/oauth2" "code.gitea.io/gitea/modules/auth/pam" @@ -22,7 +22,6 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" - gouuid "github.com/satori/go.uuid" "github.com/unknwon/com" "xorm.io/xorm" "xorm.io/xorm/convert" @@ -41,6 +40,7 @@ const ( LoginDLDAP // 5 LoginOAuth2 // 6 LoginSSPI // 7 + LoginCloubBrain // 8 ) // LoginNames contains the name of LoginType values. @@ -51,6 +51,7 @@ var LoginNames = map[LoginType]string{ LoginPAM: "PAM", LoginOAuth2: "OAuth2", LoginSSPI: "SPNEGO with SSPI", + LoginCloubBrain: "Cloud Brain", } // 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)) case LoginPAM: user, err = LoginViaPAM(user, login, password, source.ID, source.Cfg.(*PAMConfig)) + case LoginCloubBrain: + user, err = LoginViaCloudBrain(user, login, password, source) default: return nil, ErrUnsupportedLoginType } @@ -763,16 +766,6 @@ func UserSignIn(username, password string) (*User, error) { } 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 { case LoginNoType, LoginPlain, LoginOAuth2: if user.IsPasswordSet() && user.ValidatePassword(password) { @@ -807,32 +800,6 @@ func UserSignIn(username, password string) (*User, error) { 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) @@ -856,6 +823,34 @@ func UserSignIn(username, password string) (*User, error) { 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 } diff --git a/modules/auth/cloudbrain/cloudbrain.go b/modules/auth/cloudbrain/cloudbrain.go index c0ce3b3dd..2408128b8 100755 --- a/modules/auth/cloudbrain/cloudbrain.go +++ b/modules/auth/cloudbrain/cloudbrain.go @@ -26,7 +26,12 @@ type RespAuth struct { 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 + "&grant_type=" + GrantTypePassword + "&scope=" + ScopeRead + "&username=" + username + "&password=" + password @@ -35,29 +40,31 @@ func UserValidate(username string, password string) (string, string, error) { strings.NewReader(reqHttp)) if err != nil { log.Error("req user center failed:" + err.Error()) - return "", "", err + return "", err } body,err := ioutil.ReadAll(resp.Body) if err != nil { log.Error("read resp body failed:" + err.Error()) - return "", "", err + return "", err } var respAuth RespAuth err = json.Unmarshal(body, &respAuth) if err != nil { log.Error("unmarshal resp failed:" + err.Error()) - return "", "", err + 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) + 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 } diff --git a/routers/repo/attachment.go b/routers/repo/attachment.go index e5f811e96..266e3c0cf 100755 --- a/routers/repo/attachment.go +++ b/routers/repo/attachment.go @@ -5,13 +5,6 @@ package repo import ( - contexExt "context" - "encoding/json" - "fmt" - "net/http" - "strconv" - "strings" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" @@ -20,6 +13,12 @@ import ( "code.gitea.io/gitea/modules/storage" "code.gitea.io/gitea/modules/upload" "code.gitea.io/gitea/modules/worker" + contexExt "context" + "encoding/json" + "fmt" + "net/http" + "strconv" + "strings" gouuid "github.com/satori/go.uuid" ) @@ -31,8 +30,11 @@ const ( ) type PublicDataset struct { + UUID string `json:"id"` 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) { @@ -635,13 +637,20 @@ func QueryAllPublicDataset(ctx *context.Context){ var publicDatasets []PublicDataset 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.Bucket + "/" + setting.Attachment.Minio.BasePath + models.AttachmentRelativePath(attch.UUID) + - attch.UUID}) + attch.UUID, + "admin", + attch.CreatedUnix.Format("2006-01-02 03:04:05")}) } data,err := json.Marshal(publicDatasets)