aboutsummaryrefslogtreecommitdiff
path: root/internal/auth/model.go
blob: c51ff059a97561e68db305d71add10a7dbd4a7e2 (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
package auth

import (
	"time"

	"golang.org/x/oauth2"
)

// GoogleAuthProfile auth'd user profile
// ProfileId, Email, Name, PictureURL
type GoogleAuthProfile struct {
	ProfileID  string `json:"sub"`
	Email      string `json:"email"`
	Name       string `json:"name"`
	PictureURL string `json:"picture"`
}

// Profile profile and token
// Identifier + Token
type Profile struct {
	ID           string    `json:"sub"`
	Email        string    `json:"email"`
	AccessToken  string    `json:"access_token"`
	TokenType    string    `json:"token_type,omitempty"`
	RefreshToken string    `json:"refresh_token,omitempty"`
	Expiry       time.Time `json:"expiry,omitempty"`
}

// Token token from profile
func (ap *Profile) Token() (*oauth2.Token, error) {
	return &oauth2.Token{
		AccessToken:  ap.AccessToken,
		TokenType:    ap.TokenType,
		RefreshToken: ap.RefreshToken,
		Expiry:       ap.Expiry,
	}, nil
}

// NewAuthProfile merge token and profile
func NewAuthProfile(t *oauth2.Token, u *GoogleAuthProfile) *Profile {
	return &Profile{
		u.ProfileID,
		u.Email,
		t.AccessToken,
		t.TokenType,
		t.RefreshToken,
		t.Expiry,
	}
}