-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
63 lines (52 loc) · 1.38 KB
/
auth.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
package main
import (
"encoding/json"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"io/ioutil"
)
// User is a retrieved and authenticated user.
type GoogleUser struct {
Sub string `json:"sub"`
Name string `json:"name"`
GivenName string `json:"given_name"`
FamilyName string `json:"family_name"`
Profile string `json:"profile"`
Picture string `json:"picture"`
Email string `json:"email"`
EmailVerified bool `json:"email_verified"`
Gender string `json:"gender"`
}
func authInit (ClientID, ClientSecret, RedirectUrl string) {
oauthConf = &oauth2.Config{
ClientID: ClientID,
ClientSecret: ClientSecret,
RedirectURL: RedirectUrl,
Scopes: []string{
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
},
Endpoint: google.Endpoint,
}
}
func userInfoFromToken (token *oauth2.Token) (err error, user *GoogleUser) {
if !token.Valid() {
log.Error(err)
return
}
client := oauthConf.Client(oauth2.NoContext, token)
response, err := client.Get("https://www.googleapis.com/oauth2/v3/userinfo")
if err != nil {
log.Error("Error getting user from token ", err)
return
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
err = json.Unmarshal(contents, &user)
if err != nil {
log.Error("Error unmarshaling Google user", err)
return
}
return
}