Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-1716] Add tests for claim* MW #113

Merged
merged 2 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions middleware/requiresAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,9 @@ function checkJSONprimitive(value) {
}
}

// TODO: find a better name
module.exports.withClaimEqualCheck = function withClaimEqualCheck(
claim,
expected
) {
module.exports.claimEquals = function claimEquals(claim, expected) {
// check that claim is a string value
if (typeof claims !== 'string') {
if (typeof claim !== 'string') {
throw new TypeError('"claim" must be a string');
}
// check that expected is a JSON supported primitive
Expand All @@ -84,13 +80,9 @@ module.exports.withClaimEqualCheck = function withClaimEqualCheck(
return requiresLoginMiddleware.bind(undefined, authenticationCheck);
};

// TODO: find a better name
module.exports.withClaimIncluding = function withClaimIncluding(
claim,
...expected
) {
module.exports.claimIncludes = function claimIncludes(claim, ...expected) {
// check that claim is a string value
if (typeof claims !== 'string') {
if (typeof claim !== 'string') {
throw new TypeError('"claim" must be a string');
}
// check that all expected are JSON supported primitives
Expand All @@ -109,7 +101,9 @@ module.exports.withClaimIncluding = function withClaimIncluding(
if (typeof actual === 'string') {
actual = actual.split(' ');
} else if (!Array.isArray(actual)) {
// TODO: log unexpected type;
debug.trace(
`Unexpected claim type. Expected array or string, got ${typeof actual}`
);
return true;
}

Expand All @@ -120,11 +114,10 @@ module.exports.withClaimIncluding = function withClaimIncluding(
return requiresLoginMiddleware.bind(undefined, authenticationCheck);
};

// TODO: find a better name
module.exports.custom = function custom(func) {
module.exports.claimCheck = function claimCheck(func) {
// check that func is a function
if (typeof func !== 'function' || func.constructor.name !== 'Function') {
throw new TypeError('"function" must be a function');
throw new TypeError('"claimCheck" expects a function');
}
const authenticationCheck = (req) => {
if (defaultRequiresLogin(req)) {
Expand All @@ -133,7 +126,7 @@ module.exports.custom = function custom(func) {

const { idTokenClaims } = req.oidc;

return func(req, idTokenClaims);
return !func(req, idTokenClaims);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought claimCheck(() => true) should mean allow, claimCheck(() => false) should mean 401

};
return requiresLoginMiddleware.bind(undefined, authenticationCheck);
};
22 changes: 1 addition & 21 deletions test/callback.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const TransientCookieHandler = require('../lib/transientHandler');
const { encodeState } = require('../lib/hooks/getLoginState');
const expressOpenid = require('..');
const { create: createServer } = require('./fixture/server');
const cert = require('./fixture/cert');
const { makeIdToken } = require('./fixture/cert');
const clientID = '__test_client_id__';
const expectedDefaultState = encodeState({ returnTo: 'https://example.org' });
const nock = require('nock');
Expand Down Expand Up @@ -100,26 +100,6 @@ const setup = async (params) => {
};
};

function makeIdToken(payload) {
payload = Object.assign(
{
nickname: '__test_nickname__',
sub: '__test_sub__',
iss: 'https://op.example.com/',
aud: clientID,
iat: Math.round(Date.now() / 1000),
exp: Math.round(Date.now() / 1000) + 60000,
nonce: '__test_nonce__',
},
payload
);

return jose.JWT.sign(payload, cert.key, {
algorithm: 'RS256',
header: { kid: cert.kid },
});
}

// For the purpose of this test the fake SERVER returns the error message in the body directly
// production application should have an error middleware.
// http://expressjs.com/en/guide/error-handling.html
Expand Down
26 changes: 23 additions & 3 deletions test/fixture/cert.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const jose = require('jose');
const { JWK, JWKS, JWT } = require('jose');

const key = jose.JWK.asKey({
const key = JWK.asKey({
e: 'AQAB',
n:
'wQrThQ9HKf8ksCQEzqOu0ofF8DtLJgexeFSQBNnMQetACzt4TbHPpjhTWUIlD8bFCkyx88d2_QV3TewMtfS649Pn5hV6adeYW2TxweAA8HVJxskcqTSa_ktojQ-cD43HIStsbqJhHoFv0UY6z5pwJrVPT-yt38ciKo9Oc9IhEl6TSw-zAnuNW0zPOhKjuiIqpAk1lT3e6cYv83ahx82vpx3ZnV83dT9uRbIbcgIpK4W64YnYb5uDH7hGI8-4GnalZDfdApTu-9Y8lg_1v5ul-eQDsLCkUCPkqBaNiCG3gfZUAKp9rrFRE_cJTv_MJn-y_XSTMWILvTY7vdSMRMo4kQ',
Expand All @@ -21,7 +21,27 @@ const key = jose.JWK.asKey({
alg: 'RS256',
});

module.exports.jwks = new jose.JWKS.KeyStore(key).toJWKS(false);
module.exports.jwks = new JWKS.KeyStore(key).toJWKS(false);

module.exports.key = key.toPEM(true);
module.exports.kid = key.kid;

module.exports.makeIdToken = (payload) => {
payload = Object.assign(
{
nickname: '__test_nickname__',
sub: '__test_sub__',
iss: 'https://op.example.com/',
aud: '__test_client_id__',
iat: Math.round(Date.now() / 1000),
exp: Math.round(Date.now() / 1000) + 60000,
nonce: '__test_nonce__',
},
payload
);

return JWT.sign(payload, key.toPEM(true), {
algorithm: 'RS256',
header: { kid: key.kid },
});
};
18 changes: 0 additions & 18 deletions test/invalid_response_type.tests.js

This file was deleted.

Loading