diff options
author | Christopher Speller <crspeller@gmail.com> | 2015-08-18 08:47:35 -0400 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2015-08-18 08:47:35 -0400 |
commit | 2f1dcf6f5267ecf85d14c8a36a9d3059a5d41218 (patch) | |
tree | 1de5578c9050a33635960fc046fdf97339254be0 /model/google.go | |
parent | 96d1eb1c800a427e31e63970e57d0824a3bc91e3 (diff) | |
parent | a7f09be9783f3354ab38d4fb4fb82085241d018f (diff) | |
download | chat-2f1dcf6f5267ecf85d14c8a36a9d3059a5d41218.tar.gz chat-2f1dcf6f5267ecf85d14c8a36a9d3059a5d41218.tar.bz2 chat-2f1dcf6f5267ecf85d14c8a36a9d3059a5d41218.zip |
Merge pull request #381 from mattermost/mm-1705
MM-1705 add google as an oauth single-sign-on service
Diffstat (limited to 'model/google.go')
-rw-r--r-- | model/google.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/model/google.go b/model/google.go new file mode 100644 index 000000000..2a1eb3caa --- /dev/null +++ b/model/google.go @@ -0,0 +1,60 @@ +// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. +// See License.txt for license information. + +package model + +import ( + "encoding/json" + "io" + "strings" +) + +const ( + USER_AUTH_SERVICE_GOOGLE = "google" +) + +type GoogleUser struct { + Id string `json:"id"` + Nickname string `json:"nickname"` + DisplayName string `json:"displayName"` + Emails []map[string]string `json:"emails"` + Names map[string]string `json:"name"` +} + +func UserFromGoogleUser(gu *GoogleUser) *User { + user := &User{} + if len(gu.Nickname) > 0 { + user.Username = gu.Nickname + } else { + user.Username = strings.ToLower(strings.Replace(gu.DisplayName, " ", "", -1)) + } + user.FirstName = gu.Names["givenName"] + user.LastName = gu.Names["familyName"] + user.Nickname = gu.Nickname + + for _, e := range gu.Emails { + if e["type"] == "account" { + user.Email = e["value"] + } + } + + user.AuthData = gu.Id + user.AuthService = USER_AUTH_SERVICE_GOOGLE + + return user +} + +func GoogleUserFromJson(data io.Reader) *GoogleUser { + decoder := json.NewDecoder(data) + var gu GoogleUser + err := decoder.Decode(&gu) + if err == nil { + return &gu + } else { + return nil + } +} + +func (gu *GoogleUser) GetAuthData() string { + return gu.Id +} |