Skip to content

Commit b640361

Browse files
tjleingmzraghibmattcreaser
authored
fix(liveness): Added Rekognition backend for Android app and updated README (#59)
Co-authored-by: Zuhayr Raghib <[email protected]> Co-authored-by: Matt Creaser <[email protected]>
1 parent 53f6bcb commit b640361

File tree

5 files changed

+203
-11
lines changed

5 files changed

+203
-11
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {
2+
RekognitionClient,
3+
CreateFaceLivenessSessionCommand,
4+
} from '@aws-sdk/client-rekognition';
5+
6+
/**
7+
* @type {import('@types/aws-lambda').APIGatewayProxyHandler}
8+
*/
9+
10+
export const handler = async (event, req) => {
11+
const client = new RekognitionClient({ region: 'us-east-1' });
12+
const command = new CreateFaceLivenessSessionCommand({});
13+
const response = await client.send(command);
14+
15+
return {
16+
statusCode: 200,
17+
headers: {
18+
'Access-Control-Allow-Origin': '*',
19+
'Access-Control-Allow-Headers': '*',
20+
},
21+
body: JSON.stringify({ sessionId: response.SessionId }),
22+
};
23+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "create",
3+
"version": "2.0.0",
4+
"main": "index.js",
5+
"license": "Apache-2.0",
6+
"type": "module",
7+
"dependencies": {
8+
"@aws-sdk/client-rekognition": "latest"
9+
},
10+
"devDependencies": {
11+
"@types/aws-lambda": "^8.10.92"
12+
}
13+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {
2+
RekognitionClient,
3+
GetFaceLivenessSessionResultsCommand,
4+
} from '@aws-sdk/client-rekognition';
5+
6+
/**
7+
* @type {import('@types/aws-lambda').APIGatewayProxyHandler}
8+
*/
9+
10+
export const handler = async (event, req) => {
11+
console.log({ req });
12+
console.log({ event });
13+
const client = new RekognitionClient({ region: 'us-east-1' });
14+
const command = new GetFaceLivenessSessionResultsCommand({
15+
SessionId: event.pathParameters.sessionId,
16+
});
17+
const response = await client.send(command);
18+
19+
const isLive = response.Confidence > 90;
20+
21+
return {
22+
statusCode: 200,
23+
headers: {
24+
'Access-Control-Allow-Origin': '*',
25+
'Access-Control-Allow-Headers': '*',
26+
},
27+
body: JSON.stringify({
28+
isLive,
29+
confidenceScore: response.Confidence,
30+
auditImageBytes: Buffer.from(
31+
new Uint8Array(Object.values(response.ReferenceImage.Bytes))
32+
).toString('base64'),
33+
}),
34+
};
35+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "getresults",
3+
"version": "2.0.0",
4+
"main": "index.js",
5+
"license": "Apache-2.0",
6+
"type": "module",
7+
"dependencies": {
8+
"@aws-sdk/client-rekognition": "latest"
9+
},
10+
"devDependencies": {
11+
"@types/aws-lambda": "^8.10.92"
12+
}
13+
}

samples/liveness/README.md

Lines changed: 119 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ amplify init
2626
Provide the responses shown after each of the following prompts.
2727
```
2828
? Enter a name for the environment
29-
`dev`
29+
`dev`
3030
? Choose your default editor:
31-
`Android Studio`
31+
`Android Studio`
3232
? Where is your Res directory:
33-
`app/src/main/res`
33+
`app/src/main/res`
3434
? Select the authentication method you want to use:
35-
`AWS profile`
35+
`AWS profile`
3636
? Please choose the profile you want to use
37-
`default`
37+
`default`
3838
```
3939
Wait until provisioning is finished. Upon successfully running `amplify init`, you will see a configuration file created in `./app/src/main/res/raw/` called `amplifyconfiguration.json`. This file will be bundled into your application so that the Amplify libraries know how to reach your provisioned backend resources at runtime.
4040

@@ -65,12 +65,11 @@ Provide the responses shown after each of the following prompts.
6565
? Select the social providers you want to configure for your user pool:
6666
`<hit enter>`
6767
```
68-
4. Update the `AndroidManifest.xml` file in your project according to the steps [here](https://docs.amplify.aws/lib/auth/signin_web_ui/q/platform/android/#update-androidmanifestxml).
69-
5. Once finished, run `amplify push` to publish your changes.
68+
4. Once finished, run `amplify push` to publish your changes.
7069
Upon completion, `amplifyconfiguration.json` should be updated to reference these provisioned backend resources.
71-
6. Follow the steps below to create an inline policy to enable authenticated app users to access Rekognition, which powers the FaceLivenessDetector.
70+
5. Follow the steps below to create an inline policy to enable authenticated app users to access Rekognition, which powers the FaceLivenessDetector.
7271
1. Go to AWS IAM console, then Roles
73-
2. Select the newly created `unauthRole` for the project (`amplify-<project_name>-<env_name>-<id>-authRole`).
72+
2. Select the newly created `authRole` for the project (`amplify-<project_name>-<env_name>-<id>-authRole`).
7473
3. Choose **Add Permissions**, then select **Create Inline Policy**, then choose **JSON** and paste the following:
7574

7675
```
@@ -90,8 +89,117 @@ Provide the responses shown after each of the following prompts.
9089
5. Name the policy
9190
6. Choose **Create Policy**
9291
93-
7. Set up a backend to create the liveness session and retrieve the liveness session results. The liveness sample app is set up to use API Gateway endpoints for creating and retrieving the liveness session. Follow the [Amazon Rekognition Liveness guide](https://docs.aws.amazon.com/rekognition/latest/dg/face-liveness-programming-api.html) to set up your backend and edit the [LivenessSampleBackend class](https://github.com/aws-amplify/amplify-ui-android/blob/main/samples/liveness/app/src/main/java/com/amplifyframework/ui/sample/liveness/LivenessSampleBackend.kt) in your project as necessary to work with your backend.
92+
### Provision Backend API
93+
Set up a backend API using [Amplify API category](https://docs.amplify.aws/lib/restapi/getting-started/q/platform/android/) to create the liveness session and retrieve the liveness session results. The liveness sample app is set up to use API Gateway endpoints for creating and retrieving the liveness session.
9494
95+
1. Run the following command to create a new REST API:
96+
```
97+
amplify add api
98+
```
99+
Provide the responses shown after each of the following prompts.
100+
```
101+
? Please select from one of the below mentioned services
102+
`REST`
103+
? Would you like to add a new path to an existing REST API:
104+
`N`
105+
? Provide a friendly name for your resource to be used as a label for this category in the project:
106+
`livenessBackendAPI`
107+
? Provide a path (e.g., /book/{isbn}):
108+
`/liveness/create`
109+
? Choose a Lambda source
110+
`Create a new Lambda function`
111+
? Provide an AWS Lambda function name:
112+
`createSession`
113+
? Choose the runtime that you want to use:
114+
`NodeJS`
115+
? Choose the function template that you want to use:
116+
`Serverless ExpressJS function (Integration with API Gateway)`
117+
? Do you want to configure advanced settings?
118+
`N`
119+
? Do you want to edit the local lambda function now?
120+
`Y`
121+
? Restrict API access?
122+
`Y`
123+
? Who should have access?
124+
`N`
125+
? Do you want to configure advanced settings?
126+
`Authenticated users only`
127+
? What permissions do you want to grant to Authenticated users?
128+
`create,read,update`
129+
? Do you want to add another path?
130+
`Y`
131+
? Provide a path (e.g., /book/{isbn}):
132+
`/liveness/{sessionId}`
133+
? Choose a Lambda source
134+
`Create a new Lambda function`
135+
? Provide an AWS Lambda function name:
136+
`getResults`
137+
? Choose the runtime that you want to use:
138+
`NodeJS`
139+
? Choose the function template that you want to use:
140+
`Serverless ExpressJS function (Integration with API Gateway)`
141+
? Do you want to configure advanced settings?
142+
`N`
143+
? Do you want to edit the local lambda function now?
144+
`Y`
145+
? Restrict API access?
146+
`Y`
147+
? Who should have access?
148+
`Authenticated users only`
149+
? What permissions do you want to grant to Authenticated users?
150+
`create,read,update`
151+
? Do you want to add another path?
152+
`N`
153+
```
154+
2. Copy the code for from amplify-ui-android/samples/backend-lambda-functions to the path provided
155+
3. Once finished, run `amplify push` to publish your changes.
156+
4. Follow the steps below to create an inline policy to enable the **createSession** lambda function to access Rekognition.
157+
1. Go to AWS Lambda console -> **CreateSession** -> Configuration -> Permissions
158+
2. Click the role name under 'Execution role'
159+
3. Choose **Add Permissions**, then select **Create Inline Policy**, then choose **JSON** and paste the following:
160+
161+
```
162+
{
163+
"Version": "2012-10-17",
164+
"Statement": [
165+
{
166+
"Effect": "Allow",
167+
"Action": "rekognition:CreateFaceLivenessSession",
168+
"Resource": "*"
169+
}
170+
]
171+
}
172+
```
173+
4. Choose **Review Policy**
174+
5. Name the policy
175+
6. Choose **Create Policy**
176+
5. Follow the steps below to create an inline policy to enable the **getResults** lambda function to access Rekognition.
177+
1. Go to AWS Lambda console -> **getResults** -> Configuration -> Permissions
178+
2. Click the role name under 'Execution role'
179+
3. Choose **Add Permissions**, then select **Create Inline Policy**, then choose **JSON** and paste the following:
180+
181+
```
182+
{
183+
"Version": "2012-10-17",
184+
"Statement": [
185+
{
186+
"Effect": "Allow",
187+
"Action": "rekognition:GetFaceLivenessSessionResults",
188+
"Resource": "*"
189+
}
190+
]
191+
}
192+
```
193+
4. Choose **Review Policy**
194+
5. Name the policy
195+
6. Choose **Create Policy**
95196
### Run the App
96197
97-
Build and run the project on an Android device in Android Studio. The project requires Android SDK API level 24 (Android 7.0) or higher.
198+
Delete the generated API files app/src/main/java/[YOUR_API_NAME]
199+
200+
You may need to go to File -> Sync Project with Gradle Files if you get an error "SDK location not found".
201+
202+
Build and run the project on an Android device in Android Studio.
203+
204+
205+
The project requires Android SDK API level 24 (Android 7.0) or higher.

0 commit comments

Comments
 (0)