2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-23 07:22:33 +01:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package oauth2 // import "miniflux.app/v2/internal/oauth2"
|
2017-11-23 07:22:33 +01:00
|
|
|
|
2020-03-08 03:45:19 +01:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2023-09-25 01:32:09 +02:00
|
|
|
"log/slog"
|
2020-03-08 03:45:19 +01:00
|
|
|
)
|
2017-11-23 07:22:33 +01:00
|
|
|
|
|
|
|
type Manager struct {
|
|
|
|
providers map[string]Provider
|
|
|
|
}
|
|
|
|
|
2020-12-22 06:14:10 +01:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) AddProvider(name string, provider Provider) {
|
|
|
|
m.providers[name] = provider
|
|
|
|
}
|
|
|
|
|
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)}
|
2023-09-03 06:35:10 +02:00
|
|
|
m.AddProvider("google", NewGoogleProvider(clientID, clientSecret, redirectURL))
|
2020-03-08 03:45:19 +01:00
|
|
|
|
|
|
|
if oidcDiscoveryEndpoint != "" {
|
2023-09-03 06:35:10 +02:00
|
|
|
if genericOidcProvider, err := NewOidcProvider(ctx, clientID, clientSecret, redirectURL, oidcDiscoveryEndpoint); err != nil {
|
2023-09-25 01:32:09 +02:00
|
|
|
slog.Error("Failed to initialize OIDC provider",
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
2020-03-08 03:45:19 +01:00
|
|
|
} else {
|
|
|
|
m.AddProvider("oidc", genericOidcProvider)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-23 07:22:33 +01:00
|
|
|
return m
|
|
|
|
}
|