oidc.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // Use the first non-empty value from the claims to set the username.
  64. // The order of preference is: preferred_username, email, name, profile.
  65. for _, value := range []string{userClaims.PreferredUsername, userClaims.Email, userClaims.Name, userClaims.Profile} {
  66. if value != "" {
  67. profile.Username = value
  68. break
  69. }
  70. }
  71. if profile.Username == "" {
  72. return nil, ErrEmptyUsername
  73. }
  74. return profile, nil
  75. }
  76. func (o *oidcProvider) PopulateUserCreationWithProfileID(user *model.UserCreationRequest, profile *Profile) {
  77. user.OpenIDConnectID = profile.ID
  78. }
  79. func (o *oidcProvider) PopulateUserWithProfileID(user *model.User, profile *Profile) {
  80. user.OpenIDConnectID = profile.ID
  81. }
  82. func (o *oidcProvider) UnsetUserProfileID(user *model.User) {
  83. user.OpenIDConnectID = ""
  84. }
  85. type userClaims struct {
  86. Email string `json:"email"`
  87. Profile string `json:"profile"`
  88. Name string `json:"name"`
  89. PreferredUsername string `json:"preferred_username"`
  90. }