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

fix(liveness): API to add liveness version to websocket #2572

Merged
merged 8 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amplifyframework.predictions

import com.amplifyframework.annotations.InternalAmplifyApi

@InternalAmplifyApi
enum class AWSPredictionsMetadataType(val key: String) {
Liveness("liveness")
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.amplifyframework.auth.CognitoCredentialsProvider;
import com.amplifyframework.core.Action;
import com.amplifyframework.core.Consumer;
import com.amplifyframework.predictions.AWSPredictionsMetadataType;
import com.amplifyframework.predictions.PredictionsException;
import com.amplifyframework.predictions.PredictionsPlugin;
import com.amplifyframework.predictions.aws.models.AWSVoiceType;
Expand Down Expand Up @@ -61,6 +62,8 @@

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand All @@ -72,6 +75,8 @@
public final class AWSPredictionsPlugin extends PredictionsPlugin<AWSPredictionsEscapeHatch> {
private static final String AWS_PREDICTIONS_PLUGIN_KEY = "awsPredictionsPlugin";

private static Map<String, String> userAgentPairs = new HashMap<>();

private final ExecutorService executorService;

private AWSPredictionsPluginConfiguration configuration;
Expand Down Expand Up @@ -100,6 +105,16 @@ public String getPluginKey() {
return AWS_PREDICTIONS_PLUGIN_KEY;
}

/**
* Add version to liveness websocket user agent.
* @param type The type of version we're adding
* @param value The version
*/
@InternalAmplifyApi
public static void addToUserAgent(AWSPredictionsMetadataType type, String value) {
tylerjroach marked this conversation as resolved.
Show resolved Hide resolved
userAgentPairs.put(type.name(), value);
}

@Override
public void configure(JSONObject pluginConfiguration, @NonNull Context context) throws PredictionsException {
this.configuration = AWSPredictionsPluginConfiguration.fromJson(pluginConfiguration);
Expand Down Expand Up @@ -358,6 +373,6 @@ public static void startFaceLivenessSession(@NonNull String sessionId,
.convertToSdkCredentialsProvider(awsCredentialsProvider);
}
new RunFaceLivenessSession(sessionId, sessionInformation, credentialsProvider,
onSessionStarted, onComplete, onError);
userAgentPairs, onSessionStarted, onComplete, onError);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ internal class LivenessWebSocket(
val endpoint: String,
val region: String,
val sessionInformation: FaceLivenessSessionInformation,
val userAgentPairs: Map<String, String> = mapOf(),
val onSessionInformationReceived: Consumer<SessionInformation>,
val onErrorReceived: Consumer<PredictionsException>,
val onComplete: Action
Expand Down Expand Up @@ -201,8 +202,15 @@ internal class LivenessWebSocket(
val amplifyVersion = BuildConfig.VERSION_NAME
val deviceManufacturer = Build.MANUFACTURER.replace(" ", "_")
val deviceName = Build.MODEL.replace(" ", "_")
val userAgent = "${UserAgent.string()} os/Android/${Build.VERSION.SDK_INT} md/device/$deviceName " +
var userAgent = "${UserAgent.string()} os/Android/${Build.VERSION.SDK_INT} md/device/$deviceName " +
"md/device-manufacturer/$deviceManufacturer api/rekognitionstreaming/$amplifyVersion"

userAgentPairs.forEach { kv ->
tjleing marked this conversation as resolved.
Show resolved Hide resolved
val key = kv.key
val value = kv.value
userAgent += " $key:$value"
}

return userAgent.replace(Build.MANUFACTURER, deviceManufacturer).replace(Build.MODEL, deviceName)
.replace("+", "_")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ internal class RunFaceLivenessSession(
sessionId: String,
sessionInformation: FaceLivenessSessionInformation,
val credentialsProvider: CredentialsProvider,
val userAgentPairs: Map<String, String> = mapOf(),
onSessionStarted: Consumer<FaceLivenessSession>,
onComplete: Action,
onError: Consumer<PredictionsException>
Expand All @@ -55,6 +56,7 @@ internal class RunFaceLivenessSession(
"${sessionInformation.videoWidth.toInt()}&video-height=${sessionInformation.videoHeight.toInt()}",
region = sessionInformation.region,
sessionInformation = sessionInformation,
userAgentPairs = userAgentPairs,
onSessionInformationReceived = { sessionInformation ->
val challenges = processSessionInformation(sessionInformation)
val faceLivenessSession = FaceLivenessSession(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ internal class LivenessWebSocketTest {
server.url("/").toString(),
"",
sessionInformation,
mapOf(),
tjleing marked this conversation as resolved.
Show resolved Hide resolved
onSessionInformationReceived,
onErrorReceived,
onComplete
Expand Down
Loading