-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Face match and liveness implementation (#15)
* add: rekognito and amplify implementation * add: liveness and face match implementation * fix: optional payload * add: `disableInstructionScreen` props to component
- Loading branch information
1 parent
620f234
commit 29037bb
Showing
24 changed files
with
5,614 additions
and
425 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
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
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
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,50 @@ | ||
import { FaceLivenessDetector } from "@aws-amplify/ui-react-liveness"; | ||
import { Loader, ThemeProvider } from "@aws-amplify/ui-react"; | ||
import React from "react"; | ||
|
||
export function LivenessQuickStartReact({ handleNextForm, cedula }: any) { | ||
const next = handleNextForm; | ||
const id = cedula; | ||
const [loading, setLoading] = React.useState<boolean>(true); | ||
const [sessionId, setSessionId] = React.useState<string>(""); | ||
|
||
React.useEffect(() => { | ||
const fetchCreateLiveness = async () => { | ||
const response = await fetch(`/api/biometric`, { method: "POST" }); | ||
const { sessionId } = await response.json(); | ||
|
||
setSessionId(sessionId); | ||
setLoading(false); | ||
}; | ||
|
||
fetchCreateLiveness(); | ||
}, []); | ||
|
||
const handleAnalysisComplete = async () => { | ||
const response = await fetch( | ||
`/api/biometric?sessionId=${sessionId}&cedula=${id}` | ||
); | ||
const data = await response.json(); | ||
|
||
if (data.match) { | ||
next(); | ||
} else { | ||
alert("No se ha podido validar correctamente la identidad."); | ||
} | ||
}; | ||
|
||
return ( | ||
<ThemeProvider> | ||
{loading ? ( | ||
<Loader /> | ||
) : ( | ||
<FaceLivenessDetector | ||
sessionId={sessionId} | ||
region="us-east-1" | ||
onAnalysisComplete={handleAnalysisComplete} | ||
disableInstructionScreen={true} | ||
/> | ||
)} | ||
</ThemeProvider> | ||
); | ||
} |
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
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,2 @@ | ||
export { validateSameSiteRequest } from "./same-site-validator"; | ||
export { getRekognitionClient } from "./rekognition"; |
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,21 @@ | ||
import { Rekognition } from "@aws-sdk/client-rekognition"; | ||
import { Amplify, withSSRContext } from "aws-amplify"; | ||
import { NextApiRequest } from "next/types"; | ||
|
||
import awsExports from "../aws-exports"; | ||
|
||
Amplify.configure({ ...awsExports, ssr: true }); | ||
|
||
export async function getRekognitionClient( | ||
req: NextApiRequest | ||
): Promise<Rekognition> { | ||
const { Credentials } = withSSRContext({ req }); | ||
const credentials = await Credentials.get(); | ||
const endpoint = "https://rekognition.us-east-1.amazonaws.com"; | ||
|
||
return new Rekognition({ | ||
region: "us-east-1", | ||
credentials, | ||
endpoint, | ||
}); | ||
} |
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,10 @@ | ||
import { IncomingHttpHeaders } from "node:http"; | ||
|
||
export function validateSameSiteRequest(headers: IncomingHttpHeaders) { | ||
const fetchHeaderKey = "sec-fetch-site"; | ||
const fetchHeaderValue = "same-origin"; | ||
|
||
return ( | ||
headers[fetchHeaderKey] && headers[fetchHeaderKey] === fetchHeaderValue | ||
); | ||
} |
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,72 @@ | ||
import { NextApiRequest, NextApiResponse } from "next/types"; | ||
import axios from "axios"; | ||
|
||
import { getRekognitionClient, validateSameSiteRequest } from "@/helpers"; | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse<any | void> | ||
): Promise<any> { | ||
const isValidRequest = validateSameSiteRequest(req.headers); | ||
|
||
if (!isValidRequest) { | ||
return res.status(401).send(null); | ||
} | ||
|
||
const http = axios.create({ | ||
baseURL: process.env.NEXT_PUBLIC_PHOTO_API, | ||
}); | ||
|
||
const rekognition = await getRekognitionClient(req); | ||
|
||
if (req.method === "GET") { | ||
const { sessionId, cedula } = req.query; | ||
const SessionId = sessionId as string; | ||
|
||
const response = await rekognition.getFaceLivenessSessionResults({ | ||
SessionId, | ||
}); | ||
|
||
let isLive: any; | ||
let base64Image: string = ""; | ||
let result: any; | ||
|
||
if (response.Confidence) { | ||
isLive = response.Confidence > 90; | ||
} | ||
|
||
if (response.ReferenceImage && response.ReferenceImage.Bytes) { | ||
base64Image = response.ReferenceImage.Bytes.toString(); | ||
|
||
const { data } = await http.get(`/${cedula}/photo`, { | ||
params: { | ||
"api-key": process.env.NEXT_PUBLIC_PHOTO_API_KEY, | ||
}, | ||
responseType: "arraybuffer", | ||
}); | ||
|
||
result = await rekognition.compareFaces({ | ||
SimilarityThreshold: 80, | ||
TargetImage: { | ||
Bytes: data, | ||
}, | ||
SourceImage: { | ||
Bytes: response.ReferenceImage.Bytes, | ||
}, | ||
}); | ||
} | ||
|
||
const { FaceMatches } = result; | ||
const isFaceMatched = | ||
FaceMatches && FaceMatches.length && FaceMatches[0].Similarity > 90; | ||
|
||
return res.status(200).json({ | ||
match: isFaceMatched, | ||
}); | ||
} else if (req.method === "POST") { | ||
const { SessionId: sessionId } = | ||
await rekognition.createFaceLivenessSession({}); | ||
|
||
return res.status(201).json({ sessionId }); | ||
} | ||
} |
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
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
Oops, something went wrong.