Skip to content

Commit 4aacec9

Browse files
easyCZroboquat
authored andcommitted
[oidc] Cleanup registration of route handlers
1 parent 32b2674 commit 4aacec9

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

components/iam/pkg/server/server.go

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,57 +28,58 @@ func Start(logger *logrus.Entry, version string, cfg *config.ServiceConfig) erro
2828
return fmt.Errorf("failed to initialize IAM server: %w", err)
2929
}
3030

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

37-
// Requests to /oidc/* are handled by oidc.Router
38-
oidcService := oidc.NewOIDCService()
39-
rootHandler.Mount("/oidc", oidc.Router(oidcService))
40-
4137
// TODO(at) remove the demo config after start sync'ing with DB
42-
err = loadTestConfig(oidcService, cfg)
38+
clientConfig, err := loadTestConfig(cfg.OIDCClientsConfigFile)
4339
if err != nil {
4440
return fmt.Errorf("failed to load test config")
4541
}
4642

43+
err = oidcService.AddClientConfig(clientConfig)
44+
if err != nil {
45+
return fmt.Errorf("failed to add client config to oidc service: %w", err)
46+
}
47+
4748
if listenErr := srv.ListenAndServe(); listenErr != nil {
4849
return fmt.Errorf("failed to serve iam server: %w", listenErr)
4950
}
5051

5152
return nil
5253
}
5354

54-
func registerRootRouter(srv *baseserver.Server) (*chi.Mux, error) {
55-
rootHandler := chi.NewRouter()
55+
func register(srv *baseserver.Server, oidcSvc *oidc.OIDCService) error {
56+
root := chi.NewRouter()
5657

57-
srv.HTTPMux().Handle("/", rootHandler)
58-
return rootHandler, nil
58+
root.Mount("/oidc", oidc.Router(oidcSvc))
59+
60+
// All root requests are handled by our router
61+
srv.HTTPMux().Handle("/", root)
62+
return nil
5963
}
6064

6165
// TODO(at) remove the demo config after start sync'ing with DB
62-
func loadTestConfig(oidcService *oidc.OIDCService, cfg *config.ServiceConfig) error {
63-
testConfig, err := oidc.ReadDemoConfigFromFile(cfg.OIDCClientsConfigFile)
66+
func loadTestConfig(clientsConfigFilePath string) (*oidc.OIDCClientConfig, error) {
67+
testConfig, err := oidc.ReadDemoConfigFromFile(clientsConfigFilePath)
6468
if err != nil {
65-
return fmt.Errorf("failed to read test config: %w", err)
66-
}
67-
oidcConfig := &goidc.Config{
68-
ClientID: testConfig.ClientID,
69+
return nil, fmt.Errorf("failed to read test config: %w", err)
6970
}
70-
oauth2Config := &oauth2.Config{
71-
ClientID: testConfig.ClientID,
72-
ClientSecret: testConfig.ClientSecret,
73-
RedirectURL: testConfig.RedirectURL,
74-
Scopes: []string{goidc.ScopeOpenID, "profile", "email"},
75-
}
76-
clientConfig := &oidc.OIDCClientConfig{
77-
Issuer: testConfig.Issuer,
78-
ID: "R4ND0M1D",
79-
OAuth2Config: oauth2Config,
80-
OIDCConfig: oidcConfig,
81-
}
82-
err = oidcService.AddClientConfig(clientConfig)
83-
return err
71+
72+
return &oidc.OIDCClientConfig{
73+
Issuer: testConfig.Issuer,
74+
ID: "R4ND0M1D",
75+
OAuth2Config: &oauth2.Config{
76+
ClientID: testConfig.ClientID,
77+
ClientSecret: testConfig.ClientSecret,
78+
RedirectURL: testConfig.RedirectURL,
79+
Scopes: []string{goidc.ScopeOpenID, "profile", "email"},
80+
},
81+
OIDCConfig: &goidc.Config{
82+
ClientID: testConfig.ClientID,
83+
},
84+
}, nil
8485
}

0 commit comments

Comments
 (0)