-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jörn Friedrich Dreyer <[email protected]>
- Loading branch information
Showing
18 changed files
with
163 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,28 +2,29 @@ package middleware | |
|
||
import ( | ||
"context" | ||
"github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" | ||
"github.com/cs3org/reva/pkg/token" | ||
"github.com/owncloud/ocis/ocis-pkg/log" | ||
"github.com/owncloud/ocis/ocis-pkg/oidc" | ||
"github.com/owncloud/ocis/proxy/pkg/config" | ||
"github.com/owncloud/ocis/proxy/pkg/user/backend" | ||
"github.com/owncloud/ocis/proxy/pkg/user/backend/test" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestTokenIsAddedWithMailClaim(t *testing.T) { | ||
sut := newMockAccountResolver(&userv1beta1.User{ | ||
Id: &userv1beta1.UserId{Idp: "https://idx.example.com", OpaqueId: "123"}, | ||
Mail: "[email protected]", | ||
}, nil) | ||
}, nil, oidc.Email, "mail") | ||
|
||
req, rw := mockRequest(&oidc.StandardClaims{ | ||
Iss: "https://idx.example.com", | ||
Email: "[email protected]", | ||
req, rw := mockRequest(map[string]interface{}{ | ||
oidc.Iss: "https://idx.example.com", | ||
oidc.Email: "[email protected]", | ||
}) | ||
|
||
sut.ServeHTTP(rw, req) | ||
|
@@ -37,11 +38,11 @@ func TestTokenIsAddedWithUsernameClaim(t *testing.T) { | |
sut := newMockAccountResolver(&userv1beta1.User{ | ||
Id: &userv1beta1.UserId{Idp: "https://idx.example.com", OpaqueId: "123"}, | ||
Mail: "[email protected]", | ||
}, nil) | ||
}, nil, oidc.PreferredUsername, "username") | ||
|
||
req, rw := mockRequest(&oidc.StandardClaims{ | ||
Iss: "https://idx.example.com", | ||
PreferredUsername: "foo", | ||
req, rw := mockRequest(map[string]interface{}{ | ||
oidc.Iss: "https://idx.example.com", | ||
oidc.PreferredUsername: "foo", | ||
}) | ||
|
||
sut.ServeHTTP(rw, req) | ||
|
@@ -53,7 +54,7 @@ func TestTokenIsAddedWithUsernameClaim(t *testing.T) { | |
} | ||
|
||
func TestNSkipOnNoClaims(t *testing.T) { | ||
sut := newMockAccountResolver(nil, backend.ErrAccountDisabled) | ||
sut := newMockAccountResolver(nil, backend.ErrAccountDisabled, oidc.Email, "mail") | ||
req, rw := mockRequest(nil) | ||
|
||
sut.ServeHTTP(rw, req) | ||
|
@@ -64,10 +65,10 @@ func TestNSkipOnNoClaims(t *testing.T) { | |
} | ||
|
||
func TestUnauthorizedOnUserNotFound(t *testing.T) { | ||
sut := newMockAccountResolver(nil, backend.ErrAccountNotFound) | ||
req, rw := mockRequest(&oidc.StandardClaims{ | ||
Iss: "https://idx.example.com", | ||
PreferredUsername: "foo", | ||
sut := newMockAccountResolver(nil, backend.ErrAccountNotFound, oidc.PreferredUsername, "username") | ||
req, rw := mockRequest(map[string]interface{}{ | ||
oidc.Iss: "https://idx.example.com", | ||
oidc.PreferredUsername: "foo", | ||
}) | ||
|
||
sut.ServeHTTP(rw, req) | ||
|
@@ -78,10 +79,10 @@ func TestUnauthorizedOnUserNotFound(t *testing.T) { | |
} | ||
|
||
func TestUnauthorizedOnUserDisabled(t *testing.T) { | ||
sut := newMockAccountResolver(nil, backend.ErrAccountDisabled) | ||
req, rw := mockRequest(&oidc.StandardClaims{ | ||
Iss: "https://idx.example.com", | ||
PreferredUsername: "foo", | ||
sut := newMockAccountResolver(nil, backend.ErrAccountDisabled, oidc.PreferredUsername, "username") | ||
req, rw := mockRequest(map[string]interface{}{ | ||
oidc.Iss: "https://idx.example.com", | ||
oidc.PreferredUsername: "foo", | ||
}) | ||
|
||
sut.ServeHTTP(rw, req) | ||
|
@@ -92,9 +93,9 @@ func TestUnauthorizedOnUserDisabled(t *testing.T) { | |
} | ||
|
||
func TestInternalServerErrorOnMissingMailAndUsername(t *testing.T) { | ||
sut := newMockAccountResolver(nil, backend.ErrAccountDisabled) | ||
req, rw := mockRequest(&oidc.StandardClaims{ | ||
Iss: "https://idx.example.com", | ||
sut := newMockAccountResolver(nil, backend.ErrAccountNotFound, oidc.Email, "mail") | ||
req, rw := mockRequest(map[string]interface{}{ | ||
oidc.Iss: "https://idx.example.com", | ||
}) | ||
|
||
sut.ServeHTTP(rw, req) | ||
|
@@ -104,7 +105,7 @@ func TestInternalServerErrorOnMissingMailAndUsername(t *testing.T) { | |
assert.Equal(t, http.StatusInternalServerError, rw.Code) | ||
} | ||
|
||
func newMockAccountResolver(userBackendResult *userv1beta1.User, userBackendErr error) http.Handler { | ||
func newMockAccountResolver(userBackendResult *userv1beta1.User, userBackendErr error, oidcclaim, cs3claim string) http.Handler { | ||
mock := &test.UserBackendMock{ | ||
GetUserByClaimsFunc: func(ctx context.Context, claim string, value string, withRoles bool) (*userv1beta1.User, error) { | ||
return userBackendResult, userBackendErr | ||
|
@@ -115,11 +116,13 @@ func newMockAccountResolver(userBackendResult *userv1beta1.User, userBackendErr | |
Logger(log.NewLogger()), | ||
UserProvider(mock), | ||
TokenManagerConfig(config.TokenManager{JWTSecret: "secret"}), | ||
UserOIDCClaim(oidcclaim), | ||
UserCS3Claim(cs3claim), | ||
AutoprovisionAccounts(false), | ||
)(mockHandler{}) | ||
} | ||
|
||
func mockRequest(claims *oidc.StandardClaims) (*http.Request, *httptest.ResponseRecorder) { | ||
func mockRequest(claims map[string]interface{}) (*http.Request, *httptest.ResponseRecorder) { | ||
if claims == nil { | ||
return httptest.NewRequest("GET", "http://example.com/foo", nil), httptest.NewRecorder() | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.