gitlab.go 2.23 KB
Newer Older
1
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2 3
// See License.txt for license information.

Christopher Speller's avatar
Christopher Speller committed
4
package oauthgitlab
5 6 7 8 9 10

import (
	"encoding/json"
	"io"
	"strconv"
	"strings"
Chris's avatar
Chris committed
11

Christopher Speller's avatar
Christopher Speller committed
12 13
	"github.com/mattermost/mattermost-server/einterfaces"
	"github.com/mattermost/mattermost-server/model"
14 15
)

Christopher Speller's avatar
Christopher Speller committed
16 17 18
type GitLabProvider struct {
}

19 20 21
type GitLabUser struct {
	Id       int64  `json:"id"`
	Username string `json:"username"`
22
	Login    string `json:"login"`
23 24 25 26
	Email    string `json:"email"`
	Name     string `json:"name"`
}

Christopher Speller's avatar
Christopher Speller committed
27 28
func init() {
	provider := &GitLabProvider{}
29
	einterfaces.RegisterOauthProvider(model.USER_AUTH_SERVICE_GITLAB, provider)
Christopher Speller's avatar
Christopher Speller committed
30 31 32 33
}

func userFromGitLabUser(glu *GitLabUser) *model.User {
	user := &model.User{}
34 35 36 37
	username := glu.Username
	if username == "" {
		username = glu.Login
	}
Christopher Speller's avatar
Christopher Speller committed
38
	user.Username = model.CleanUsername(username)
39 40 41 42 43 44 45 46 47 48 49
	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
50 51
	userId := strconv.FormatInt(glu.Id, 10)
	user.AuthData = &userId
52
	user.AuthService = model.USER_AUTH_SERVICE_GITLAB
53 54 55 56

	return user
}

Christopher Speller's avatar
Christopher Speller committed
57
func gitLabUserFromJson(data io.Reader) *GitLabUser {
58 59 60 61 62 63 64 65 66 67
	decoder := json.NewDecoder(data)
	var glu GitLabUser
	err := decoder.Decode(&glu)
	if err == nil {
		return &glu
	} else {
		return nil
	}
}

68 69 70 71 72 73 74 75 76
func (glu *GitLabUser) ToJson() string {
	b, err := json.Marshal(glu)
	if err != nil {
		return ""
	} else {
		return string(b)
	}
}

77 78 79 80 81 82 83 84 85 86 87 88
func (glu *GitLabUser) IsValid() bool {
	if glu.Id == 0 {
		return false
	}

	if len(glu.Email) == 0 {
		return false
	}

	return true
}

Christopher Speller's avatar
Christopher Speller committed
89
func (glu *GitLabUser) getAuthData() string {
90 91
	return strconv.FormatInt(glu.Id, 10)
}
Christopher Speller's avatar
Christopher Speller committed
92 93

func (m *GitLabProvider) GetIdentifier() string {
94
	return model.USER_AUTH_SERVICE_GITLAB
Christopher Speller's avatar
Christopher Speller committed
95 96 97
}

func (m *GitLabProvider) GetUserFromJson(data io.Reader) *model.User {
98 99 100 101 102 103
	glu := gitLabUserFromJson(data)
	if glu.IsValid() {
		return userFromGitLabUser(glu)
	}

	return &model.User{}
Christopher Speller's avatar
Christopher Speller committed
104 105 106
}

func (m *GitLabProvider) GetAuthDataFromJson(data io.Reader) string {
107 108 109 110 111 112 113
	glu := gitLabUserFromJson(data)

	if glu.IsValid() {
		return glu.getAuthData()
	}

	return ""
Christopher Speller's avatar
Christopher Speller committed
114
}