5
5
"crypto/sha256"
6
6
"encoding/base64"
7
7
"encoding/json"
8
+ "errors"
8
9
"fmt"
9
10
"net/http"
10
11
"net/url"
@@ -13,7 +14,9 @@ import (
13
14
14
15
"golang.org/x/oauth2"
15
16
17
+ authv1 "k8s.io/api/authentication/v1"
16
18
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19
+ authenticationv1 "k8s.io/client-go/kubernetes/typed/authentication/v1"
17
20
"k8s.io/client-go/rest"
18
21
"k8s.io/klog/v2"
19
22
@@ -220,6 +223,7 @@ func (o *openShiftAuth) logout(w http.ResponseWriter, r *http.Request) {
220
223
func (o * openShiftAuth ) LogoutRedirectURL () string {
221
224
return o .logoutRedirectOverride
222
225
}
226
+
223
227
func (o * openShiftAuth ) Authenticate (_ http.ResponseWriter , r * http.Request ) (* auth.User , error ) {
224
228
token , err := sessions .GetSessionTokenFromCookie (r )
225
229
if err != nil {
@@ -261,3 +265,36 @@ func tokenToObjectName(token string) string {
261
265
h := sha256 .Sum256 ([]byte (name ))
262
266
return sha256Prefix + base64 .RawURLEncoding .EncodeToString (h [0 :])
263
267
}
268
+
269
+ func (o * openShiftAuth ) ReviewToken (r * http.Request , tokenReviewClient authenticationv1.TokenReviewInterface ) error {
270
+ // This is only sufficient because this token handler stores the actual
271
+ // access token in the session token cookie
272
+ token , err := sessions .GetSessionTokenFromCookie (r )
273
+ if err != nil {
274
+ return fmt .Errorf ("failed to get session token: %v" , err )
275
+ }
276
+
277
+ tokenReview := & authv1.TokenReview {
278
+ TypeMeta : metav1.TypeMeta {
279
+ APIVersion : "authentication.k8s.io/v1" ,
280
+ Kind : "TokenReview" ,
281
+ },
282
+ Spec : authv1.TokenReviewSpec {
283
+ Token : token ,
284
+ },
285
+ }
286
+
287
+ completedTokenReview , err := tokenReviewClient .Create (r .Context (), tokenReview , metav1.CreateOptions {})
288
+ if err != nil {
289
+ return fmt .Errorf ("failed to create TokenReview, %v" , err )
290
+ }
291
+
292
+ // Check if the token is authenticated
293
+ if ! completedTokenReview .Status .Authenticated {
294
+ if completedTokenReview .Status .Error != "" {
295
+ return errors .New (completedTokenReview .Status .Error )
296
+ }
297
+ return errors .New ("failed to authenticate the token, unknown error" )
298
+ }
299
+ return nil
300
+ }
0 commit comments