blob: e189ee68d91356b8ba054061e56eb02930ad695b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package oauthgitlab
import (
"encoding/json"
"github.com/mattermost/platform/einterfaces"
"github.com/mattermost/platform/model"
"io"
"strconv"
"strings"
)
type GitLabProvider struct {
}
type GitLabUser struct {
Id int64 `json:"id"`
Username string `json:"username"`
Login string `json:"login"`
Email string `json:"email"`
Name string `json:"name"`
}
func init() {
provider := &GitLabProvider{}
einterfaces.RegisterOauthProvider(model.USER_AUTH_SERVICE_GITLAB, provider)
}
func userFromGitLabUser(glu *GitLabUser) *model.User {
user := &model.User{}
username := glu.Username
if username == "" {
username = glu.Login
}
user.Username = model.CleanUsername(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
}
strings.TrimSpace(user.Email)
user.Email = glu.Email
userId := strconv.FormatInt(glu.Id, 10)
user.AuthData = &userId
user.AuthService = model.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
}
}
func (glu *GitLabUser) IsValid() bool {
if glu.Id == 0 {
return false
}
if len(glu.Email) == 0 {
return false
}
return true
}
func (glu *GitLabUser) getAuthData() string {
return strconv.FormatInt(glu.Id, 10)
}
func (m *GitLabProvider) GetIdentifier() string {
return model.USER_AUTH_SERVICE_GITLAB
}
func (m *GitLabProvider) GetUserFromJson(data io.Reader) *model.User {
glu := gitLabUserFromJson(data)
if glu.IsValid() {
return userFromGitLabUser(glu)
}
return &model.User{}
}
func (m *GitLabProvider) GetAuthDataFromJson(data io.Reader) string {
glu := gitLabUserFromJson(data)
if glu.IsValid() {
return glu.getAuthData()
}
return ""
}
|