Skip to content

Commit 754bd6c

Browse files
committed
move non-oauthserver packages out
1 parent 508e2ca commit 754bd6c

File tree

12 files changed

+512
-493
lines changed

12 files changed

+512
-493
lines changed

hack/import-restrictions.json

-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,6 @@
374374
"github.com/openshift/origin/pkg/oauth/apis/oauth/validation",
375375
"github.com/openshift/origin/pkg/oauth/scope",
376376
"github.com/openshift/origin/pkg/oauth/apis/oauth",
377-
"github.com/openshift/origin/pkg/util/rankedset",
378377
"github.com/openshift/origin/pkg/oauth/registry/oauthclientauthorization",
379378
"github.com/openshift/origin/pkg/cmd/server/api",
380379
"github.com/openshift/origin/pkg/cmd/server/api/latest",

pkg/oauthserver/oauth/registry/expirationvalidator.go pkg/apiserver/authentication/internaloauth/expirationvalidator.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
package registry
1+
package internaloauth
22

33
import (
44
"errors"
55
"time"
66

77
userv1 "github.com/openshift/api/user/v1"
88
"github.com/openshift/origin/pkg/oauth/apis/oauth"
9-
"github.com/openshift/origin/pkg/oauthserver/authenticator"
109
)
1110

1211
var errExpired = errors.New("token is expired")
1312

14-
func NewExpirationValidator() authenticator.OAuthTokenValidator {
15-
return authenticator.OAuthTokenValidatorFunc(
13+
func NewExpirationValidator() OAuthTokenValidator {
14+
return OAuthTokenValidatorFunc(
1615
func(token *oauth.OAuthAccessToken, _ *userv1.User) error {
1716
if token.ExpiresIn > 0 {
1817
if expire(token).Before(time.Now()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package internaloauth
2+
3+
import (
4+
userapi "github.com/openshift/api/user/v1"
5+
"github.com/openshift/origin/pkg/oauth/apis/oauth"
6+
)
7+
8+
type OAuthTokenValidator interface {
9+
Validate(token *oauth.OAuthAccessToken, user *userapi.User) error
10+
}
11+
12+
var _ OAuthTokenValidator = OAuthTokenValidatorFunc(nil)
13+
14+
type OAuthTokenValidatorFunc func(token *oauth.OAuthAccessToken, user *userapi.User) error
15+
16+
func (f OAuthTokenValidatorFunc) Validate(token *oauth.OAuthAccessToken, user *userapi.User) error {
17+
return f(token, user)
18+
}
19+
20+
var _ OAuthTokenValidator = OAuthTokenValidators(nil)
21+
22+
type OAuthTokenValidators []OAuthTokenValidator
23+
24+
func (v OAuthTokenValidators) Validate(token *oauth.OAuthAccessToken, user *userapi.User) error {
25+
for _, validator := range v {
26+
if err := validator.Validate(token, user); err != nil {
27+
return err
28+
}
29+
}
30+
return nil
31+
}
32+
33+
type UserToGroupMapper interface {
34+
GroupsFor(username string) ([]*userapi.Group, error)
35+
}
36+
37+
type NoopGroupMapper struct{}
38+
39+
func (n NoopGroupMapper) GroupsFor(username string) ([]*userapi.Group, error) {
40+
return []*userapi.Group{}, nil
41+
}

pkg/oauthserver/oauth/registry/timeoutvalidator.go pkg/apiserver/authentication/internaloauth/timeoutvalidator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package registry
1+
package internaloauth
22

33
import (
44
"errors"

pkg/oauthserver/oauth/registry/tokenauthenticator.go pkg/apiserver/authentication/internaloauth/tokenauthenticator.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package registry
1+
package internaloauth
22

33
import (
44
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -8,23 +8,21 @@ import (
88
userclient "github.com/openshift/client-go/user/clientset/versioned/typed/user/v1"
99
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
1010
oauthclient "github.com/openshift/origin/pkg/oauth/generated/internalclientset/typed/oauth/internalversion"
11-
"github.com/openshift/origin/pkg/oauthserver/authenticator"
12-
"github.com/openshift/origin/pkg/oauthserver/userregistry/identitymapper"
1311
)
1412

1513
type tokenAuthenticator struct {
1614
tokens oauthclient.OAuthAccessTokenInterface
1715
users userclient.UserInterface
18-
groupMapper identitymapper.UserToGroupMapper
19-
validators authenticator.OAuthTokenValidator
16+
groupMapper UserToGroupMapper
17+
validators OAuthTokenValidator
2018
}
2119

22-
func NewTokenAuthenticator(tokens oauthclient.OAuthAccessTokenInterface, users userclient.UserInterface, groupMapper identitymapper.UserToGroupMapper, validators ...authenticator.OAuthTokenValidator) kauthenticator.Token {
20+
func NewTokenAuthenticator(tokens oauthclient.OAuthAccessTokenInterface, users userclient.UserInterface, groupMapper UserToGroupMapper, validators ...OAuthTokenValidator) kauthenticator.Token {
2321
return &tokenAuthenticator{
2422
tokens: tokens,
2523
users: users,
2624
groupMapper: groupMapper,
27-
validators: authenticator.OAuthTokenValidators(validators),
25+
validators: OAuthTokenValidators(validators),
2826
}
2927
}
3028

0 commit comments

Comments
 (0)