google.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. "net/http"
  9. "miniflux.app/v2/internal/model"
  10. "golang.org/x/oauth2"
  11. )
  12. // Google OAuth2 API documentation: https://developers.google.com/identity/protocols/oauth2
  13. const (
  14. googleAuthURL = "https://accounts.google.com/o/oauth2/v2/auth"
  15. googleTokenURL = "https://oauth2.googleapis.com/token"
  16. googleUserInfoURL = "https://www.googleapis.com/oauth2/v3/userinfo"
  17. )
  18. type googleProfile struct {
  19. Sub string `json:"sub"`
  20. Email string `json:"email"`
  21. }
  22. type googleProvider struct {
  23. clientID string
  24. clientSecret string
  25. redirectURL string
  26. }
  27. // NewGoogleProvider returns a Provider that authenticates users via Google OAuth2.
  28. func NewGoogleProvider(clientID, clientSecret, redirectURL string) Provider {
  29. return &googleProvider{clientID: clientID, clientSecret: clientSecret, redirectURL: redirectURL}
  30. }
  31. func (g *googleProvider) Config() *oauth2.Config {
  32. return &oauth2.Config{
  33. RedirectURL: g.redirectURL,
  34. ClientID: g.clientID,
  35. ClientSecret: g.clientSecret,
  36. Scopes: []string{"email"},
  37. Endpoint: oauth2.Endpoint{
  38. AuthURL: googleAuthURL,
  39. TokenURL: googleTokenURL,
  40. },
  41. }
  42. }
  43. func (g *googleProvider) UserExtraKey() string {
  44. return "google_id"
  45. }
  46. func (g *googleProvider) Profile(ctx context.Context, code, codeVerifier string) (*UserProfile, error) {
  47. conf := g.Config()
  48. token, err := conf.Exchange(ctx, code, oauth2.SetAuthURLParam("code_verifier", codeVerifier))
  49. if err != nil {
  50. return nil, fmt.Errorf("google: failed to exchange token: %w", err)
  51. }
  52. client := conf.Client(ctx, token)
  53. resp, err := client.Get(googleUserInfoURL)
  54. if err != nil {
  55. return nil, fmt.Errorf("google: failed to get user info: %w", err)
  56. }
  57. defer resp.Body.Close()
  58. if resp.StatusCode != http.StatusOK {
  59. return nil, fmt.Errorf("google: unexpected status code %d from userinfo endpoint", resp.StatusCode)
  60. }
  61. var user googleProfile
  62. decoder := json.NewDecoder(resp.Body)
  63. if err := decoder.Decode(&user); err != nil {
  64. return nil, fmt.Errorf("google: unable to unserialize Google profile: %w", err)
  65. }
  66. return &UserProfile{Key: g.UserExtraKey(), ID: user.Sub, Username: user.Email}, nil
  67. }
  68. func (g *googleProvider) PopulateUserCreationWithProfileID(user *model.UserCreationRequest, profile *UserProfile) {
  69. user.GoogleID = profile.ID
  70. }
  71. func (g *googleProvider) PopulateUserWithProfileID(user *model.User, profile *UserProfile) {
  72. user.GoogleID = profile.ID
  73. }
  74. func (g *googleProvider) UserProfileID(user *model.User) string {
  75. return user.GoogleID
  76. }
  77. func (g *googleProvider) UnsetUserProfileID(user *model.User) {
  78. user.GoogleID = ""
  79. }