miniflux/oauth2/google.go

88 lines
2.2 KiB
Go
Raw Normal View History

2017-11-23 07:22:33 +01:00
// 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.
2018-08-25 06:51:50 +02:00
package oauth2 // import "miniflux.app/oauth2"
2017-11-23 07:22:33 +01:00
import (
"context"
"encoding/json"
"fmt"
"miniflux.app/model"
2017-11-23 07:22:33 +01:00
"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 {
2017-11-25 01:09:10 +01:00
return "google_id"
}
func (g *googleProvider) GetRedirectURL(state string) string {
2017-11-23 07:22:33 +01:00
return g.config().AuthCodeURL(state)
}
func (g *googleProvider) GetProfile(ctx context.Context, code string) (*Profile, error) {
2017-11-23 07:22:33 +01:00
conf := g.config()
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("oauth2: unable to unserialize google profile: %v", err)
2017-11-23 07:22:33 +01:00
}
2017-11-25 01:09:10 +01:00
profile := &Profile{Key: g.GetUserExtraKey(), ID: user.Sub, Username: user.Email}
2017-11-23 07:22:33 +01:00
return profile, nil
}
func (g *googleProvider) PopulateUserCreationWithProfileID(user *model.UserCreationRequest, profile *Profile) {
user.GoogleID = profile.ID
}
func (g *googleProvider) PopulateUserWithProfileID(user *model.User, profile *Profile) {
user.GoogleID = profile.ID
}
func (g *googleProvider) UnsetUserProfileID(user *model.User) {
user.GoogleID = ""
}
func (g *googleProvider) config() *oauth2.Config {
2017-11-23 07:22:33 +01:00
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}
}