summaryrefslogtreecommitdiffstats
path: root/model/google.go
blob: 2a1eb3caa9fbb5ab46e40781a6d3cfcc307384eb (plain)
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
// 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
}