@@ -15,59 +15,56 @@ import (
15
15
16
16
"github.com/coreos/go-oidc/v3/oidc"
17
17
"github.com/gitpod-io/gitpod/common-go/log"
18
- "github.com/go-chi/chi/v5"
19
18
"golang.org/x/oauth2"
20
19
)
21
20
22
- type OIDCService struct {
23
- Handler chi.Router
24
-
25
- configsById map [string ]* OIDCClientConfig
21
+ type Service struct {
22
+ configsById map [string ]* ClientConfig
26
23
verifierByIssuer map [string ]* oidc.IDTokenVerifier
27
24
providerByIssuer map [string ]* oidc.Provider
28
25
}
29
26
30
- type OIDCClientConfig struct {
27
+ type ClientConfig struct {
31
28
ID string
32
29
Issuer string
33
30
OAuth2Config * oauth2.Config
34
31
OIDCConfig * oidc.Config
35
32
}
36
33
37
- type OIDCStartParams struct {
34
+ type StartParams struct {
38
35
State string
39
36
Nonce string
40
37
AuthCodeURL string
41
38
}
42
39
43
- type OIDCAuthResult struct {
40
+ type AuthResult struct {
44
41
IDToken * oidc.IDToken
45
42
}
46
43
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
+ }
53
50
}
54
51
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 {
57
54
provider , err := oidc .NewProvider (context .Background (), config .Issuer )
58
55
if err != nil {
59
56
return errors .New ("OIDC discovery failed: " + err .Error ())
60
57
}
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 )
63
60
}
64
61
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
67
64
return nil
68
65
}
69
66
70
- func (service * OIDCService ) GetStartParams (config * OIDCClientConfig ) (* OIDCStartParams , error ) {
67
+ func (s * Service ) GetStartParams (config * ClientConfig ) (* StartParams , error ) {
71
68
// TODO(at) state should be a JWT encoding a redirect location
72
69
// Using a random string to get the flow running.
73
70
state , err := randString (32 )
@@ -83,7 +80,7 @@ func (service *OIDCService) GetStartParams(config *OIDCClientConfig) (*OIDCStart
83
80
// Nonce is the single option passed on to configure the consent page ATM.
84
81
authCodeURL := config .OAuth2Config .AuthCodeURL (state , oidc .Nonce (nonce ))
85
82
86
- return & OIDCStartParams {
83
+ return & StartParams {
87
84
AuthCodeURL : authCodeURL ,
88
85
State : state ,
89
86
Nonce : nonce ,
@@ -98,7 +95,7 @@ func randString(size int) (string, error) {
98
95
return base64 .RawURLEncoding .EncodeToString (b ), nil
99
96
}
100
97
101
- func (service * OIDCService ) GetClientConfigFromRequest (r * http.Request ) (* OIDCClientConfig , error ) {
98
+ func (s * Service ) GetClientConfigFromRequest (r * http.Request ) (* ClientConfig , error ) {
102
99
issuerParam := r .URL .Query ().Get ("issuer" )
103
100
if issuerParam == "" {
104
101
return nil , errors .New ("issuer param not specified" )
@@ -109,21 +106,21 @@ func (service *OIDCService) GetClientConfigFromRequest(r *http.Request) (*OIDCCl
109
106
}
110
107
log .WithField ("issuer" , issuer ).Trace ("at GetClientConfigFromRequest" )
111
108
112
- for _ , value := range service .configsById {
109
+ for _ , value := range s .configsById {
113
110
if value .Issuer == issuer {
114
111
return value , nil
115
112
}
116
113
}
117
114
return nil , errors .New ("failed to find OIDC config for request" )
118
115
}
119
116
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 ) {
121
118
rawIDToken , ok := oauth2Result .OAuth2Token .Extra ("id_token" ).(string )
122
119
if ! ok {
123
120
return nil , errors .New ("id_token not found" )
124
121
}
125
122
126
- verifier := service .verifierByIssuer [issuer ]
123
+ verifier := s .verifierByIssuer [issuer ]
127
124
if verifier == nil {
128
125
return nil , errors .New ("verifier not found" )
129
126
}
@@ -136,7 +133,7 @@ func (service *OIDCService) Authenticate(ctx context.Context, oauth2Result *OAut
136
133
if idToken .Nonce != nonceCookieValue {
137
134
return nil , errors .New ("nonce mismatch" )
138
135
}
139
- return & OIDCAuthResult {
136
+ return & AuthResult {
140
137
IDToken : idToken ,
141
138
}, nil
142
139
}
0 commit comments