Skip to content

Commit

Permalink
Merge 73be9f2 into 4563872
Browse files Browse the repository at this point in the history
  • Loading branch information
dddoronnn committed Mar 30, 2019
2 parents 4563872 + 73be9f2 commit a13ca87
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/binding-redirect.ts
Expand Up @@ -72,10 +72,11 @@ function buildRedirectURL(opts: BuildRedirectConfig) {
* @param {function} customTagReplacement used when developers have their own login response template
* @return {string} redirect URL
*/
function loginRequestRedirectURL(entity: { idp: Idp, sp: Sp }, customTagReplacement?: (template: string) => BindingContext): BindingContext {
function loginRequestRedirectURL(entity: { idp: Idp, sp: Sp, relayState?: string }, customTagReplacement?: (template: string) => BindingContext): BindingContext {

const metadata: any = { idp: entity.idp.entityMeta, sp: entity.sp.entityMeta };
const spSetting: any = entity.sp.entitySetting;
const relayState = entity.relayState === undefined ? spSetting.relayState : entity.relayState;
let id: string = '';

if (metadata && metadata.idp && metadata.sp) {
Expand Down Expand Up @@ -106,7 +107,7 @@ function loginRequestRedirectURL(entity: { idp: Idp, sp: Sp }, customTagReplacem
isSigned: metadata.sp.isAuthnRequestSigned(),
entitySetting: spSetting,
baseUrl: base,
relayState: spSetting.relayState,
relayState,
}),
};
}
Expand Down
27 changes: 21 additions & 6 deletions src/entity-sp.ts
Expand Up @@ -12,6 +12,7 @@ import {
IdentityProviderConstructor as IdentityProvider,
ServiceProviderMetadata,
ServiceProviderSettings,
ValidationSettings,
} from './types';
import { namespace } from './urn';
import redirectBinding from './binding-redirect';
Expand Down Expand Up @@ -50,28 +51,34 @@ export class ServiceProvider extends Entity {
* @desc Generates the login request for developers to design their own method
* @param {IdentityProvider} idp object of identity provider
* @param {string} binding protocol binding
* @param {function} customTagReplacement used when developers have their own login response template
* @param {function} customTagReplacement used when developers have their own login response template
* @param {string} relayState optionally override default SP relayState
*/
public createLoginRequest(
idp: IdentityProvider,
binding = 'redirect',
customTagReplacement?: (...args: any[]) => any,
relayState?: string
): BindingContext | PostBindingContext {
const nsBinding = namespace.binding;
const protocol = nsBinding[binding];
if (this.entityMeta.isAuthnRequestSigned() !== idp.entityMeta.isWantAuthnRequestsSigned()) {
throw new Error('ERR_METADATA_CONFLICT_REQUEST_SIGNED_FLAG');
}

if (relayState === undefined) {
relayState = this.entitySetting.relayState;
}

if (protocol === nsBinding.redirect) {
return redirectBinding.loginRequestRedirectURL({ idp, sp: this }, customTagReplacement);
return redirectBinding.loginRequestRedirectURL({ idp, sp: this, relayState }, customTagReplacement);
}

if (protocol === nsBinding.post) {
const context = postBinding.base64LoginRequest("/*[local-name(.)='AuthnRequest']", { idp, sp: this }, customTagReplacement);
return {
...context,
relayState: this.entitySetting.relayState,
relayState,
entityEndpoint: idp.entityMeta.getSingleSignOnService(binding),
type: 'SAMLRequest',
};
Expand All @@ -85,18 +92,26 @@ export class ServiceProvider extends Entity {
* @param {IdentityProvider} idp object of identity provider
* @param {string} binding protocol binding
* @param {request} req request
* @param {ValidationSettings} validation optionally skip some validations
*/
public parseLoginResponse(idp, binding, request: ESamlHttpRequest) {
public parseLoginResponse(idp, binding, request: ESamlHttpRequest, validation?: ValidationSettings) {
const self = this;
return flow({

const options = {
from: idp,
self: self,
checkSignature: true, // saml response must have signature
parserType: 'SAMLResponse',
type: 'login',
binding: binding,
request: request
});
};

if (validation) {
Object.assign(options, validation);
}

return flow(options);
}

}
8 changes: 7 additions & 1 deletion src/flow.ts
Expand Up @@ -118,7 +118,10 @@ async function postFlow(options): Promise<FlowResult> {
from,
self,
parserType,
checkSignature = true
checkSignature = true,
checkIssuer = true,
checkSessionTime = true,
checkTime = true
} = options;

const { body } = request;
Expand Down Expand Up @@ -189,6 +192,7 @@ async function postFlow(options): Promise<FlowResult> {

// unmatched issuer
if (
checkIssuer &&
(parserType === 'LogoutResponse' || parserType === 'SAMLResponse')
&& extractedProperties
&& extractedProperties.issuer !== issuer
Expand All @@ -198,6 +202,7 @@ async function postFlow(options): Promise<FlowResult> {

// invalid session time
if (
checkSessionTime &&
parserType === 'SAMLResponse'
&& !verifyTime(
undefined,
Expand All @@ -210,6 +215,7 @@ async function postFlow(options): Promise<FlowResult> {
// invalid time
// 2.4.1.2 https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf
if (
checkTime &&
parserType === 'SAMLResponse'
&& extractedProperties.conditions
&& !verifyTime(
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Expand Up @@ -118,3 +118,10 @@ export interface IdentityProviderSettings {
wantLogoutRequestSignedResponseSigned?: boolean;
tagPrefix?: { [key: string]: string };
}

export interface ValidationSettings {
checkSignature?: boolean;
checkIssuer?: boolean;
checkSessionTime?: boolean;
checkTime?: boolean;
}

0 comments on commit a13ca87

Please sign in to comment.