Skip to content

remove old callback functions, tests use async/await #545

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

Merged
merged 3 commits into from
Feb 23, 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
122 changes: 2 additions & 120 deletions src/passport-saml/saml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ async function processValidlySignedPostRequestAsync(
} else {
throw new Error("Missing SAML issuer");
}
const nameID = await util.promisify(self.getNameID).bind(self)(self, dom);
const nameID = await self.getNameIDAsync(self, dom);
if (nameID) {
profile.nameID = nameID.value;
profile.nameID = nameID.value!;
if (nameID.format) {
profile.nameIDFormat = nameID.format;
}
Expand Down Expand Up @@ -250,21 +250,6 @@ class SAML {
samlMessage.Signature = signer.sign(this.keyToPEM(this.options.privateKey), "base64");
}

/**
*
* @deprecated
*/
generateAuthorizeRequest(
req: Request,
isPassive: boolean,
isHttpPostBinding: boolean,
callback: (err: Error | null, request?: string) => void
) {
return util.callbackify(() =>
this.generateAuthorizeRequestAsync(req, isPassive, isHttpPostBinding)
)(callback);
}

async generateAuthorizeRequestAsync(
req: Request,
isPassive: boolean,
Expand Down Expand Up @@ -466,20 +451,6 @@ class SAML {
return xmlbuilder.create(request).end();
}

/**
* @deprecated
*/
requestToUrl(
request: string | null | undefined,
response: string | null,
operation: string,
additionalParameters: querystring.ParsedUrlQuery,
callback: (err: Error | null, url?: string | null | undefined) => void
) {
util.callbackify(() =>
this.requestToUrlAsync(request, response, operation, additionalParameters)
)(callback);
}
async requestToUrlAsync(
request: string | null | undefined,
response: string | null,
Expand Down Expand Up @@ -570,17 +541,6 @@ class SAML {
return additionalParams;
}

/**
* @deprecated
*/
getAuthorizeUrl(
req: Request,
options: AuthorizeOptions,
callback: (err: Error | null, url: string) => void
): void {
util.callbackify(() => this.getAuthorizeUrlAsync(req, options))(callback);
}

async getAuthorizeUrlAsync(req: Request, options: AuthorizeOptions): Promise<string> {
const request = await this.generateAuthorizeRequestAsync(req, this.options.passive, false);
const operation = "authorize";
Expand All @@ -593,12 +553,6 @@ class SAML {
);
}

/**
* @deprecated
*/
getAuthorizeForm(req: Request, callback: (err: Error | null, data?: unknown) => void) {
util.callbackify(() => this.getAuthorizeFormAsync(req))(callback);
}
async getAuthorizeFormAsync(req: Request) {
// The quoteattr() function is used in a context, where the result will not be evaluated by javascript
// but must be interpreted by an XML or HTML parser, and it must absolutely avoid breaking the syntax
Expand Down Expand Up @@ -675,16 +629,6 @@ class SAML {
].join("\r\n");
}

/**
* @deprecated
*/
getLogoutUrl(
req: RequestWithUser,
options: AuthenticateOptions & AuthorizeOptions,
callback: (err: Error | null, url?: string | null) => void
) {
util.callbackify(() => this.getLogoutUrlAsync(req, options))(callback);
}
async getLogoutUrlAsync(req: RequestWithUser, options: AuthenticateOptions & AuthorizeOptions) {
const request = await this.generateLogoutRequest(req);
const operation = "logout";
Expand Down Expand Up @@ -813,18 +757,6 @@ class SAML {
return sig.checkSignature(fullXml);
}

/**
* @deprecated
*/
validatePostResponse(
container: Record<string, string>,
callback: (err: Error | null, profile?: Profile | null, loggedOut?: boolean) => void
) {
this.validatePostResponseAsync(container)
.then((res) => callback(null, res.profile, res.loggedOut))
.catch((err) => callback(err));
}

async validatePostResponseAsync(
container: Record<string, string>
): Promise<{ profile?: Profile | null; loggedOut?: boolean }> {
Expand Down Expand Up @@ -1007,19 +939,6 @@ class SAML {
}
}

/**
* @deprecated
*/
validateRedirect(
container: ParsedQs,
originalQuery: string | null,
callback: (err: Error | null, profile?: Profile | null, loggedOut?: boolean) => void
) {
this.validateRedirectAsync(container, originalQuery)
.then((res) => callback(null, res.profile, res.loggedOut))
.catch((err) => callback(err));
}

async validateRedirectAsync(
container: ParsedQs,
originalQuery: string | null
Expand Down Expand Up @@ -1151,24 +1070,6 @@ class SAML {
}
}

/**
* @deprecated
*/
processValidlySignedAssertion(
xml: xml2js.convertableToString,
samlResponseXml: string,
inResponseTo: string,
callback: (
err: Error | null,
profile?: Profile | undefined,
loggedOut?: boolean | undefined
) => void
) {
this.processValidlySignedAssertionAsync(xml, samlResponseXml, inResponseTo)
.then((res) => callback(null, res.profile, res.loggedOut))
.catch((err) => callback(err));
}

async processValidlySignedAssertionAsync(
xml: xml2js.convertableToString,
samlResponseXml: string,
Expand Down Expand Up @@ -1391,18 +1292,6 @@ class SAML {
return null;
}

/**
* @deprecated
*/
validatePostRequest(
container: Record<string, string>,
callback: (err: Error | null, profile?: Profile, loggedOut?: boolean) => void
) {
this.validatePostRequestAsync(container)
.then((res) => callback(null, res.profile, res.loggedOut))
.catch((err) => callback(err));
}

async validatePostRequestAsync(
container: Record<string, string>
): Promise<{ profile?: Profile; loggedOut?: boolean }> {
Expand All @@ -1422,13 +1311,6 @@ class SAML {
return await processValidlySignedPostRequestAsync(this, doc, dom);
}

/**
* @deprecated
*/
getNameID(self: SAML, doc: Node, callback: (err: Error | null, nameID?: XMLOutput) => void) {
return util.callbackify(() => this.getNameIDAsync(self, doc)).bind(this)(callback);
}

async getNameIDAsync(self: SAML, doc: Node): Promise<NameID> {
const nameIds = xmlCrypto.xpath(
doc,
Expand Down
66 changes: 41 additions & 25 deletions src/passport-saml/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ class Strategy extends PassportStrategy {

authenticate(req: RequestWithUser, options: AuthenticateOptions): void {
options.samlFallback = options.samlFallback || "login-request";

const validateCallback = (err: Error | null, profile?: Profile | null, loggedOut?: boolean) => {
if (err) {
return this.error(err);
}

const validateCallback = ({
profile,
loggedOut,
}: {
profile?: Profile | null;
loggedOut?: boolean;
}) => {
if (loggedOut) {
req.logout();
if (profile) {
Expand Down Expand Up @@ -93,30 +94,42 @@ class Strategy extends PassportStrategy {

if (req.query && (req.query.SAMLResponse || req.query.SAMLRequest)) {
const originalQuery = url.parse(req.url).query;
this._saml.validateRedirect(req.query, originalQuery, validateCallback);
this._saml
.validateRedirectAsync(req.query, originalQuery)
.then(validateCallback)
.catch((err) => this.error(err));
} else if (req.body && req.body.SAMLResponse) {
this._saml.validatePostResponse(req.body, validateCallback);
this._saml
.validatePostResponseAsync(req.body)
.then(validateCallback)
.catch((err) => this.error(err));
} else if (req.body && req.body.SAMLRequest) {
this._saml.validatePostRequest(req.body, validateCallback);
this._saml
.validatePostRequestAsync(req.body)
.then(validateCallback)
.catch((err) => this.error(err));
} else {
const requestHandler = {
"login-request": () => {
if (this._saml.options.authnRequestBinding === "HTTP-POST") {
this._saml.getAuthorizeForm(req, (err: Error | null, data?: any) => {
if (err) {
this.error(err);
} else {
const res = req.res!;
res.send(data);
}
});
} else {
// Defaults to HTTP-Redirect
this._saml.getAuthorizeUrl(req, options, redirectIfSuccess);
"login-request": async () => {
try {
if (this._saml.options.authnRequestBinding === "HTTP-POST") {
const data = await this._saml.getAuthorizeFormAsync(req);
const res = req.res!;
res.send(data);
} else {
// Defaults to HTTP-Redirect
this.redirect(await this._saml.getAuthorizeUrlAsync(req, options));
}
} catch (err) {
this.error(err);
}
},
"logout-request": () => {
this._saml.getLogoutUrl(req, options, redirectIfSuccess);
"logout-request": async () => {
try {
this.redirect(await this._saml.getLogoutUrlAsync(req, options));
} catch (err) {
this.error(err);
}
},
}[options.samlFallback];

Expand All @@ -129,7 +142,10 @@ class Strategy extends PassportStrategy {
}

logout(req: RequestWithUser, callback: (err: Error | null, url?: string | null) => void): void {
this._saml.getLogoutUrl(req, {}, callback);
this._saml
.getLogoutUrlAsync(req, {})
.then((url) => callback(null, url))
.catch((err) => callback(err));
}

generateServiceProviderMetadata(
Expand Down
4 changes: 2 additions & 2 deletions test/multiSamlStrategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ describe("MultiSamlStrategy#authenticate", function () {

describe("MultiSamlStrategy#authorize", function () {
beforeEach(function () {
this.getAuthorizeFormStub = sinon.stub(SAML.prototype, "getAuthorizeForm");
this.getAuthorizeUrlStub = sinon.stub(SAML.prototype, "getAuthorizeUrl");
this.getAuthorizeFormStub = sinon.stub(SAML.prototype, "getAuthorizeFormAsync").resolves();
this.getAuthorizeUrlStub = sinon.stub(SAML.prototype, "getAuthorizeUrlAsync").resolves();
});

afterEach(function () {
Expand Down
Loading