From 70fbf57f7b6b776bc1c55bf16caeeca2e9de16fe Mon Sep 17 00:00:00 2001 From: echojoshchen <48116767+echojoshchen@users.noreply.github.com> Date: Wed, 13 Mar 2019 17:48:19 -0400 Subject: [PATCH] Add type defintion for Profile and fix multiSamlStrategy types. (#33851) * Add missing types and fix typings for multiSamlStrategy * Update SamlConfig required fields --- types/passport-saml/index.d.ts | 25 ++++++++++++++++++---- types/passport-saml/multiSamlStrategy.d.ts | 14 ++++++++---- types/passport-saml/passport-saml-tests.ts | 10 +++++---- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/types/passport-saml/index.d.ts b/types/passport-saml/index.d.ts index 24f0effa71..e4846cb19d 100644 --- a/types/passport-saml/index.d.ts +++ b/types/passport-saml/index.d.ts @@ -20,9 +20,9 @@ export interface CacheProvider { remove(key: string, callback: (err: Error | null, key: string) => void | null): void; } -export type VerifiedCallback = (err: Error | null, user: object, info: object) => void; +export type VerifiedCallback = (err: Error | null, user?: object, info?: object) => void; -export type VerifyWithRequest = (req: express.Request, profile: object, done: VerifiedCallback) => void; +export type VerifyWithRequest = (req: express.Request, profile: Profile, done: VerifiedCallback) => void; export type VerifyWithoutRequest = (profile: object, done: VerifiedCallback) => void; @@ -41,8 +41,8 @@ export interface SamlConfig { path?: string; protocol?: string; host?: string; - entryPoint: string; - issuer: string; + entryPoint?: string; + issuer?: string; privateCert?: string; cert?: string | string[] | CertCallback; decryptionPvk?: string; @@ -82,3 +82,20 @@ export interface AuthenticateOptions extends passport.AuthenticateOptions { export interface AuthorizeOptions extends AuthenticateOptions { samlFallback?: string; } + +export type Profile = { + issuer?: string; + sessionIndex?: string; + nameID?: string; + nameIDFormat?: string; + nameQualifier?: string; + spNameQualifier?: string; + ID?: string; + mail?: string; // InCommon Attribute urn:oid:0.9.2342.19200300.100.1.3 + email?: string; // `mail` if not present in the assertion + getAssertionXml(): string; // get the raw assertion XML + getAssertion(): object; // get the assertion XML parsed as a JavaScript object + getSamlResponseXml(): string; // get the raw SAML response XML +} & { + [attributeName: string]: string; // arbitrary `AttributeValue`s +}; diff --git a/types/passport-saml/multiSamlStrategy.d.ts b/types/passport-saml/multiSamlStrategy.d.ts index 405c7264dc..c9d8568aad 100644 --- a/types/passport-saml/multiSamlStrategy.d.ts +++ b/types/passport-saml/multiSamlStrategy.d.ts @@ -1,10 +1,16 @@ import express = require('express'); import { Strategy, SamlConfig, VerifyWithRequest, VerifyWithoutRequest } from './index'; -export interface MultiSamlConfig extends SamlConfig { - getSamlOptions(req: express.Request, callback: (err: Error | null, samlOptions: SamlConfig) => void): void; +declare namespace MultiSamlStrategy { + type SamlOptionsCallback = (err: Error | null, samlOptions?: SamlConfig) => void; + + interface MultiSamlConfig extends SamlConfig { + getSamlOptions(req: express.Request, callback: SamlOptionsCallback): void; + } } -export class MultiSamlStrategy extends Strategy { - constructor(config: MultiSamlConfig, verify: VerifyWithRequest | VerifyWithoutRequest); +declare class MultiSamlStrategy extends Strategy { + constructor(config: MultiSamlStrategy.MultiSamlConfig, verify: VerifyWithRequest | VerifyWithoutRequest); } + +export = MultiSamlStrategy; diff --git a/types/passport-saml/passport-saml-tests.ts b/types/passport-saml/passport-saml-tests.ts index b22421fe81..04901e52a1 100644 --- a/types/passport-saml/passport-saml-tests.ts +++ b/types/passport-saml/passport-saml-tests.ts @@ -35,24 +35,26 @@ passport.authenticate('samlCustomName', {failureRedirect: '/', failureFlash: tru const metadata = samlStrategy.generateServiceProviderMetadata("decryptionCert"); -const multiSamlStrategy = new MultiSamlStrategy.MultiSamlStrategy( +const multiSamlStrategy = new MultiSamlStrategy( { name: 'samlCustomName', path: '/login/callback', entryPoint: 'https://openidp.feide.no/simplesaml/saml2/idp/SSOService.php', issuer: 'passport-saml', - getSamlOptions(req: express.Request, callback: (err: Error | null, samlOptions: SamlStrategy.SamlConfig) => void) { + getSamlOptions(req: express.Request, callback: MultiSamlStrategy.SamlOptionsCallback) { callback(null, { name: 'samlCustomName', path: '/login/callback2', entryPoint: 'https://openidp.feide.no/simplesaml/saml2/idp/SSOService.php', issuer: 'passport-saml', - }); + }); + callback(new Error("SAML Options Error")); } }, - (profile: {}, done: (err: Error | null, user: {}, info?: {}) => void) => { + (profile: {}, done: (err: Error | null, user?: {}, info?: {}) => void) => { const user = {}; done(null, user); + done(new Error("Verify Request Error")); } );