Skip to content

Commit

Permalink
[oidc] Cleanup registration of route handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
easyCZ authored and roboquat committed Dec 19, 2022
1 parent 32b2674 commit 4aacec9
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions components/iam/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,57 +28,58 @@ func Start(logger *logrus.Entry, version string, cfg *config.ServiceConfig) erro
return fmt.Errorf("failed to initialize IAM server: %w", err)
}

// All root requests are handled by our router
rootHandler, err := registerRootRouter(srv)
oidcService := oidc.NewOIDCService()
err = register(srv, oidcService)
if err != nil {
return fmt.Errorf("failed to register services to iam server")
}

// Requests to /oidc/* are handled by oidc.Router
oidcService := oidc.NewOIDCService()
rootHandler.Mount("/oidc", oidc.Router(oidcService))

// TODO(at) remove the demo config after start sync'ing with DB
err = loadTestConfig(oidcService, cfg)
clientConfig, err := loadTestConfig(cfg.OIDCClientsConfigFile)
if err != nil {
return fmt.Errorf("failed to load test config")
}

err = oidcService.AddClientConfig(clientConfig)
if err != nil {
return fmt.Errorf("failed to add client config to oidc service: %w", err)
}

if listenErr := srv.ListenAndServe(); listenErr != nil {
return fmt.Errorf("failed to serve iam server: %w", listenErr)
}

return nil
}

func registerRootRouter(srv *baseserver.Server) (*chi.Mux, error) {
rootHandler := chi.NewRouter()
func register(srv *baseserver.Server, oidcSvc *oidc.OIDCService) error {
root := chi.NewRouter()

srv.HTTPMux().Handle("/", rootHandler)
return rootHandler, nil
root.Mount("/oidc", oidc.Router(oidcSvc))

// All root requests are handled by our router
srv.HTTPMux().Handle("/", root)
return nil
}

// TODO(at) remove the demo config after start sync'ing with DB
func loadTestConfig(oidcService *oidc.OIDCService, cfg *config.ServiceConfig) error {
testConfig, err := oidc.ReadDemoConfigFromFile(cfg.OIDCClientsConfigFile)
func loadTestConfig(clientsConfigFilePath string) (*oidc.OIDCClientConfig, error) {
testConfig, err := oidc.ReadDemoConfigFromFile(clientsConfigFilePath)
if err != nil {
return fmt.Errorf("failed to read test config: %w", err)
}
oidcConfig := &goidc.Config{
ClientID: testConfig.ClientID,
return nil, fmt.Errorf("failed to read test config: %w", err)
}
oauth2Config := &oauth2.Config{
ClientID: testConfig.ClientID,
ClientSecret: testConfig.ClientSecret,
RedirectURL: testConfig.RedirectURL,
Scopes: []string{goidc.ScopeOpenID, "profile", "email"},
}
clientConfig := &oidc.OIDCClientConfig{
Issuer: testConfig.Issuer,
ID: "R4ND0M1D",
OAuth2Config: oauth2Config,
OIDCConfig: oidcConfig,
}
err = oidcService.AddClientConfig(clientConfig)
return err

return &oidc.OIDCClientConfig{
Issuer: testConfig.Issuer,
ID: "R4ND0M1D",
OAuth2Config: &oauth2.Config{
ClientID: testConfig.ClientID,
ClientSecret: testConfig.ClientSecret,
RedirectURL: testConfig.RedirectURL,
Scopes: []string{goidc.ScopeOpenID, "profile", "email"},
},
OIDCConfig: &goidc.Config{
ClientID: testConfig.ClientID,
},
}, nil
}

0 comments on commit 4aacec9

Please sign in to comment.