mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-05-11 06:48:11 +00:00
4f7335ed73
* feat: add new logger * refactor: use one struct for context handling and cancellation * refactor: rework logging and config in controllers * refactor: rework logging and config in middlewares * refactor: rework logging and cancellation in services * refactor: rework cli logging * fix: improve logging in routines * feat: use sync groups for better cancellation * refactor: simplify middleware, controller and service init * tests: fix controller tests * tests: use require instead of assert where previous step is required * tests: fix middleware tests * tests: fix service tests * tests: fix context tests * fix: fix typos * feat: add option to enable or disable concurrent listeners * fix: assign public key correctly in oidc server * tests: fix don't try to test logger with char size * fix: coderabbit comments * tests: use filepath join instead of path join * fix: ensure unix socket shutdown doesn't run twice * chore: remove temp lint file
72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/tinyauthapp/tinyauth/internal/model"
|
|
"github.com/tinyauthapp/tinyauth/internal/utils/logger"
|
|
|
|
"slices"
|
|
|
|
"golang.org/x/oauth2"
|
|
)
|
|
|
|
type OAuthServiceImpl interface {
|
|
Name() string
|
|
ID() string
|
|
NewRandom() string
|
|
GetAuthURL(state string, verifier string) string
|
|
GetToken(code string, verifier string) (*oauth2.Token, error)
|
|
GetUserinfo(token *oauth2.Token) (*model.Claims, error)
|
|
}
|
|
|
|
type OAuthBrokerService struct {
|
|
log *logger.Logger
|
|
|
|
services map[string]OAuthServiceImpl
|
|
configs map[string]model.OAuthServiceConfig
|
|
}
|
|
|
|
var presets = map[string]func(config model.OAuthServiceConfig, ctx context.Context) *OAuthService{
|
|
"github": newGitHubOAuthService,
|
|
"google": newGoogleOAuthService,
|
|
}
|
|
|
|
func NewOAuthBrokerService(
|
|
log *logger.Logger,
|
|
configs map[string]model.OAuthServiceConfig,
|
|
ctx context.Context,
|
|
) *OAuthBrokerService {
|
|
service := &OAuthBrokerService{
|
|
log: log,
|
|
services: make(map[string]OAuthServiceImpl),
|
|
configs: configs,
|
|
}
|
|
|
|
for name, cfg := range configs {
|
|
if presetFunc, exists := presets[name]; exists {
|
|
service.services[name] = presetFunc(cfg, ctx)
|
|
service.log.App.Debug().Str("service", name).Msg("Loaded OAuth service from preset")
|
|
} else {
|
|
service.services[name] = NewOAuthService(cfg, name, ctx)
|
|
service.log.App.Debug().Str("service", name).Msg("Loaded OAuth service from custom config")
|
|
}
|
|
}
|
|
|
|
return service
|
|
}
|
|
|
|
func (broker *OAuthBrokerService) GetConfiguredServices() []string {
|
|
services := make([]string, 0, len(broker.services))
|
|
for name := range broker.services {
|
|
services = append(services, name)
|
|
}
|
|
slices.Sort(services)
|
|
return services
|
|
}
|
|
|
|
func (broker *OAuthBrokerService) GetService(name string) (OAuthServiceImpl, bool) {
|
|
service, exists := broker.services[name]
|
|
return service, exists
|
|
}
|