-
Notifications
You must be signed in to change notification settings - Fork 0
/
google.go
68 lines (55 loc) · 1.44 KB
/
google.go
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
package auth
import (
"github.com/dgrijalva/jwt-go"
)
type (
Google struct {
}
)
type GoogleTokenInfo struct {
Sub string `json:"sub"`
Name string `json:"name"`
FirstName string `json:"given_name"`
LastName string `json:"family_name"`
Email string `json:"email"`
Picture string `json:"picture"`
jwt.StandardClaims
}
func (c *GoogleTokenInfo) Valid() error {
// leeway in seconds
expiresLeeway := Leeway
issuedLeeway := Leeway
c.StandardClaims.ExpiresAt += expiresLeeway
c.StandardClaims.IssuedAt -= issuedLeeway
err := c.StandardClaims.Valid()
c.StandardClaims.ExpiresAt -= expiresLeeway
c.StandardClaims.IssuedAt += issuedLeeway
return err
}
const googleKeysEndpoint = "https://www.googleapis.com/oauth2/v2/certs"
func (s *Google) auth(token string) (ud *UserDetails, err error) {
return s.authWithCheckAUD(token, "")
}
func (s *Google) authWithCheckAUD(token, aud string) (ud *UserDetails, err error) {
t, err := new(jwt.Parser).ParseWithClaims(token, &GoogleTokenInfo{}, getTokenValidateFunc(googleKeysEndpoint))
if err != nil {
return nil, err
}
info, _ := t.Claims.(*GoogleTokenInfo)
if aud != "" {
if ok := info.VerifyAudience(aud, true); !ok {
return nil, ErrNotValidAudience
}
}
if info != nil {
ud = &UserDetails{
ID: info.Sub,
UserName: info.Name,
FirstName: info.FirstName,
LastName: info.LastName,
Email: info.Email,
Picture: info.Picture,
}
}
return
}