Skip to content

Commit

Permalink
async / await in cache interface (#532)
Browse files Browse the repository at this point in the history
* async / await in cache interface
* eslint warnings fixed for cache, remove comments and checks for node < 0.9
  • Loading branch information
gugu committed Feb 10, 2021
1 parent 54809d1 commit 54704de
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 132 deletions.
25 changes: 11 additions & 14 deletions src/passport-saml/inmemory-cache-provider.ts
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);
}
});
}, 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
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

0 comments on commit 54704de

Please sign in to comment.