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

async / await in cache interface #532

Merged
merged 2 commits into from
Feb 10, 2021
Merged
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
25 changes: 11 additions & 14 deletions src/passport-saml/inmemory-cache-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,13 @@ export class CacheProvider {
nowMs >=
new Date(this.cacheKeys[key].createdAt).getTime() + this.options.keyExpirationPeriodMs
) {
this.remove(key, () => undefined);
this.removeAsync(key);
cjbarth marked this conversation as resolved.
Show resolved Hide resolved
}
});
}, this.options.keyExpirationPeriodMs);

// we only want this to run if the process is still open; it shouldn't hold the process open (issue #68)
// (unref only introduced in node 0.9, so check whether we have it)
// Skip this in 0.10.34 due to https://github.com/joyent/node/issues/8900
if (expirationTimer.unref && process.version !== "v0.10.34") expirationTimer.unref();
expirationTimer.unref();
}

/**
Expand All @@ -65,16 +63,15 @@ export class CacheProvider {
* @param id
* @param value
*/
save(key: string, value: string, callback: (error: null, value: CacheItem | null) => void) {
async saveAsync(key: string, value: string): Promise<CacheItem | null> {
if (!this.cacheKeys[key]) {
this.cacheKeys[key] = {
createdAt: new Date().getTime(),
value: value,
};

callback(null, this.cacheKeys[key]);
return this.cacheKeys[key];
} else {
callback(null, null);
return null;
}
}

Expand All @@ -83,24 +80,24 @@ export class CacheProvider {
* @param id
* @returns {boolean}
*/
get(key: string, callback: (key: string | null, value: string | null) => void) {
async getAsync(key: string): Promise<string | null> {
if (this.cacheKeys[key]) {
callback(null, this.cacheKeys[key].value);
return this.cacheKeys[key].value;
} else {
callback(null, null);
return null;
}
}

/**
* Removes an item from the cache if it exists
* @param key
*/
remove(key: string, callback: (err: Error | null, key: string | null) => void) {
async removeAsync(key: string): Promise<string | null> {
if (this.cacheKeys[key]) {
delete this.cacheKeys[key];
callback(null, key);
return key;
} else {
callback(null, null);
return null;
}
}
}
24 changes: 8 additions & 16 deletions src/passport-saml/saml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class SAML {
const forceAuthn = this.options.forceAuthn || false;

if (this.options.validateInResponseTo) {
await util.promisify(this.cacheProvider.save).bind(this.cacheProvider)(id, instant);
await this.cacheProvider.saveAsync(id, instant);
}
const request: AuthorizeRequestXML = {
"samlp:AuthnRequest": {
Expand Down Expand Up @@ -433,7 +433,7 @@ class SAML {
};
}

await util.promisify(this.cacheProvider.save).bind(this.cacheProvider)(id, instant);
await this.cacheProvider.saveAsync(id, instant);
return xmlbuilder.create((request as unknown) as Record<string, any>).end();
}

Expand Down Expand Up @@ -985,7 +985,7 @@ class SAML {
} catch (err) {
debug("validatePostResponse resulted in an error: %s", err);
if (this.options.validateInResponseTo) {
await util.promisify(this.cacheProvider.remove).bind(this.cacheProvider)(inResponseTo!);
await this.cacheProvider.removeAsync(inResponseTo!);
}
throw err;
}
Expand All @@ -994,9 +994,7 @@ class SAML {
async validateInResponseTo(inResponseTo: string | null) {
if (this.options.validateInResponseTo) {
if (inResponseTo) {
const result = await util.promisify(this.cacheProvider.get).bind(this.cacheProvider)(
inResponseTo
);
const result = await this.cacheProvider.getAsync(inResponseTo);
if (!result) throw new Error("InResponseTo is not valid");
return;
} else {
Expand Down Expand Up @@ -1253,31 +1251,25 @@ class SAML {
if (confirmData && confirmData.$) {
const subjectInResponseTo = confirmData.$.InResponseTo;
if (inResponseTo && subjectInResponseTo && subjectInResponseTo != inResponseTo) {
await util.promisify(this.cacheProvider.remove).bind(this.cacheProvider)(
inResponseTo
);
await this.cacheProvider.removeAsync(inResponseTo);
throw new Error("InResponseTo is not valid");
} else if (subjectInResponseTo) {
let foundValidInResponseTo = false;
const result = await util.promisify(this.cacheProvider.get).bind(this.cacheProvider)(
subjectInResponseTo
);
const result = await this.cacheProvider.getAsync(subjectInResponseTo);
if (result) {
const createdAt = new Date(result);
if (nowMs < createdAt.getTime() + this.options.requestIdExpirationPeriodMs)
foundValidInResponseTo = true;
}
await util.promisify(this.cacheProvider.remove).bind(this.cacheProvider)(
inResponseTo
);
await this.cacheProvider.removeAsync(inResponseTo);
if (!foundValidInResponseTo) {
throw new Error("InResponseTo is not valid");
}
break getInResponseTo;
}
}
} else {
await util.promisify(this.cacheProvider.remove).bind(this.cacheProvider)(inResponseTo);
await this.cacheProvider.removeAsync(inResponseTo);
break getInResponseTo;
}
} else {
Expand Down
Loading