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

Validation and relay state customization #257

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions src/binding-redirect.ts
Original file line number Diff line number Diff line change
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 @@ -108,7 +109,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
Original file line number Diff line number Diff line change
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,21 +51,27 @@ 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?: (template: string) => BindingContext,
relayState?: string,
customTagReplacement?: (template: string) => BindingContext
): 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) {
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,10 @@ export type IdentityProviderSettings = {
wantLogoutRequestSignedResponseSigned?: boolean;
tagPrefix?: { [key: string]: string };
};

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