From c39e95c7cb1ad6e812aa3ce4000b4dfdf214e77e Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Wed, 15 Jul 2015 12:48:50 -0400 Subject: inital implementation of using GitLab OAuth2 provider for signup/login --- model/access.go | 41 +++++++++++++++++++++++++++++ model/user.go | 81 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 110 insertions(+), 12 deletions(-) create mode 100644 model/access.go (limited to 'model') diff --git a/model/access.go b/model/access.go new file mode 100644 index 000000000..f9e36ce07 --- /dev/null +++ b/model/access.go @@ -0,0 +1,41 @@ +// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. +// See License.txt for license information. + +package model + +import ( + "encoding/json" + "io" +) + +const ( + ACCESS_TOKEN_GRANT_TYPE = "authorization_code" + ACCESS_TOKEN_TYPE = "bearer" +) + +type AccessResponse struct { + AccessToken string `json:"access_token"` + TokenType string `json:"token_type"` + ExpiresIn int32 `json:"expires_in"` + RefreshToken string `json:"refresh_token"` +} + +func (ar *AccessResponse) ToJson() string { + b, err := json.Marshal(ar) + if err != nil { + return "" + } else { + return string(b) + } +} + +func AccessResponseFromJson(data io.Reader) *AccessResponse { + decoder := json.NewDecoder(data) + var ar AccessResponse + err := decoder.Decode(&ar) + if err == nil { + return &ar + } else { + return nil + } +} diff --git a/model/user.go b/model/user.go index 727165b8c..22158b6ac 100644 --- a/model/user.go +++ b/model/user.go @@ -8,22 +8,24 @@ import ( "encoding/json" "io" "regexp" + "strconv" "strings" ) const ( - ROLE_ADMIN = "admin" - ROLE_SYSTEM_ADMIN = "system_admin" - ROLE_SYSTEM_SUPPORT = "system_support" - USER_AWAY_TIMEOUT = 5 * 60 * 1000 // 5 minutes - USER_OFFLINE_TIMEOUT = 1 * 60 * 1000 // 1 minute - USER_OFFLINE = "offline" - USER_AWAY = "away" - USER_ONLINE = "online" - USER_NOTIFY_ALL = "all" - USER_NOTIFY_MENTION = "mention" - USER_NOTIFY_NONE = "none" - BOT_USERNAME = "valet" + ROLE_ADMIN = "admin" + ROLE_SYSTEM_ADMIN = "system_admin" + ROLE_SYSTEM_SUPPORT = "system_support" + USER_AWAY_TIMEOUT = 5 * 60 * 1000 // 5 minutes + USER_OFFLINE_TIMEOUT = 1 * 60 * 1000 // 1 minute + USER_OFFLINE = "offline" + USER_AWAY = "away" + USER_ONLINE = "online" + USER_NOTIFY_ALL = "all" + USER_NOTIFY_MENTION = "mention" + USER_NOTIFY_NONE = "none" + BOT_USERNAME = "valet" + USER_AUTH_SERVICE_GITLAB = "gitlab" ) type User struct { @@ -48,6 +50,14 @@ type User struct { NotifyProps StringMap `json:"notify_props"` LastPasswordUpdate int64 `json:"last_password_update"` LastPictureUpdate int64 `json:"last_picture_update"` + AuthService string `json:"auth_service"` +} + +type GitLabUser struct { + Id int64 `json:"id"` + Username string `json:"username"` + Email string `json:"email"` + Name string `json:"name"` } // IsValid validates the user and returns an error if it isn't configured @@ -96,6 +106,22 @@ func (u *User) IsValid() *AppError { return NewAppError("User.IsValid", "Invalid last name", "user_id="+u.Id) } + if len(u.Password) > 128 { + return NewAppError("User.IsValid", "Invalid password", "user_id="+u.Id) + } + + if len(u.AuthData) > 128 { + return NewAppError("User.IsValid", "Invalid auth data", "user_id="+u.Id) + } + + if len(u.AuthData) > 0 && len(u.AuthService) == 0 { + return NewAppError("User.IsValid", "Invalid user, auth data must be set with auth type", "user_id="+u.Id) + } + + if len(u.Password) > 0 && len(u.AuthData) > 0 { + return NewAppError("User.IsValid", "Invalid user, password and auth data cannot both be set", "user_id="+u.Id) + } + return nil } @@ -328,3 +354,34 @@ func IsUsernameValid(username string) bool { return true } + +func UserFromGitLabUser(glu *GitLabUser) *User { + user := &User{} + user.Username = glu.Username + splitName := strings.Split(glu.Name, " ") + if len(splitName) == 2 { + user.FirstName = splitName[0] + user.LastName = splitName[1] + } else if len(splitName) >= 2 { + user.FirstName = splitName[0] + user.LastName = strings.Join(splitName[1:], " ") + } else { + user.FirstName = glu.Name + } + user.Email = glu.Email + user.AuthData = strconv.FormatInt(glu.Id, 10) + user.AuthService = USER_AUTH_SERVICE_GITLAB + + return user +} + +func GitLabUserFromJson(data io.Reader) *GitLabUser { + decoder := json.NewDecoder(data) + var glu GitLabUser + err := decoder.Decode(&glu) + if err == nil { + return &glu + } else { + return nil + } +} -- cgit v1.2.3-1-g7c22 From 03528b9619747b8bd184b852497dcf14ee1e0081 Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Fri, 17 Jul 2015 09:47:25 -0400 Subject: made oauth architecture more generalized --- model/user.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'model') diff --git a/model/user.go b/model/user.go index 22158b6ac..78b033327 100644 --- a/model/user.go +++ b/model/user.go @@ -385,3 +385,7 @@ func GitLabUserFromJson(data io.Reader) *GitLabUser { return nil } } + +func (glu *GitLabUser) GetAuthData() string { + return strconv.FormatInt(glu.Id, 10) +} -- cgit v1.2.3-1-g7c22 From 41bbbbf4462205348c978a2cce5162f73e35f6b7 Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Wed, 22 Jul 2015 15:05:20 -0400 Subject: add changes from team review --- model/user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'model') diff --git a/model/user.go b/model/user.go index 78b033327..c71d75405 100644 --- a/model/user.go +++ b/model/user.go @@ -37,6 +37,7 @@ type User struct { Username string `json:"username"` Password string `json:"password"` AuthData string `json:"auth_data"` + AuthService string `json:"auth_service"` Email string `json:"email"` EmailVerified bool `json:"email_verified"` Nickname string `json:"nickname"` @@ -50,7 +51,6 @@ type User struct { NotifyProps StringMap `json:"notify_props"` LastPasswordUpdate int64 `json:"last_password_update"` LastPictureUpdate int64 `json:"last_picture_update"` - AuthService string `json:"auth_service"` } type GitLabUser struct { -- cgit v1.2.3-1-g7c22