aboutsummaryrefslogtreecommitdiff
path: root/oauth2/google.go
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net> 2018-01-02 22:04:48 -0800
committerGravatar Frédéric Guillot <fred@miniflux.net> 2018-01-02 22:04:48 -0800
commit320d1b016747ba4501da9417d9ce5f99368a5768 (patch)
tree1054d96afde6022951b76cc4a09b78e1e3f05058 /oauth2/google.go
parentc39f2e1a8d2de6d412bcc673d29eb0f7a2d1f5f7 (diff)
downloadv2-320d1b016747ba4501da9417d9ce5f99368a5768.tar.gz
v2-320d1b016747ba4501da9417d9ce5f99368a5768.tar.zst
v2-320d1b016747ba4501da9417d9ce5f99368a5768.zip
Refactor packages to have more idiomatic code base
Diffstat (limited to 'oauth2/google.go')
-rw-r--r--oauth2/google.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/oauth2/google.go b/oauth2/google.go
new file mode 100644
index 00000000..e57e027f
--- /dev/null
+++ b/oauth2/google.go
@@ -0,0 +1,74 @@
+// Copyright 2017 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package oauth2
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+
+ "golang.org/x/oauth2"
+)
+
+type googleProfile struct {
+ Sub string `json:"sub"`
+ Email string `json:"email"`
+}
+
+type googleProvider struct {
+ clientID string
+ clientSecret string
+ redirectURL string
+}
+
+func (g googleProvider) GetUserExtraKey() string {
+ return "google_id"
+}
+
+func (g googleProvider) GetRedirectURL(state string) string {
+ return g.config().AuthCodeURL(state)
+}
+
+func (g googleProvider) GetProfile(code string) (*Profile, error) {
+ conf := g.config()
+ ctx := context.Background()
+ token, err := conf.Exchange(ctx, code)
+ if err != nil {
+ return nil, err
+ }
+
+ client := conf.Client(ctx, token)
+ resp, err := client.Get("https://www.googleapis.com/oauth2/v3/userinfo")
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+
+ var user googleProfile
+ decoder := json.NewDecoder(resp.Body)
+ if err := decoder.Decode(&user); err != nil {
+ return nil, fmt.Errorf("unable to unserialize google profile: %v", err)
+ }
+
+ profile := &Profile{Key: g.GetUserExtraKey(), ID: user.Sub, Username: user.Email}
+ return profile, nil
+}
+
+func (g googleProvider) config() *oauth2.Config {
+ return &oauth2.Config{
+ RedirectURL: g.redirectURL,
+ ClientID: g.clientID,
+ ClientSecret: g.clientSecret,
+ Scopes: []string{"email"},
+ Endpoint: oauth2.Endpoint{
+ AuthURL: "https://accounts.google.com/o/oauth2/auth",
+ TokenURL: "https://accounts.google.com/o/oauth2/token",
+ },
+ }
+}
+
+func newGoogleProvider(clientID, clientSecret, redirectURL string) *googleProvider {
+ return &googleProvider{clientID: clientID, clientSecret: clientSecret, redirectURL: redirectURL}
+}