Skip to content

Commit

Permalink
More tests and fix the mutation in defining binding method of logout
Browse files Browse the repository at this point in the history
response
  • Loading branch information
tngan committed Jun 11, 2017
1 parent 4256b68 commit 5df2b70
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
18 changes: 9 additions & 9 deletions src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export default class Entity {
* @param {date} notOnOrAfter
* @return {boolean}
*/
verifyTime(notBefore: Date, notOnOrAfter: Date): boolean {
verifyTime(notBefore?: Date, notOnOrAfter?: Date): boolean {
const now = new Date();
if (isUndefined(notBefore) && isUndefined(notOnOrAfter)) {
return true; // throw exception todo
Expand Down Expand Up @@ -265,7 +265,7 @@ export default class Entity {
return parseResult;
}
// Will support artifact in the next release
throw new Error('this binding is not support');
throw new Error('this binding is not supported');
}

/** @desc Generates the logout request for developers to design their own method
Expand All @@ -275,12 +275,12 @@ export default class Entity {
* @param {string} relayState the URL to which to redirect the user when logout is complete
* @param {function} customTagReplacement used when developers have their own login response template
*/
createLogoutRequest(targetEntity, binding, user, relayState, customTagReplacement): BindingContext | PostRequestInfo {
createLogoutRequest(targetEntity, binding, user, relayState = '', customTagReplacement?): BindingContext | PostRequestInfo {
if (binding === wording.binding.redirect) {
return redirectBinding.logoutRequestRedirectURL(user, {
init: this,
target: targetEntity,
}, customTagReplacement, relayState);
}, relayState, customTagReplacement);
}
if (binding === wording.binding.post) {
const entityEndpoint = targetEntity.entityMeta.getSingleLogoutService(binding);
Expand All @@ -304,15 +304,15 @@ export default class Entity {
* @param {string} binding protocol binding
* @param {function} customTagReplacement used when developers have their own login response template
*/
createLogoutResponse(target, requestInfo, binding, relayState, customTagReplacement): BindingContext {
binding = namespace.binding[binding] || namespace.binding.redirect;
if (binding === namespace.binding.redirect) {
createLogoutResponse(target, requestInfo, binding, relayState = '', customTagReplacement?): BindingContext {
const protocol = namespace.binding[binding];
if (protocol === namespace.binding.redirect) {
return redirectBinding.logoutResponseRedirectURL(requestInfo, {
init: this,
target,
}, relayState, customTagReplacement);
}
if (binding === namespace.binding.post) {
if (protocol === namespace.binding.post) {
const context = postBinding.base64LogoutResponse(requestInfo, {
init: this,
target,
Expand All @@ -324,7 +324,7 @@ export default class Entity {
type: 'SAMLResponse',
};
}
throw new Error('This binding is not support');
throw new Error('this binding is not supported');
}

/**
Expand Down
47 changes: 40 additions & 7 deletions test/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ test('signed in sp is not matched with the signed notation in idp with redirect
});

test('create login request with redirect binding using custom template', t => {
const _sp = serviceProvider({ ...defaultSpConfig, loginRequestTemplate: {
context: '<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="{ID}" Version="2.0" IssueInstant="{IssueInstant}" Destination="{Destination}" ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" AssertionConsumerServiceURL="{AssertionConsumerServiceURL}"><saml:Issuer>{Issuer}</saml:Issuer><samlp:NameIDPolicy Format="{NameIDFormat}" AllowCreate="{AllowCreate}"/></samlp:AuthnRequest>',
}});
const _sp = serviceProvider({
...defaultSpConfig, loginRequestTemplate: {
context: '<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="{ID}" Version="2.0" IssueInstant="{IssueInstant}" Destination="{Destination}" ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" AssertionConsumerServiceURL="{AssertionConsumerServiceURL}"><saml:Issuer>{Issuer}</saml:Issuer><samlp:NameIDPolicy Format="{NameIDFormat}" AllowCreate="{AllowCreate}"/></samlp:AuthnRequest>',
},
});
const { id, context } = _sp.createLoginRequest(idp, 'redirect', template => {
return {
id: 'exposed_testing_id',
Expand All @@ -109,9 +111,11 @@ test('create login request with redirect binding using custom template', t => {
});

test('create login request with post binding using custom template', t => {
const _sp = serviceProvider({ ...defaultSpConfig, loginRequestTemplate: {
context: '<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="{ID}" Version="2.0" IssueInstant="{IssueInstant}" Destination="{Destination}" ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" AssertionConsumerServiceURL="{AssertionConsumerServiceURL}"><saml:Issuer>{Issuer}</saml:Issuer><samlp:NameIDPolicy Format="{NameIDFormat}" AllowCreate="{AllowCreate}"/></samlp:AuthnRequest>',
}});
const _sp = serviceProvider({
...defaultSpConfig, loginRequestTemplate: {
context: '<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="{ID}" Version="2.0" IssueInstant="{IssueInstant}" Destination="{Destination}" ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" AssertionConsumerServiceURL="{AssertionConsumerServiceURL}"><saml:Issuer>{Issuer}</saml:Issuer><samlp:NameIDPolicy Format="{NameIDFormat}" AllowCreate="{AllowCreate}"/></samlp:AuthnRequest>',
},
});
const { id, context, entityEndpoint, type, relayState } = _sp.createLoginRequest(idp, 'post', template => {
return {
id: 'exposed_testing_id',
Expand All @@ -133,5 +137,34 @@ test('create login response with undefined binding', async t => {

test('create post login response', async t => {
const { id, context } = await idp.createLoginResponse(sp, null, 'post', { email: 'user@esaml2.com' });
_.isString(id) && _.isString(context) ? t.pass() : t.fail();
_.isString(id) && _.isString(context) ? t.pass() : t.fail();
});

test('create logout request with redirect binding', t => {
const { id, context } = sp.createLogoutRequest(idp, 'redirect', { email: 'user@esaml2' });
_.isString(id) && _.isString(context) ? t.pass() : t.fail();
});

test('create logout request with post binding', t => {
const { relayState, type, entityEndpoint, id, context } = sp.createLogoutRequest(idp, 'post', { email: 'user@esaml2' }) as PostRequestInfo;
_.isString(id) && _.isString(context) && _.isString(entityEndpoint) && _.isEqual(type, 'SAMLRequest') ? t.pass() : t.fail();
});

test('create logout response with undefined binding', t => {
try {
const { id, context } = idp.createLogoutResponse(sp, {}, 'undefined');
t.fail();
} catch (e) {
t.is(e.message, 'this binding is not supported');
}
});

test('create logout response with redirect binding', t => {
const { id, context } = idp.createLogoutResponse(sp, {}, 'redirect');
_.isString(id) && _.isString(context) ? t.pass() : t.fail();
});

test('create logout response with post binding', t => {
const { relayState, type, entityEndpoint, id, context } = idp.createLogoutResponse(sp, {}, 'post') as PostRequestInfo;
_.isString(id) && _.isString(context) && _.isString(entityEndpoint) && _.isEqual(type, 'SAMLResponse') ? t.pass() : t.fail();
});
11 changes: 11 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,14 @@ test('getAssertionConsumerService with two bindings', t => {
});

})();

test('verify time', t => {
let timeAfter5Mins = new Date();
let timeBefore5Mins = new Date();
timeBefore5Mins = new Date(timeBefore5Mins.setMinutes(timeBefore5Mins.getMinutes() - 5));
timeAfter5Mins = new Date(timeAfter5Mins.setMinutes(timeAfter5Mins.getMinutes() + 5));
t.true(sp.verifyTime(timeBefore5Mins, timeAfter5Mins));
t.false(sp.verifyTime(undefined, timeBefore5Mins));
t.false(sp.verifyTime(timeAfter5Mins));
t.true(sp.verifyTime());
});

0 comments on commit 5df2b70

Please sign in to comment.