oidc.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. "errors"
  7. "fmt"
  8. "miniflux.app/v2/internal/model"
  9. "github.com/coreos/go-oidc/v3/oidc"
  10. "golang.org/x/oauth2"
  11. )
  12. var (
  13. ErrEmptyUsername = errors.New("oidc: username is empty")
  14. )
  15. type oidcProvider struct {
  16. clientID string
  17. clientSecret string
  18. redirectURL string
  19. provider *oidc.Provider
  20. }
  21. func NewOidcProvider(ctx context.Context, clientID, clientSecret, redirectURL, discoveryEndpoint string) (*oidcProvider, error) {
  22. provider, err := oidc.NewProvider(ctx, discoveryEndpoint)
  23. if err != nil {
  24. return nil, fmt.Errorf(`oidc: failed to initialize provider %q: %w`, discoveryEndpoint, err)
  25. }
  26. return &oidcProvider{
  27. clientID: clientID,
  28. clientSecret: clientSecret,
  29. redirectURL: redirectURL,
  30. provider: provider,
  31. }, nil
  32. }
  33. func (o *oidcProvider) GetUserExtraKey() string {
  34. return "openid_connect_id"
  35. }
  36. func (o *oidcProvider) GetConfig() *oauth2.Config {
  37. return &oauth2.Config{
  38. RedirectURL: o.redirectURL,
  39. ClientID: o.clientID,
  40. ClientSecret: o.clientSecret,
  41. Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
  42. Endpoint: o.provider.Endpoint(),
  43. }
  44. }
  45. func (o *oidcProvider) GetProfile(ctx context.Context, code, codeVerifier string) (*Profile, error) {
  46. conf := o.GetConfig()
  47. token, err := conf.Exchange(ctx, code, oauth2.SetAuthURLParam("code_verifier", codeVerifier))
  48. if err != nil {
  49. return nil, fmt.Errorf(`oidc: failed to exchange token: %w`, err)
  50. }
  51. userInfo, err := o.provider.UserInfo(ctx, oauth2.StaticTokenSource(token))
  52. if err != nil {
  53. return nil, fmt.Errorf(`oidc: failed to get user info: %w`, err)
  54. }
  55. profile := &Profile{
  56. Key: o.GetUserExtraKey(),
  57. ID: userInfo.Subject,
  58. }
  59. var userClaims userClaims
  60. if err := userInfo.Claims(&userClaims); err != nil {
  61. return nil, fmt.Errorf(`oidc: failed to parse user claims: %w`, err)
  62. }
  63. for _, value := range []string{userClaims.Email, userClaims.PreferredUsername, userClaims.Name, userClaims.Profile} {
  64. if value != "" {
  65. profile.Username = value
  66. break
  67. }
  68. }
  69. if profile.Username == "" {
  70. return nil, ErrEmptyUsername
  71. }
  72. return profile, nil
  73. }
  74. func (o *oidcProvider) PopulateUserCreationWithProfileID(user *model.UserCreationRequest, profile *Profile) {
  75. user.OpenIDConnectID = profile.ID
  76. }
  77. func (o *oidcProvider) PopulateUserWithProfileID(user *model.User, profile *Profile) {
  78. user.OpenIDConnectID = profile.ID
  79. }
  80. func (o *oidcProvider) UnsetUserProfileID(user *model.User) {
  81. user.OpenIDConnectID = ""
  82. }
  83. type userClaims struct {
  84. Email string `json:"email"`
  85. Profile string `json:"profile"`
  86. Name string `json:"name"`
  87. PreferredUsername string `json:"preferred_username"`
  88. }