google.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package oauth2 // import "miniflux.app/oauth2"
  5. import (
  6. "context"
  7. "encoding/json"
  8. "fmt"
  9. "miniflux.app/model"
  10. "golang.org/x/oauth2"
  11. )
  12. type googleProfile struct {
  13. Sub string `json:"sub"`
  14. Email string `json:"email"`
  15. }
  16. type googleProvider struct {
  17. clientID string
  18. clientSecret string
  19. redirectURL string
  20. }
  21. func (g *googleProvider) GetUserExtraKey() string {
  22. return "google_id"
  23. }
  24. func (g *googleProvider) GetRedirectURL(state string) string {
  25. return g.config().AuthCodeURL(state)
  26. }
  27. func (g *googleProvider) GetProfile(ctx context.Context, code string) (*Profile, error) {
  28. conf := g.config()
  29. token, err := conf.Exchange(ctx, code)
  30. if err != nil {
  31. return nil, err
  32. }
  33. client := conf.Client(ctx, token)
  34. resp, err := client.Get("https://www.googleapis.com/oauth2/v3/userinfo")
  35. if err != nil {
  36. return nil, err
  37. }
  38. defer resp.Body.Close()
  39. var user googleProfile
  40. decoder := json.NewDecoder(resp.Body)
  41. if err := decoder.Decode(&user); err != nil {
  42. return nil, fmt.Errorf("oauth2: unable to unserialize google profile: %v", err)
  43. }
  44. profile := &Profile{Key: g.GetUserExtraKey(), ID: user.Sub, Username: user.Email}
  45. return profile, nil
  46. }
  47. func (g *googleProvider) PopulateUserCreationWithProfileID(user *model.UserCreationRequest, profile *Profile) {
  48. user.GoogleID = profile.ID
  49. }
  50. func (g *googleProvider) PopulateUserWithProfileID(user *model.User, profile *Profile) {
  51. user.GoogleID = profile.ID
  52. }
  53. func (g *googleProvider) UnsetUserProfileID(user *model.User) {
  54. user.GoogleID = ""
  55. }
  56. func (g *googleProvider) config() *oauth2.Config {
  57. return &oauth2.Config{
  58. RedirectURL: g.redirectURL,
  59. ClientID: g.clientID,
  60. ClientSecret: g.clientSecret,
  61. Scopes: []string{"email"},
  62. Endpoint: oauth2.Endpoint{
  63. AuthURL: "https://accounts.google.com/o/oauth2/auth",
  64. TokenURL: "https://accounts.google.com/o/oauth2/token",
  65. },
  66. }
  67. }
  68. func newGoogleProvider(clientID, clientSecret, redirectURL string) *googleProvider {
  69. return &googleProvider{clientID: clientID, clientSecret: clientSecret, redirectURL: redirectURL}
  70. }