summaryrefslogtreecommitdiffstats
path: root/model
diff options
context:
space:
mode:
authorJoramWilander <jwawilander@gmail.com>2015-07-15 12:48:50 -0400
committerJoramWilander <jwawilander@gmail.com>2015-07-22 08:41:53 -0400
commitc39e95c7cb1ad6e812aa3ce4000b4dfdf214e77e (patch)
tree78075f919a9efd78ad845c6b0f50511a0a826e43 /model
parent2fef71da693b5afcc31ccad6be8790da8a70817f (diff)
downloadchat-c39e95c7cb1ad6e812aa3ce4000b4dfdf214e77e.tar.gz
chat-c39e95c7cb1ad6e812aa3ce4000b4dfdf214e77e.tar.bz2
chat-c39e95c7cb1ad6e812aa3ce4000b4dfdf214e77e.zip
inital implementation of using GitLab OAuth2 provider for signup/login
Diffstat (limited to 'model')
-rw-r--r--model/access.go41
-rw-r--r--model/user.go81
2 files changed, 110 insertions, 12 deletions
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
+ }
+}