google.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package oauth2 // import "miniflux.app/v2/internal/oauth2"
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "miniflux.app/v2/internal/model"
  9. "golang.org/x/oauth2"
  10. )
  11. type googleProfile struct {
  12. Sub string `json:"sub"`
  13. Email string `json:"email"`
  14. }
  15. type googleProvider struct {
  16. clientID string
  17. clientSecret string
  18. redirectURL string
  19. }
  20. func NewGoogleProvider(clientID, clientSecret, redirectURL string) *googleProvider {
  21. return &googleProvider{clientID: clientID, clientSecret: clientSecret, redirectURL: redirectURL}
  22. }
  23. func (g *googleProvider) GetConfig() *oauth2.Config {
  24. return &oauth2.Config{
  25. RedirectURL: g.redirectURL,
  26. ClientID: g.clientID,
  27. ClientSecret: g.clientSecret,
  28. Scopes: []string{"email"},
  29. Endpoint: oauth2.Endpoint{
  30. AuthURL: "https://accounts.google.com/o/oauth2/auth",
  31. TokenURL: "https://accounts.google.com/o/oauth2/token",
  32. },
  33. }
  34. }
  35. func (g *googleProvider) GetUserExtraKey() string {
  36. return "google_id"
  37. }
  38. func (g *googleProvider) GetProfile(ctx context.Context, code, codeVerifier string) (*Profile, error) {
  39. conf := g.GetConfig()
  40. token, err := conf.Exchange(ctx, code, oauth2.SetAuthURLParam("code_verifier", codeVerifier))
  41. if err != nil {
  42. return nil, fmt.Errorf("google: failed to exchange token: %w", err)
  43. }
  44. client := conf.Client(ctx, token)
  45. resp, err := client.Get("https://www.googleapis.com/oauth2/v3/userinfo")
  46. if err != nil {
  47. return nil, fmt.Errorf("google: failed to get user info: %w", err)
  48. }
  49. defer resp.Body.Close()
  50. var user googleProfile
  51. decoder := json.NewDecoder(resp.Body)
  52. if err := decoder.Decode(&user); err != nil {
  53. return nil, fmt.Errorf("google: unable to unserialize Google profile: %w", err)
  54. }
  55. profile := &Profile{Key: g.GetUserExtraKey(), ID: user.Sub, Username: user.Email}
  56. return profile, nil
  57. }
  58. func (g *googleProvider) PopulateUserCreationWithProfileID(user *model.UserCreationRequest, profile *Profile) {
  59. user.GoogleID = profile.ID
  60. }
  61. func (g *googleProvider) PopulateUserWithProfileID(user *model.User, profile *Profile) {
  62. user.GoogleID = profile.ID
  63. }
  64. func (g *googleProvider) UnsetUserProfileID(user *model.User) {
  65. user.GoogleID = ""
  66. }