2017-11-23 07:22:33 +01:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
package oauth2 // import "miniflux.app/oauth2"
|
2017-11-23 07:22:33 +01:00
|
|
|
|
2020-03-08 03:45:19 +01:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2020-12-22 06:14:10 +01:00
|
|
|
|
2020-03-08 03:45:19 +01:00
|
|
|
"miniflux.app/logger"
|
|
|
|
)
|
2017-11-23 07:22:33 +01:00
|
|
|
|
|
|
|
// Manager handles OAuth2 providers.
|
|
|
|
type Manager struct {
|
|
|
|
providers map[string]Provider
|
|
|
|
}
|
|
|
|
|
2020-12-22 06:14:10 +01:00
|
|
|
// FindProvider returns the given provider.
|
|
|
|
func (m *Manager) FindProvider(name string) (Provider, error) {
|
2017-11-23 07:22:33 +01:00
|
|
|
if provider, found := m.providers[name]; found {
|
|
|
|
return provider, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("oauth2 provider not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddProvider add a new OAuth2 provider.
|
|
|
|
func (m *Manager) AddProvider(name string, provider Provider) {
|
|
|
|
m.providers[name] = provider
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewManager returns a new Manager.
|
2020-03-08 03:45:19 +01:00
|
|
|
func NewManager(ctx context.Context, clientID, clientSecret, redirectURL, oidcDiscoveryEndpoint string) *Manager {
|
2017-11-23 07:22:33 +01:00
|
|
|
m := &Manager{providers: make(map[string]Provider)}
|
|
|
|
m.AddProvider("google", newGoogleProvider(clientID, clientSecret, redirectURL))
|
2020-03-08 03:45:19 +01:00
|
|
|
|
|
|
|
if oidcDiscoveryEndpoint != "" {
|
|
|
|
if genericOidcProvider, err := newOidcProvider(ctx, clientID, clientSecret, redirectURL, oidcDiscoveryEndpoint); err != nil {
|
|
|
|
logger.Error("[OAuth2] failed to initialize OIDC provider: %v", err)
|
|
|
|
} else {
|
|
|
|
m.AddProvider("oidc", genericOidcProvider)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-23 07:22:33 +01:00
|
|
|
return m
|
|
|
|
}
|