Skip to content

Commit ac2a99f

Browse files
committed
oauth2: update existing loginMethod implementations to have a ReviewToken method
Signed-off-by: Bryce Palmer <[email protected]>
1 parent 51ee670 commit ac2a99f

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

pkg/auth/oauth2/auth_oidc.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@ package oauth2
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"net/http"
78
"sync"
89
"time"
910

1011
oidc "github.com/coreos/go-oidc"
1112
"golang.org/x/oauth2"
13+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1214
"k8s.io/client-go/rest"
1315

1416
"github.com/openshift/console/pkg/auth"
1517
"github.com/openshift/console/pkg/auth/sessions"
1618
"github.com/openshift/console/pkg/serverutils/asynccache"
19+
authv1 "k8s.io/api/authentication/v1"
20+
authenticationv1 "k8s.io/client-go/kubernetes/typed/authentication/v1"
1721
)
1822

1923
type oauth2ConfigConstructor func(oauth2.Endpoint) *oauth2.Config
@@ -181,3 +185,37 @@ func (o *oidcAuth) LogoutRedirectURL() string {
181185
func (o *oidcAuth) oauth2Config() *oauth2.Config {
182186
return o.oidcConfig.constructOAuth2Config(o.providerCache.GetItem().Endpoint())
183187
}
188+
189+
func (o *oidcAuth) ReviewToken(r *http.Request, tokenReviewClient authenticationv1.TokenReviewInterface) error {
190+
loginState, err := o.sessions.GetSession(nil, r)
191+
if err != nil {
192+
return fmt.Errorf("getting session state: %w", err)
193+
}
194+
if loginState == nil {
195+
return errors.New("no login state found for session")
196+
}
197+
198+
tokenReview := &authv1.TokenReview{
199+
TypeMeta: metav1.TypeMeta{
200+
APIVersion: "authentication.k8s.io/v1",
201+
Kind: "TokenReview",
202+
},
203+
Spec: authv1.TokenReviewSpec{
204+
Token: loginState.AccessToken(),
205+
},
206+
}
207+
208+
completedTokenReview, err := tokenReviewClient.Create(r.Context(), tokenReview, metav1.CreateOptions{})
209+
if err != nil {
210+
return fmt.Errorf("failed to create TokenReview, %v", err)
211+
}
212+
213+
// Check if the token is authenticated
214+
if !completedTokenReview.Status.Authenticated {
215+
if completedTokenReview.Status.Error != "" {
216+
return errors.New(completedTokenReview.Status.Error)
217+
}
218+
return errors.New("failed to authenticate the token, unknown error")
219+
}
220+
return nil
221+
}

pkg/auth/oauth2/auth_openshift.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/sha256"
66
"encoding/base64"
77
"encoding/json"
8+
"errors"
89
"fmt"
910
"net/http"
1011
"net/url"
@@ -13,7 +14,9 @@ import (
1314

1415
"golang.org/x/oauth2"
1516

17+
authv1 "k8s.io/api/authentication/v1"
1618
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19+
authenticationv1 "k8s.io/client-go/kubernetes/typed/authentication/v1"
1720
"k8s.io/client-go/rest"
1821
"k8s.io/klog/v2"
1922

@@ -220,6 +223,7 @@ func (o *openShiftAuth) logout(w http.ResponseWriter, r *http.Request) {
220223
func (o *openShiftAuth) LogoutRedirectURL() string {
221224
return o.logoutRedirectOverride
222225
}
226+
223227
func (o *openShiftAuth) Authenticate(_ http.ResponseWriter, r *http.Request) (*auth.User, error) {
224228
token, err := sessions.GetSessionTokenFromCookie(r)
225229
if err != nil {
@@ -261,3 +265,36 @@ func tokenToObjectName(token string) string {
261265
h := sha256.Sum256([]byte(name))
262266
return sha256Prefix + base64.RawURLEncoding.EncodeToString(h[0:])
263267
}
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

Comments
 (0)