Skip to content

Commit

Permalink
chore(liveness): add region prop to component (#779)
Browse files Browse the repository at this point in the history
* chore(liveness): add region prop to component

* chore: update comment to not list out regions and instead add a link in future
  • Loading branch information
thaddmt authored Mar 10, 2023
1 parent 5f7bfa1 commit f864e27
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default function LivenessDefault({
{!getLivenessResponse ? (
<FaceLivenessDetector
sessionId={createLivenessSessionApiData.sessionId}
region={'us-east-1'}
onUserCancel={onUserCancel}
onAnalysisComplete={async () => {
const response = await handleGetLivenessDetection(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ describe('LivenessStreamProvider', () => {
test('happy case', () => {
const provider = new LivenessStreamProvider(
'sessionId',
'us-east-1',
mockVideoMediaStream,
mockVideoEl
);
Expand All @@ -126,6 +127,7 @@ describe('LivenessStreamProvider', () => {
test('happy case', async () => {
const provider = new LivenessStreamProvider(
'sessionId',
'us-east-1',
mockVideoMediaStream,
mockVideoEl
);
Expand All @@ -137,6 +139,7 @@ describe('LivenessStreamProvider', () => {
test('happy case', async () => {
const provider = new LivenessStreamProvider(
'sessionId',
'us-east-1',
mockVideoMediaStream,
mockVideoEl
);
Expand All @@ -149,6 +152,7 @@ describe('LivenessStreamProvider', () => {
test('yield video chunk events', async () => {
const provider = new LivenessStreamProvider(
'sessionId',
'us-east-1',
mockVideoMediaStream,
mockVideoEl
);
Expand All @@ -165,6 +169,7 @@ describe('LivenessStreamProvider', () => {
test('does not yield empty video chunks', async () => {
const provider = new LivenessStreamProvider(
'sessionId',
'us-east-1',
mockVideoMediaStream,
mockVideoEl
);
Expand All @@ -185,6 +190,7 @@ describe('LivenessStreamProvider', () => {
test('happy case', async () => {
const provider = new LivenessStreamProvider(
'sessionId',
'us-east-1',
mockVideoMediaStream,
mockVideoEl
);
Expand All @@ -198,6 +204,7 @@ describe('LivenessStreamProvider', () => {
test('should stop sending video events', async () => {
const provider = new LivenessStreamProvider(
'sessionId',
'us-east-1',
mockVideoMediaStream,
mockVideoEl
);
Expand All @@ -211,6 +218,7 @@ describe('LivenessStreamProvider', () => {
test('should dispatch an empty video chunk', async () => {
const provider = new LivenessStreamProvider(
'sessionId',
'us-east-1',
mockVideoMediaStream,
mockVideoEl
);
Expand All @@ -224,6 +232,7 @@ describe('LivenessStreamProvider', () => {
test('should stop video and end the stream and return a promise if cancelled successfully', async () => {
const provider = new LivenessStreamProvider(
'sessionId',
'us-east-1',
mockVideoMediaStream,
mockVideoEl
);
Expand Down
9 changes: 9 additions & 0 deletions packages/ui/src/helpers/liveness/liveness-event-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,12 @@ export const isServiceQuotaExceededExceptionEvent = (
): event is LivenessResponseStream.ServiceQuotaExceededExceptionMember => {
return !!event?.ServiceQuotaExceededException;
};

export const isInvalidSignatureRegionException = (
error: any
): error is Error => {
return (
error.name === 'InvalidSignatureException' &&
(error.message as string).includes('valid region')
);
};
6 changes: 4 additions & 2 deletions packages/ui/src/helpers/liveness/liveness-stream-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export interface StartLivenessStreamOutput {
const ENDPOINT =
process.env.NEXT_PUBLIC_STREAMING_API_URL ||
'wss://streaming-rekognition.us-east-1.amazonaws.com:443';
const REGION = process.env.NEXT_PUBLIC_BACKEND_API_REGION || 'us-east-1';
export const TIME_SLICE = 1000;

export interface Credentials {
Expand All @@ -33,6 +32,7 @@ export interface Credentials {

export class LivenessStreamProvider extends AmazonAIInterpretPredictionsProvider {
public sessionId: string;
public region: string;
public videoRecorder: VideoRecorder;
public responseStream: AsyncIterable<LivenessResponseStream>;

Expand All @@ -44,11 +44,13 @@ export class LivenessStreamProvider extends AmazonAIInterpretPredictionsProvider

constructor(
sessionId: string,
region: string,
stream: MediaStream,
videoEl: HTMLVideoElement
) {
super();
this.sessionId = sessionId;
this.region = region;
this._stream = stream;
this.videoEl = videoEl;
this.videoRecorder = new VideoRecorder(stream);
Expand All @@ -68,7 +70,7 @@ export class LivenessStreamProvider extends AmazonAIInterpretPredictionsProvider
const url = new URL(ENDPOINT);
return { url };
},
region: REGION,
region: this.region,
customUserAgent: getAmplifyUserAgent(),
});

Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/helpers/liveness/liveness-test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export const mockContext = (): LivenessContext => {
failedAttempts: 0,
componentProps: {
sessionId: 'foobar',
region: 'us-east-1',
onAnalysisComplete: jest.fn(),
},
videoAssociatedParams: {
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/machines/liveness/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('Liveness Machine', () => {

const mockcomponentProps: FaceLivenessDetectorProps = {
sessionId: 'some-sessionId',
region: 'us-east-1',
onAnalysisComplete: jest.fn(),
onError: jest.fn(),
onUserCancel: jest.fn(),
Expand Down
11 changes: 10 additions & 1 deletion packages/ui/src/machines/liveness/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
isInternalServerExceptionEvent,
isServerSesssionInformationEvent,
isDisconnectionEvent,
isInvalidSignatureRegionException,
} from '../../helpers/liveness/liveness-event-utils';
import {
ClientSessionInformationEvent,
Expand Down Expand Up @@ -923,6 +924,7 @@ export const livenessMachine = createMachine<LivenessContext, LivenessEvent>(
async openLivenessStreamConnection(context) {
const livenessStreamProvider = new LivenessStreamProvider(
context.componentProps.sessionId,
context.componentProps.region,
context.videoAssociatedParams.videoMediaStream,
context.videoAssociatedParams.videoEl
);
Expand Down Expand Up @@ -1245,9 +1247,16 @@ const responseStreamActor = async (callback) => {
}
}
} catch (error) {
let returnedError = error;
if (isInvalidSignatureRegionException(error)) {
returnedError = new Error(
'Invalid region in FaceLivenessDetector or credentials are scoped to the wrong region.'
);
}

callback({
type: 'SERVER_ERROR',
data: { error },
data: { error: returnedError },
});
}
};
5 changes: 5 additions & 0 deletions packages/ui/src/types/liveness/liveness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export interface FaceLivenessDetectorProps {
*/
onAnalysisComplete: () => Promise<void>;

/**
* The AWS region to stream the video to, for current regional support see the documentation here: FIXME LINK
*/
region: string;

/**
* Callback called when the user cancels the flow
*/
Expand Down

0 comments on commit f864e27

Please sign in to comment.