Skip to content

Commit

Permalink
Merge pull request #6 from random42/fix/idp-issuer
Browse files Browse the repository at this point in the history
fix: idp issuer in cached data
  • Loading branch information
random42 authored Mar 19, 2024
2 parents eda2c30 + 035759f commit 743fed7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
volumes:
- config:/certs:rw
spid:
image: "italia/spid-saml-check:1.9.2"
image: "italia/spid-saml-check:1.10.4"
ports:
- "8443:8443"
web:
Expand Down
11 changes: 8 additions & 3 deletions src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import difference from 'lodash.difference';
import { isISODateTimeUTC } from './util';

export class SpidResponse extends XML.XML {
validate(req: SpidRequest, config: SpidConfig, saml: SamlOptions) {
validate(
req: SpidRequest,
config: SpidConfig,
saml: SamlOptions,
idpIssuer: string,
) {
assert(this.response, `Missing response`);
assert(this.assertion, `Missing assertion`);
const { SAML_ASSERTION: A, SAML_PROTOCOL: P } = NS;
Expand Down Expand Up @@ -120,12 +125,12 @@ export class SpidResponse extends XML.XML {
// Issuer
assert.strictEqual(
data.issuer,
saml.idpIssuer,
idpIssuer,
`Invalid Issuer "${data.issuer}"`,
);
assert.strictEqual(
data.assertion.issuer,
saml.idpIssuer,
idpIssuer,
`Invalid Assertion Issuer "${data.assertion.issuer}"`,
);
assert(
Expand Down
17 changes: 14 additions & 3 deletions src/saml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { SpidRequest } from './request';
import { SamlSpidProfile, SpidConfig } from './types';
import { SpidResponse } from './response';

type CacheData = {
reqXml: string;
idpIssuer: string;
};

export class SpidSAML extends SAML {
constructor(samlConfig: SamlConfig, private spidConfig: SpidConfig) {
super(samlConfig);
Expand All @@ -29,7 +34,11 @@ export class SpidSAML extends SAML {
xml = signAuthnRequestPost(xml, this.options as any);
}
const { cache } = this.spidConfig;
await cache.set(id, xml);
const cacheData: CacheData = {
reqXml: xml,
idpIssuer: this.options.idpIssuer,
};
await cache.set(id, JSON.stringify(cacheData));
const timeoutMs =
this.options.requestIdExpirationPeriodMs ?? 1000 * 60 * 60 * 15;
if (cache.expire) {
Expand All @@ -51,7 +60,9 @@ export class SpidSAML extends SAML {
throw new Error(`Missing InResponseTo`);
}
const { cache } = this.spidConfig;
const reqXml = await cache.get(inResponseTo);
const cacheDataJSON = await cache.get(inResponseTo);
const cacheData = JSON.parse(cacheDataJSON) as CacheData;
const { reqXml } = cacheData;
if (!reqXml) {
throw new Error(`Missing request for ${inResponseTo} response`);
}
Expand All @@ -64,7 +75,7 @@ export class SpidSAML extends SAML {
samlResponseXml,
inResponseTo,
);
res.validate(req, this.spidConfig, this.options);
res.validate(req, this.spidConfig, this.options, cacheData.idpIssuer);
const p = profile as SamlSpidProfile;
p.getSamlRequestXml = () => reqXml;
return { profile: p, loggedOut };
Expand Down
12 changes: 6 additions & 6 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ function t {

docker-compose -f $C build -q

t HTTP-POST sha256 exact 1
# t HTTP-POST sha256 exact 1
t HTTP-Redirect sha512 minimum 1
t HTTP-POST sha256 maximum 1
# t HTTP-POST sha256 maximum 1
# t HTTP-Redirect sha512 better 1 # not working atm
t HTTP-Redirect sha512 exact 2
# t HTTP-Redirect sha512 exact 2
t HTTP-POST sha256 minimum 2
t HTTP-Redirect sha512 maximum 2
# t HTTP-Redirect sha512 maximum 2
# t HTTP-POST sha256 better 2 # not working atm
t HTTP-Redirect sha512 exact 3
t HTTP-POST sha256 minimum 3
# t HTTP-Redirect sha512 exact 3
# t HTTP-POST sha256 minimum 3
t HTTP-Redirect sha512 maximum 3

docker-compose -f $C down -v

0 comments on commit 743fed7

Please sign in to comment.