Skip to content

Commit 33b135f

Browse files
easyCZroboquat
authored andcommitted
[iam] Drop OIDC prefix from service, remove unused handler
1 parent 2698a7a commit 33b135f

File tree

5 files changed

+44
-47
lines changed

5 files changed

+44
-47
lines changed

components/iam/pkg/oidc/oauth2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func OAuth2Middleware(next http.Handler) http.Handler {
2323
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
2424
log.Trace("at oauth2 middleware")
2525
ctx := r.Context()
26-
config, ok := ctx.Value(keyOIDCClientConfig{}).(OIDCClientConfig)
26+
config, ok := ctx.Value(keyOIDCClientConfig{}).(ClientConfig)
2727
if !ok {
2828
http.Error(rw, "config not found", http.StatusInternalServerError)
2929
return

components/iam/pkg/oidc/router.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/go-chi/chi/v5"
1717
)
1818

19-
func Router(oidcService *OIDCService) *chi.Mux {
19+
func Router(oidcService *Service) *chi.Mux {
2020
router := chi.NewRouter()
2121

2222
router.Route("/start", func(r chi.Router) {
@@ -39,18 +39,18 @@ const (
3939
nonceCookieName = "nonce"
4040
)
4141

42-
func (oidcService *OIDCService) getStartHandler() http.HandlerFunc {
42+
func (s *Service) getStartHandler() http.HandlerFunc {
4343
return func(rw http.ResponseWriter, r *http.Request) {
4444
log.Trace("at start handler")
4545

4646
ctx := r.Context()
47-
config, ok := ctx.Value(keyOIDCClientConfig{}).(OIDCClientConfig)
47+
config, ok := ctx.Value(keyOIDCClientConfig{}).(ClientConfig)
4848
if !ok {
4949
http.Error(rw, "config not found", http.StatusInternalServerError)
5050
return
5151
}
5252

53-
startParams, err := oidcService.GetStartParams(&config)
53+
startParams, err := s.GetStartParams(&config)
5454
if err != nil {
5555
http.Error(rw, "failed to start auth flow", http.StatusInternalServerError)
5656
return
@@ -75,12 +75,12 @@ func newCallbackCookie(r *http.Request, name string, value string) *http.Cookie
7575
}
7676

7777
// The config middleware is responsible to retrieve the client config suitable for request
78-
func (oidcService *OIDCService) clientConfigMiddleware() func(http.Handler) http.Handler {
78+
func (s *Service) clientConfigMiddleware() func(http.Handler) http.Handler {
7979
return func(next http.Handler) http.Handler {
8080
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
8181
log.Trace("at config middleware")
8282

83-
config, err := oidcService.GetClientConfigFromRequest(r)
83+
config, err := s.GetClientConfigFromRequest(r)
8484
if err != nil {
8585
log.Warn("client config not found: " + err.Error())
8686
http.Error(rw, "config not found", http.StatusNotFound)
@@ -94,12 +94,12 @@ func (oidcService *OIDCService) clientConfigMiddleware() func(http.Handler) http
9494
}
9595

9696
// The OIDC callback handler depends on the state produced in the OAuth2 middleware
97-
func (oidcService *OIDCService) getCallbackHandler() http.HandlerFunc {
97+
func (s *Service) getCallbackHandler() http.HandlerFunc {
9898
return func(rw http.ResponseWriter, r *http.Request) {
9999
log.Trace("at callback handler")
100100

101101
ctx := r.Context()
102-
config, ok := ctx.Value(keyOIDCClientConfig{}).(OIDCClientConfig)
102+
config, ok := ctx.Value(keyOIDCClientConfig{}).(ClientConfig)
103103
if !ok {
104104
http.Error(rw, "config not found", http.StatusInternalServerError)
105105
return
@@ -117,7 +117,7 @@ func (oidcService *OIDCService) getCallbackHandler() http.HandlerFunc {
117117
return
118118
}
119119

120-
result, err := oidcService.Authenticate(ctx, &oauth2Result,
120+
result, err := s.Authenticate(ctx, &oauth2Result,
121121
config.Issuer, nonceCookie.Value)
122122
if err != nil {
123123
http.Error(rw, "OIDC authentication failed", http.StatusInternalServerError)

components/iam/pkg/oidc/service.go

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,59 +15,56 @@ import (
1515

1616
"github.com/coreos/go-oidc/v3/oidc"
1717
"github.com/gitpod-io/gitpod/common-go/log"
18-
"github.com/go-chi/chi/v5"
1918
"golang.org/x/oauth2"
2019
)
2120

22-
type OIDCService struct {
23-
Handler chi.Router
24-
25-
configsById map[string]*OIDCClientConfig
21+
type Service struct {
22+
configsById map[string]*ClientConfig
2623
verifierByIssuer map[string]*oidc.IDTokenVerifier
2724
providerByIssuer map[string]*oidc.Provider
2825
}
2926

30-
type OIDCClientConfig struct {
27+
type ClientConfig struct {
3128
ID string
3229
Issuer string
3330
OAuth2Config *oauth2.Config
3431
OIDCConfig *oidc.Config
3532
}
3633

37-
type OIDCStartParams struct {
34+
type StartParams struct {
3835
State string
3936
Nonce string
4037
AuthCodeURL string
4138
}
4239

43-
type OIDCAuthResult struct {
40+
type AuthResult struct {
4441
IDToken *oidc.IDToken
4542
}
4643

47-
func NewOIDCService() *OIDCService {
48-
var s OIDCService
49-
s.configsById = make(map[string]*OIDCClientConfig)
50-
s.verifierByIssuer = make(map[string]*oidc.IDTokenVerifier)
51-
s.providerByIssuer = make(map[string]*oidc.Provider)
52-
return &s
44+
func NewService() *Service {
45+
return &Service{
46+
configsById: map[string]*ClientConfig{},
47+
verifierByIssuer: map[string]*oidc.IDTokenVerifier{},
48+
providerByIssuer: map[string]*oidc.Provider{},
49+
}
5350
}
5451

55-
func (service *OIDCService) AddClientConfig(config *OIDCClientConfig) error {
56-
if service.providerByIssuer[config.Issuer] == nil {
52+
func (s *Service) AddClientConfig(config *ClientConfig) error {
53+
if s.providerByIssuer[config.Issuer] == nil {
5754
provider, err := oidc.NewProvider(context.Background(), config.Issuer)
5855
if err != nil {
5956
return errors.New("OIDC discovery failed: " + err.Error())
6057
}
61-
service.providerByIssuer[config.Issuer] = provider
62-
service.verifierByIssuer[config.Issuer] = provider.Verifier(config.OIDCConfig)
58+
s.providerByIssuer[config.Issuer] = provider
59+
s.verifierByIssuer[config.Issuer] = provider.Verifier(config.OIDCConfig)
6360
}
6461

65-
config.OAuth2Config.Endpoint = service.providerByIssuer[config.Issuer].Endpoint()
66-
service.configsById[config.ID] = config
62+
config.OAuth2Config.Endpoint = s.providerByIssuer[config.Issuer].Endpoint()
63+
s.configsById[config.ID] = config
6764
return nil
6865
}
6966

70-
func (service *OIDCService) GetStartParams(config *OIDCClientConfig) (*OIDCStartParams, error) {
67+
func (s *Service) GetStartParams(config *ClientConfig) (*StartParams, error) {
7168
// TODO(at) state should be a JWT encoding a redirect location
7269
// Using a random string to get the flow running.
7370
state, err := randString(32)
@@ -83,7 +80,7 @@ func (service *OIDCService) GetStartParams(config *OIDCClientConfig) (*OIDCStart
8380
// Nonce is the single option passed on to configure the consent page ATM.
8481
authCodeURL := config.OAuth2Config.AuthCodeURL(state, oidc.Nonce(nonce))
8582

86-
return &OIDCStartParams{
83+
return &StartParams{
8784
AuthCodeURL: authCodeURL,
8885
State: state,
8986
Nonce: nonce,
@@ -98,7 +95,7 @@ func randString(size int) (string, error) {
9895
return base64.RawURLEncoding.EncodeToString(b), nil
9996
}
10097

101-
func (service *OIDCService) GetClientConfigFromRequest(r *http.Request) (*OIDCClientConfig, error) {
98+
func (s *Service) GetClientConfigFromRequest(r *http.Request) (*ClientConfig, error) {
10299
issuerParam := r.URL.Query().Get("issuer")
103100
if issuerParam == "" {
104101
return nil, errors.New("issuer param not specified")
@@ -109,21 +106,21 @@ func (service *OIDCService) GetClientConfigFromRequest(r *http.Request) (*OIDCCl
109106
}
110107
log.WithField("issuer", issuer).Trace("at GetClientConfigFromRequest")
111108

112-
for _, value := range service.configsById {
109+
for _, value := range s.configsById {
113110
if value.Issuer == issuer {
114111
return value, nil
115112
}
116113
}
117114
return nil, errors.New("failed to find OIDC config for request")
118115
}
119116

120-
func (service *OIDCService) Authenticate(ctx context.Context, oauth2Result *OAuth2Result, issuer string, nonceCookieValue string) (*OIDCAuthResult, error) {
117+
func (s *Service) Authenticate(ctx context.Context, oauth2Result *OAuth2Result, issuer string, nonceCookieValue string) (*AuthResult, error) {
121118
rawIDToken, ok := oauth2Result.OAuth2Token.Extra("id_token").(string)
122119
if !ok {
123120
return nil, errors.New("id_token not found")
124121
}
125122

126-
verifier := service.verifierByIssuer[issuer]
123+
verifier := s.verifierByIssuer[issuer]
127124
if verifier == nil {
128125
return nil, errors.New("verifier not found")
129126
}
@@ -136,7 +133,7 @@ func (service *OIDCService) Authenticate(ctx context.Context, oauth2Result *OAut
136133
if idToken.Nonce != nonceCookieValue {
137134
return nil, errors.New("nonce mismatch")
138135
}
139-
return &OIDCAuthResult{
136+
return &AuthResult{
140137
IDToken: idToken,
141138
}, nil
142139
}

components/iam/pkg/oidc/service_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ func TestGetStartParams(t *testing.T) {
2525
issuerG = "https://accounts.google.com"
2626
clientID = "client-id-123"
2727
)
28-
service := NewOIDCService()
29-
config := &OIDCClientConfig{
28+
service := NewService()
29+
config := &ClientConfig{
3030
ID: "google-1",
3131
Issuer: issuerG,
3232
OIDCConfig: &oidc.Config{},
@@ -83,8 +83,8 @@ func TestGetClientConfigFromRequest(t *testing.T) {
8383
},
8484
}
8585

86-
service := NewOIDCService()
87-
err = service.AddClientConfig(&OIDCClientConfig{
86+
service := NewService()
87+
err = service.AddClientConfig(&ClientConfig{
8888
ID: "google-1",
8989
Issuer: issuer,
9090
OIDCConfig: &oidc.Config{},
@@ -112,8 +112,8 @@ func TestAuthenticate_nonce_check(t *testing.T) {
112112
issuer, err := setupFakeIdP(t)
113113
require.NoError(t, err)
114114

115-
service := NewOIDCService()
116-
err = service.AddClientConfig(&OIDCClientConfig{
115+
service := NewService()
116+
err = service.AddClientConfig(&ClientConfig{
117117
ID: "google-1",
118118
Issuer: issuer,
119119
OIDCConfig: &oidc.Config{

components/iam/pkg/server/server.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ 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-
oidcService := oidc.NewOIDCService()
31+
oidcService := oidc.NewService()
3232
err = register(srv, oidcService)
3333
if err != nil {
3434
return fmt.Errorf("failed to register services to iam server")
@@ -52,7 +52,7 @@ func Start(logger *logrus.Entry, version string, cfg *config.ServiceConfig) erro
5252
return nil
5353
}
5454

55-
func register(srv *baseserver.Server, oidcSvc *oidc.OIDCService) error {
55+
func register(srv *baseserver.Server, oidcSvc *oidc.Service) error {
5656
root := chi.NewRouter()
5757

5858
root.Mount("/oidc", oidc.Router(oidcSvc))
@@ -63,13 +63,13 @@ func register(srv *baseserver.Server, oidcSvc *oidc.OIDCService) error {
6363
}
6464

6565
// TODO(at) remove the demo config after start sync'ing with DB
66-
func loadTestConfig(clientsConfigFilePath string) (*oidc.OIDCClientConfig, error) {
66+
func loadTestConfig(clientsConfigFilePath string) (*oidc.ClientConfig, error) {
6767
testConfig, err := oidc.ReadDemoConfigFromFile(clientsConfigFilePath)
6868
if err != nil {
6969
return nil, fmt.Errorf("failed to read test config: %w", err)
7070
}
7171

72-
return &oidc.OIDCClientConfig{
72+
return &oidc.ClientConfig{
7373
Issuer: testConfig.Issuer,
7474
ID: "R4ND0M1D",
7575
OAuth2Config: &oauth2.Config{

0 commit comments

Comments
 (0)