2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-23 07:22:33 +01:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package oauth2 // import "miniflux.app/v2/internal/oauth2"
|
2017-11-23 07:22:33 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/model"
|
2020-12-22 06:14:10 +01:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-03 06:35:10 +02:00
|
|
|
func NewGoogleProvider(clientID, clientSecret, redirectURL string) *googleProvider {
|
|
|
|
return &googleProvider{clientID: clientID, clientSecret: clientSecret, redirectURL: redirectURL}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *googleProvider) GetConfig() *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",
|
|
|
|
},
|
|
|
|
}
|
2017-11-25 01:09:10 +01:00
|
|
|
}
|
|
|
|
|
2023-09-03 06:35:10 +02:00
|
|
|
func (g *googleProvider) GetUserExtraKey() string {
|
|
|
|
return "google_id"
|
2017-11-23 07:22:33 +01:00
|
|
|
}
|
|
|
|
|
2023-09-03 06:35:10 +02:00
|
|
|
func (g *googleProvider) GetProfile(ctx context.Context, code, codeVerifier string) (*Profile, error) {
|
|
|
|
conf := g.GetConfig()
|
|
|
|
token, err := conf.Exchange(ctx, code, oauth2.SetAuthURLParam("code_verifier", codeVerifier))
|
2017-11-23 07:22:33 +01:00
|
|
|
if err != nil {
|
2023-09-09 03:01:17 +02:00
|
|
|
return nil, fmt.Errorf("google: failed to exchange token: %w", err)
|
2017-11-23 07:22:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
client := conf.Client(ctx, token)
|
|
|
|
resp, err := client.Get("https://www.googleapis.com/oauth2/v3/userinfo")
|
|
|
|
if err != nil {
|
2023-09-09 03:01:17 +02:00
|
|
|
return nil, fmt.Errorf("google: failed to get user info: %w", err)
|
2017-11-23 07:22:33 +01:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var user googleProfile
|
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
|
|
if err := decoder.Decode(&user); err != nil {
|
2023-09-09 03:01:17 +02:00
|
|
|
return nil, fmt.Errorf("google: unable to unserialize Google profile: %w", 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
|
|
|
|
}
|
|
|
|
|
2021-01-04 06:20:21 +01:00
|
|
|
func (g *googleProvider) PopulateUserCreationWithProfileID(user *model.UserCreationRequest, profile *Profile) {
|
|
|
|
user.GoogleID = profile.ID
|
|
|
|
}
|
|
|
|
|
2020-12-22 06:14:10 +01:00
|
|
|
func (g *googleProvider) PopulateUserWithProfileID(user *model.User, profile *Profile) {
|
|
|
|
user.GoogleID = profile.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *googleProvider) UnsetUserProfileID(user *model.User) {
|
|
|
|
user.GoogleID = ""
|
|
|
|
}
|