forked from kuleuven/golang-url-shortener
/
google.go
62 lines (54 loc) · 1.64 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
package auth
import (
"context"
"encoding/json"
"github.com/mxschmitt/golang-url-shortener/internal/util"
"github.com/pkg/errors"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
type googleAdapter struct {
config *oauth2.Config
}
// NewGoogleAdapter creates an oAuth adapter out of the credentials and the baseURL
func NewGoogleAdapter(clientID, clientSecret string) Adapter {
return &googleAdapter{&oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURL: util.GetConfig().BaseURL + "/api/v1/auth/google/callback",
Scopes: []string{
"https://www.googleapis.com/auth/userinfo.email",
},
Endpoint: google.Endpoint,
}}
}
func (a *googleAdapter) GetRedirectURL(state string) string {
return a.config.AuthCodeURL(state)
}
func (a *googleAdapter) GetUserData(state, code string) (*user, error) {
oAuthToken, err := a.config.Exchange(context.Background(), code)
if err != nil {
return nil, errors.Wrap(err, "could not exchange code")
}
oAuthUserInfoReq, err := a.config.Client(context.Background(), oAuthToken).Get("https://www.googleapis.com/oauth2/v3/userinfo")
if err != nil {
return nil, errors.Wrap(err, "could not get user data")
}
defer oAuthUserInfoReq.Body.Close()
var gUser struct {
Sub string `json:"sub"`
Name string `json:"name"`
Picture string `json:"picture"`
}
if err = json.NewDecoder(oAuthUserInfoReq.Body).Decode(&gUser); err != nil {
return nil, errors.Wrap(err, "decoding user info failed")
}
return &user{
ID: gUser.Sub,
Name: gUser.Name,
Picture: gUser.Picture + "?sz=64",
}, nil
}
func (a *googleAdapter) GetOAuthProviderName() string {
return "google"
}