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

Provide support for professional Spid #72

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
46 changes: 44 additions & 2 deletions src/utils/__tests__/saml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { right } from "fp-ts/lib/Either";
import { isSome, tryCatch } from "fp-ts/lib/Option";
import { fromEither } from "fp-ts/lib/TaskEither";
import { SamlConfig } from "passport-saml";
import { Builder } from "xml2js";
import { DOMParser } from "xmldom";
import { EventTracker } from "../../index";
import {
Expand All @@ -12,7 +13,11 @@ import {
samlResponseCIE
} from "../__mocks__/saml";
import { StrictResponseValidationOptions } from "../middleware";
import { getPreValidateResponse, getXmlFromSamlResponse } from "../saml";
import {
getAuthorizeRequestTamperer,
getPreValidateResponse,
getXmlFromSamlResponse
} from "../saml";
import * as saml from "../saml";

const samlConfig: SamlConfig = ({
Expand Down Expand Up @@ -54,6 +59,43 @@ describe("getXmlFromSamlResponse", () => {
});
});

describe("getAuthorizeRequestTamperer", () => {
it("should add professional spid extension", async () => {
await getAuthorizeRequestTamperer(
// spid-testenv does not accept an xml header with utf8 encoding
new Builder({ xmldec: { encoding: undefined, version: "1.0" } }),
{
professionalSpidExtension: {
professionalSpidEnabled: true,
purpose: "P"
}
// tslint:disable-next-line: no-any
} as any,
samlConfig
)(samlRequest)
.fold(
err => fail(err),
_ => expect(_).toContain("<spid:Purpose>P</spid:Purpose>")
)
.run();
});

it("should not add professional spid extension", async () => {
await getAuthorizeRequestTamperer(
// spid-testenv does not accept an xml header with utf8 encoding
new Builder({ xmldec: { encoding: undefined, version: "1.0" } }),
// tslint:disable-next-line: no-any
{} as any,
samlConfig
)(samlRequest)
.fold(
err => fail(err),
_ => expect(_).not.toContain("<spid:Purpose")
)
.run();
});
});

// tslint:disable-next-line: no-big-function
describe("preValidateResponse", () => {
const mockCallback = jest.fn();
Expand All @@ -80,7 +122,7 @@ describe("preValidateResponse", () => {
error
? expect(callback).toBeCalledWith(error)
: expect(callback).toBeCalledWith(null, true, expect.any(String));
resolve();
resolve(void 0);
}, 100);
});

Expand Down
21 changes: 21 additions & 0 deletions src/utils/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as express from "express";
import { array } from "fp-ts/lib/Array";
import { Task, task } from "fp-ts/lib/Task";
import * as t from "io-ts";
import { NonEmptyString } from "italia-ts-commons/lib/strings";
import { Profile, SamlConfig, VerifiedCallback } from "passport-saml";
import { RedisClient } from "redis";
Expand All @@ -18,6 +19,25 @@ import { IDPEntityDescriptor } from "../types/IDPEntityDescriptor";
import { fetchIdpsMetadata } from "./metadata";
import { logSamlCertExpiration, SamlAttributeT } from "./saml";

export const ProfessionalPurpose = t.keyof({
LP: null,
P: null,
PF: null,
PG: null,
PX: null
});

const ProfessionalSpidExtension = t.union([
t.interface({
professionalSpidEnabled: t.literal(false)
}),
t.interface({
professionalSpidEnabled: t.literal(true),
purpose: ProfessionalPurpose
})
]);

type ProfessionalSpidExtension = t.TypeOf<typeof ProfessionalSpidExtension>;
interface IServiceProviderOrganization {
URL: string;
displayName: string;
Expand All @@ -34,6 +54,7 @@ export interface IServiceProviderConfig {
IDPMetadataUrl: string;
organization: IServiceProviderOrganization;
publicCert: string;
professionalSpidExtension?: ProfessionalSpidExtension;
strictResponseValidation?: StrictResponseValidationOptions;
}

Expand Down
13 changes: 13 additions & 0 deletions src/utils/saml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export const SAML_NAMESPACE = {
XMLDSIG: "http://www.w3.org/2000/09/xmldsig#"
};

const SAML_EXTENSION = "https://spid.gov.it/saml‐extensions";
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const SAML_EXTENSION = "https://spid.gov.it/saml‐extensions";
const SPID_SAML_EXTENSION = "https://spid.gov.it/saml‐extensions";


const ISSUER_FORMAT = "urn:oasis:names:tc:SAML:2.0:nameid-format:entity";

const decodeBase64 = (s: string) => Buffer.from(s, "base64").toString("utf8");
Expand Down Expand Up @@ -496,6 +498,17 @@ export const getAuthorizeRequestTamperer = (
authnRequest["saml:Issuer"][0].$.NameQualifier = samlConfig.issuer;
// tslint:disable-next-line: no-object-mutation
authnRequest["saml:Issuer"][0].$.Format = ISSUER_FORMAT;
if (_.professionalSpidExtension?.professionalSpidEnabled === true) {
// tslint:disable-next-line: no-object-mutation
authnRequest["samlp:Extensions"] = {
$: {
"xmlns:spid": SAML_EXTENSION
},
"spid:Purpose": {
_: _.professionalSpidExtension.purpose
}
};
}
return o;
}, toError)
)
Expand Down