-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
1,110 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class SAMLResponseNotFound extends Error { | ||
constructor() { | ||
super('SAML Response not found'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { chromium } from 'playwright-core'; | ||
import { ConfirmLogin2Request, RequestLogin2Request } from './types'; | ||
import { SAMLResponseNotFound } from './errors'; | ||
import { apiConnect } from '../../tunnel-api-connect'; | ||
import { performSSOVerification } from '../../../endpoints/performSSOVerification'; | ||
|
||
interface ConfidentialSSOParams { | ||
requestedLogin: string; | ||
} | ||
|
||
export const doConfidentialSSOVerification = async ({ requestedLogin }: ConfidentialSSOParams) => { | ||
const api = await apiConnect({ isProduction: true, enclavePcrList: [] }); | ||
const requestLoginResponse = await api.sendSecureContent<RequestLogin2Request>({ | ||
...api, | ||
path: 'authentication/RequestLogin2', | ||
payload: { login: requestedLogin }, | ||
}); | ||
|
||
const { idpAuthorizeUrl, spCallbackUrl, teamUuid, domainName } = requestLoginResponse; | ||
|
||
const browser = await chromium.launch({ headless: false, channel: 'chrome' }); | ||
const context = await browser.newContext(); | ||
const page = await context.newPage(); | ||
|
||
await page.goto(idpAuthorizeUrl); | ||
|
||
let samlResponseData; | ||
const samlResponsePromise = new Promise((resolve) => { | ||
page.on('request', (req) => { | ||
const reqURL = req.url(); | ||
if (reqURL === spCallbackUrl) { | ||
samlResponseData = req.postData(); | ||
if (browser) { | ||
void browser.close(); | ||
} | ||
resolve(undefined); | ||
} | ||
}); | ||
}); | ||
|
||
await samlResponsePromise; | ||
|
||
const samlResponse = new URLSearchParams(samlResponseData).get('SAMLResponse'); | ||
|
||
if (!samlResponse) { | ||
throw new SAMLResponseNotFound(); | ||
} | ||
|
||
const confirmLoginResponse = await api.sendSecureContent<ConfirmLogin2Request>({ | ||
...api, | ||
path: 'authentication/ConfirmLogin2', | ||
payload: { teamUuid, domainName, samlResponse }, | ||
}); | ||
|
||
const ssoVerificationResult = await performSSOVerification({ | ||
login: requestedLogin, | ||
ssoToken: confirmLoginResponse.ssoToken, | ||
}); | ||
|
||
return { ...ssoVerificationResult, ssoSpKey: confirmLoginResponse.userServiceProviderKey }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export interface RequestLogin2Data { | ||
login: string; | ||
} | ||
|
||
export interface RequestLogin2Output { | ||
domainName: string; | ||
idpAuthorizeUrl: string; | ||
spCallbackUrl: string; | ||
teamUuid: string; | ||
validatedDomains: string[]; | ||
} | ||
|
||
export interface RequestLogin2Request { | ||
path: 'authentication/RequestLogin2'; | ||
input: RequestLogin2Data; | ||
output: RequestLogin2Output; | ||
} | ||
|
||
export interface ConfirmLogin2Data { | ||
teamUuid: string; | ||
domainName: string; | ||
samlResponse: string; | ||
} | ||
|
||
export interface ConfirmLogin2Output { | ||
ssoToken: string; | ||
userServiceProviderKey: string; | ||
exists: boolean; | ||
currentAuthenticationMethods: string[]; | ||
expectedAuthenticationMethods: string[]; | ||
} | ||
|
||
export interface ConfirmLogin2Request { | ||
path: 'authentication/ConfirmLogin2'; | ||
input: ConfirmLogin2Data; | ||
output: ConfirmLogin2Output; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as sodium from 'libsodium-wrappers'; | ||
import { clientHello, terminateHello, SendSecureContentParams, sendSecureContent } from './steps'; | ||
import { ApiConnectParams, ApiConnect, ApiData, ApiRequestsDefault } from './types'; | ||
import { makeClientKeyPair, makeOrRefreshSession } from './utils'; | ||
|
||
/** Type predicates | ||
* https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates | ||
* | ||
* From Partial<ApiData> to ApiData | ||
*/ | ||
const hasFullApiData = (data: Partial<ApiData>): data is ApiData => { | ||
if (data.clientHello && data.terminateHello) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
/** Return an object that can be used to send secure content through the tunnel | ||
*/ | ||
export const apiConnect = async (apiParametersIn: ApiConnectParams): Promise<ApiConnect> => { | ||
await sodium.ready; | ||
|
||
const apiParameters = { | ||
...apiParametersIn, | ||
...{ clientKeyPair: apiParametersIn.clientKeyPair ?? makeClientKeyPair() }, | ||
}; | ||
|
||
const apiData: Partial<ApiData> = {}; | ||
const api: ApiConnect = { | ||
apiData, | ||
apiParameters, | ||
clientHello: () => clientHello(apiParameters), | ||
terminateHello: ({ attestation }: { attestation: Buffer }, apiData: Partial<ApiData>) => | ||
terminateHello({ ...apiParameters, attestation }, apiData), | ||
makeOrRefreshSession, | ||
sendSecureContent: async <R extends ApiRequestsDefault>( | ||
params: Pick<SendSecureContentParams<R>, 'path' | 'payload'> | ||
) => { | ||
await api.makeOrRefreshSession({ api, apiData }); | ||
if (!hasFullApiData(apiData)) { | ||
throw new Error('ShouldNotHappen'); | ||
} | ||
return sendSecureContent({ ...apiParameters, ...apiData.terminateHello, ...params }, apiData); | ||
}, | ||
}; | ||
return api; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export class HTTPError extends Error { | ||
constructor( | ||
readonly statusCode: number, | ||
readonly message: string | ||
) { | ||
super(`HTTP error: ${statusCode}`); | ||
} | ||
} | ||
|
||
export class ApiError extends Error { | ||
constructor( | ||
readonly status: string, | ||
readonly code: string, | ||
readonly message: string | ||
) { | ||
super(`Api error: ${code}`); | ||
} | ||
} | ||
|
||
export class SecureTunnelNotInitialized extends Error { | ||
constructor() { | ||
super('Secure tunnel not initialized'); | ||
} | ||
} | ||
|
||
export class SendSecureContentDataDecryptionError extends Error { | ||
constructor() { | ||
super('Send secure content data decryption error'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './apiconnect'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import sodium from 'libsodium-wrappers'; | ||
import type { ClientHelloParsedResponse, ClientHelloRequest, ClientHelloResponse } from './types'; | ||
import { clientHelloResponseSchema } from './schemas'; | ||
import type { ApiConnectInternalParams } from '../types'; | ||
import { TypeCheck, TypeCheckError } from '../../typecheck'; | ||
import { requestAppApi } from '../../../requestApi'; | ||
|
||
export const clientHelloRequestSchemaValidator = new TypeCheck<ClientHelloResponse>(clientHelloResponseSchema); | ||
|
||
export const clientHello = async (params: ApiConnectInternalParams): Promise<ClientHelloParsedResponse> => { | ||
const { clientKeyPair } = params; | ||
|
||
const payload = { | ||
clientPublicKey: sodium.to_hex(clientKeyPair.publicKey), | ||
} satisfies ClientHelloRequest; | ||
|
||
const response = await requestAppApi<ClientHelloResponse>({ | ||
path: `tunnel/ClientHello`, | ||
payload, | ||
isNitroEncryptionService: true, | ||
}); | ||
|
||
const validated = clientHelloRequestSchemaValidator.validate(response); | ||
if (validated instanceof TypeCheckError) { | ||
throw validated; | ||
} | ||
|
||
return { | ||
attestation: Buffer.from(validated.attestation, 'hex'), | ||
tunnelUuid: validated.tunnelUuid, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './clientHello'; | ||
export * from './sendSecureContent'; | ||
export * from './terminateHello'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { JSONSchema4 } from 'json-schema'; | ||
|
||
/** | ||
* https://docs.aws.amazon.com/enclaves/latest/user/verify-root.html | ||
* Attestation document specification | ||
* - user_data = bytes .size (0..1024) | ||
* To accommodate base64 encoding 1024 * 1.3 ~= 1332 | ||
*/ | ||
export const attestationUserDataSchema: JSONSchema4 = { | ||
type: 'object', | ||
description: 'User data from verifyAttestation', | ||
properties: { | ||
publicKey: { | ||
type: 'string', | ||
base64: true, | ||
maxLength: 1500, | ||
minLength: 4, | ||
}, | ||
header: { | ||
type: 'string', | ||
base64: true, | ||
maxLength: 1500, | ||
minLength: 4, | ||
}, | ||
}, | ||
required: ['publicKey', 'header'], | ||
additionalProperties: false, | ||
}; | ||
|
||
export const clientHelloResponseSchema: JSONSchema4 = { | ||
type: 'object', | ||
properties: { | ||
attestation: { | ||
type: 'string', | ||
pattern: '^[A-Fa-f0-9]+$', | ||
description: 'NSM enclave attestation in hexadecimal format', | ||
}, | ||
tunnelUuid: { | ||
type: 'string', | ||
description: 'The UUID of the tunnel used for the cryptographic session', | ||
}, | ||
}, | ||
required: ['attestation', 'tunnelUuid'], | ||
additionalProperties: false, | ||
}; | ||
|
||
export const secureContentBodyDataSchema: JSONSchema4 = { | ||
type: 'object', | ||
description: 'Send secure content data', | ||
properties: { | ||
encryptedData: { | ||
type: 'string', | ||
// TODO: Extends AJV with an `encoding` keyword to support base64 | hex | ||
pattern: '^[A-Fa-f0-9]+$', | ||
}, | ||
}, | ||
required: ['encryptedData'], | ||
additionalProperties: false, | ||
}; |
Oops, something went wrong.