oidc.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. "github.com/coreos/go-oidc"
  8. "golang.org/x/oauth2"
  9. )
  10. type oidcProvider struct {
  11. clientID string
  12. clientSecret string
  13. redirectURL string
  14. provider *oidc.Provider
  15. }
  16. func (o oidcProvider) GetUserExtraKey() string {
  17. return "oidc_id" // FIXME? add extra options key to allow multiple OIDC providers each with their own extra key?
  18. }
  19. func (o oidcProvider) GetRedirectURL(state string) string {
  20. return o.config().AuthCodeURL(state)
  21. }
  22. func (o oidcProvider) GetProfile(ctx context.Context, code string) (*Profile, error) {
  23. conf := o.config()
  24. token, err := conf.Exchange(ctx, code)
  25. if err != nil {
  26. return nil, err
  27. }
  28. userInfo, err := o.provider.UserInfo(ctx, oauth2.StaticTokenSource(token))
  29. if err != nil {
  30. return nil, err
  31. }
  32. profile := &Profile{Key: o.GetUserExtraKey(), ID: userInfo.Subject, Username: userInfo.Email}
  33. return profile, nil
  34. }
  35. func (o oidcProvider) config() *oauth2.Config {
  36. return &oauth2.Config{
  37. RedirectURL: o.redirectURL,
  38. ClientID: o.clientID,
  39. ClientSecret: o.clientSecret,
  40. Scopes: []string{"openid", "email"},
  41. Endpoint: o.provider.Endpoint(),
  42. }
  43. }
  44. func newOidcProvider(ctx context.Context, clientID, clientSecret, redirectURL, discoveryEndpoint string) (*oidcProvider, error) {
  45. provider, err := oidc.NewProvider(ctx, discoveryEndpoint)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return &oidcProvider{clientID: clientID, clientSecret: clientSecret, redirectURL: redirectURL, provider: provider}, nil
  50. }