|
| 1 | +package internaloauth |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + userapi "github.com/openshift/api/user/v1" |
| 8 | + userfake "github.com/openshift/client-go/user/clientset/versioned/fake" |
| 9 | + oapi "github.com/openshift/origin/pkg/oauth/apis/oauth" |
| 10 | + oauthfake "github.com/openshift/origin/pkg/oauth/generated/internalclientset/fake" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAuthenticateTokenExpired(t *testing.T) { |
| 15 | + fakeOAuthClient := oauthfake.NewSimpleClientset( |
| 16 | + // expired token that had a lifetime of 10 minutes |
| 17 | + &oapi.OAuthAccessToken{ |
| 18 | + ObjectMeta: metav1.ObjectMeta{Name: "token1", CreationTimestamp: metav1.Time{Time: time.Now().Add(-1 * time.Hour)}}, |
| 19 | + ExpiresIn: 600, |
| 20 | + UserName: "foo", |
| 21 | + }, |
| 22 | + // non-expired token that has a lifetime of 10 minutes, but has a non-nil deletion timestamp |
| 23 | + &oapi.OAuthAccessToken{ |
| 24 | + ObjectMeta: metav1.ObjectMeta{Name: "token2", CreationTimestamp: metav1.Time{Time: time.Now()}, DeletionTimestamp: &metav1.Time{}}, |
| 25 | + ExpiresIn: 600, |
| 26 | + UserName: "foo", |
| 27 | + }, |
| 28 | + ) |
| 29 | + fakeUserClient := userfake.NewSimpleClientset(&userapi.User{ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "bar"}}) |
| 30 | + |
| 31 | + tokenAuthenticator := NewTokenAuthenticator(fakeOAuthClient.Oauth().OAuthAccessTokens(), fakeUserClient.UserV1().Users(), NoopGroupMapper{}, NewExpirationValidator()) |
| 32 | + |
| 33 | + for _, tokenName := range []string{"token1", "token2"} { |
| 34 | + userInfo, found, err := tokenAuthenticator.AuthenticateToken(tokenName) |
| 35 | + if found { |
| 36 | + t.Error("Found token, but it should be missing!") |
| 37 | + } |
| 38 | + if err != errExpired { |
| 39 | + t.Errorf("Unexpected error: %v", err) |
| 40 | + } |
| 41 | + if userInfo != nil { |
| 42 | + t.Errorf("Unexpected user: %v", userInfo) |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestAuthenticateTokenValidated(t *testing.T) { |
| 48 | + fakeOAuthClient := oauthfake.NewSimpleClientset( |
| 49 | + &oapi.OAuthAccessToken{ |
| 50 | + ObjectMeta: metav1.ObjectMeta{Name: "token", CreationTimestamp: metav1.Time{Time: time.Now()}}, |
| 51 | + ExpiresIn: 600, // 10 minutes |
| 52 | + UserName: "foo", |
| 53 | + UserUID: string("bar"), |
| 54 | + }, |
| 55 | + ) |
| 56 | + fakeUserClient := userfake.NewSimpleClientset(&userapi.User{ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "bar"}}) |
| 57 | + |
| 58 | + tokenAuthenticator := NewTokenAuthenticator(fakeOAuthClient.Oauth().OAuthAccessTokens(), fakeUserClient.UserV1().Users(), NoopGroupMapper{}, NewExpirationValidator(), NewUIDValidator()) |
| 59 | + |
| 60 | + userInfo, found, err := tokenAuthenticator.AuthenticateToken("token") |
| 61 | + if !found { |
| 62 | + t.Error("Did not find a token!") |
| 63 | + } |
| 64 | + if err != nil { |
| 65 | + t.Errorf("Unexpected error: %v", err) |
| 66 | + } |
| 67 | + if userInfo == nil { |
| 68 | + t.Error("Did not get a user!") |
| 69 | + } |
| 70 | +} |
0 commit comments