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

Use cert from metadata to validate response #1908

Merged
merged 6 commits into from
Nov 10, 2023
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
64 changes: 25 additions & 39 deletions npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@aws-sdk/util-dynamodb": "3.445.0",
"@boxyhq/error-code-mnemonic": "0.1.1",
"@boxyhq/metrics": "0.2.5",
"@boxyhq/saml20": "1.3.0",
"@boxyhq/saml20": "1.3.1",
"@googleapis/admin": "13.0.0",
"axios": "1.6.0",
"encoding": "0.1.13",
Expand Down
11 changes: 8 additions & 3 deletions npm/src/controller/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import * as codeVerifier from './oauth/code-verifier';
import * as redirect from './oauth/redirect';
import { getDefaultCertificate } from '../saml/x509';
import { SAMLHandler } from './saml-handler';
import { extractSAMLResponseAttributes } from '../saml/lib';
import { ValidateOption, extractSAMLResponseAttributes } from '../saml/lib';
import { oidcIssuerInstance } from './oauth/oidc-issuer';

const deflateRawAsync = promisify(deflateRaw);
Expand Down Expand Up @@ -494,7 +494,7 @@ export class OAuthController implements IOAuthController {
let issuer: string | undefined;
let isIdPFlow: boolean | undefined;
let isSAMLFederated: boolean | undefined;
let validateOpts: { thumbprint: string; audience: string; privateKey: string };
let validateOpts: ValidateOption;
let redirect_uri: string | undefined;
const { SAMLResponse, idp_hint, RelayState = '' } = body;

Expand Down Expand Up @@ -587,11 +587,16 @@ export class OAuthController implements IOAuthController {
const { privateKey } = await getDefaultCertificate();

validateOpts = {
thumbprint: `${connection?.idpMetadata.thumbprint}`,
audience: `${this.opts.samlAudience}`,
privateKey,
};

if (connection.idpMetadata.publicKey) {
validateOpts.publicKey = connection.idpMetadata.publicKey;
} else if (connection.idpMetadata.thumbprint) {
validateOpts.thumbprint = connection.idpMetadata.thumbprint;
}

if (session && session.id) {
validateOpts['inResponseTo'] = session.id;
}
Expand Down
4 changes: 4 additions & 0 deletions npm/src/controller/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import crypto from 'crypto';
import * as jose from 'jose';
import { Client, TokenSet } from 'openid-client';
import saml from '@boxyhq/saml20';

import * as dbutils from '../db/utils';
import type {
Expand Down Expand Up @@ -68,6 +69,9 @@ export const OAuthErrorResponse = ({

// https://kentcdodds.com/blog/get-a-catch-block-error-message-with-typescript
export function getErrorMessage(error: unknown) {
if (error instanceof saml.WrapError) {
return error.message + ' ' + error.inner.message;
}
if (error instanceof Error) {
return error.message;
}
Expand Down
5 changes: 3 additions & 2 deletions npm/src/saml/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,9 @@ export const createSAMLResponse = async ({
);
};

type ValidateOption = {
thumbprint: string;
export type ValidateOption = {
thumbprint?: string;
publicKey?: string;
audience: string;
privateKey: string;
inResponseTo?: string;
Expand Down
1 change: 1 addition & 0 deletions npm/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface SAMLSSORecord extends SAMLSSOConnection {
redirectUrl?: string;
};
thumbprint?: string;
publicKey?: string;
validTo?: string;
};
deactivated?: boolean;
Expand Down
Loading
Loading