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 6 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
Expand Up @@ -314,24 +314,27 @@ public InterpretOperation<?> interpret(
* Starts a Liveness session.
* @param sessionId ID for the session to start.
* @param sessionInformation Information about the face liveness session.
* @param livenessVersion The version of liveness, which will be attached to the user agent.
* @param onSessionStarted Called when the face liveness session has been started.
* @param onComplete Called when the session is complete.
* @param onError Called when an error occurs during the session.
*/
@InternalAmplifyApi
public static void startFaceLivenessSession(@NonNull String sessionId,
@NonNull FaceLivenessSessionInformation sessionInformation,
@NonNull String livenessVersion,
@NonNull Consumer<FaceLivenessSession> onSessionStarted,
@NonNull Action onComplete,
@NonNull Consumer<PredictionsException> onError) {
startFaceLivenessSession(sessionId, sessionInformation, FaceLivenessSessionOptions.defaults(),
onSessionStarted, onComplete, onError);
livenessVersion, onSessionStarted, onComplete, onError);
}

/**
* Starts a Liveness session with the given options.
* @param sessionId ID for the session to start.
* @param sessionInformation Information about the face liveness session.
* @param livenessVersion The version of liveness, which will be attached to the user agent.
* @param options The options for this session.
* @param onSessionStarted Called when the face liveness session has been started.
* @param onComplete Called when the session is complete.
Expand All @@ -341,6 +344,7 @@ public static void startFaceLivenessSession(@NonNull String sessionId,
public static void startFaceLivenessSession(@NonNull String sessionId,
@NonNull FaceLivenessSessionInformation sessionInformation,
@NonNull FaceLivenessSessionOptions options,
@NonNull String livenessVersion,
@NonNull Consumer<FaceLivenessSession> onSessionStarted,
@NonNull Action onComplete,
@NonNull Consumer<PredictionsException> onError) {
Expand All @@ -358,6 +362,6 @@ public static void startFaceLivenessSession(@NonNull String sessionId,
.convertToSdkCredentialsProvider(awsCredentialsProvider);
}
new RunFaceLivenessSession(sessionId, sessionInformation, credentialsProvider,
onSessionStarted, onComplete, onError);
livenessVersion, 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 livenessVersion: String = "",
val onSessionInformationReceived: Consumer<SessionInformation>,
val onErrorReceived: Consumer<PredictionsException>,
val onComplete: Action
Expand Down Expand Up @@ -197,12 +198,18 @@ internal class LivenessWebSocket(
}
}

private fun getUserAgent(): String {
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
fun getUserAgent(livenessVersion: String = ""): String {
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"

if (livenessVersion != "") {
userAgent += " api/liveness/$livenessVersion"
}

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,
livenessVersion: String,
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,
livenessVersion = livenessVersion,
onSessionInformationReceived = { sessionInformation ->
val challenges = processSessionInformation(sessionInformation)
val faceLivenessSession = FaceLivenessSession(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@

package com.amplifyframework.predictions.aws.http

import android.os.Build
import aws.smithy.kotlin.runtime.auth.awscredentials.Credentials
import aws.smithy.kotlin.runtime.auth.awscredentials.CredentialsProvider
import aws.smithy.kotlin.runtime.util.Attributes
import com.amplifyframework.core.Action
import com.amplifyframework.core.BuildConfig
import com.amplifyframework.core.Consumer
import com.amplifyframework.predictions.PredictionsException
import com.amplifyframework.predictions.aws.models.liveness.ChallengeConfig
Expand Down Expand Up @@ -94,6 +96,7 @@ internal class LivenessWebSocketTest {
server.url("/").toString(),
"",
sessionInformation,
"",
onSessionInformationReceived,
onErrorReceived,
onComplete
Expand Down Expand Up @@ -275,6 +278,29 @@ internal class LivenessWebSocketTest {
verify { livenessWebSocket.webSocket!!.close(1000, any()) }
}

@Test
fun `web socket user agent base`() {
livenessWebSocket.webSocket = mockk()

val version = BuildConfig.VERSION_NAME
val os = Build.VERSION.SDK_INT
val baseline = "amplify-android:$version md/unknown/robolectric md/locale/en_UNKNOWN os/Android/$os " +
"md/device/robolectric md/device-manufacturer/unknown api/rekognitionstreaming/$version"
assertEquals(livenessWebSocket.getUserAgent(), baseline)
}

@Test
fun `web socket user agent includes added UI version`() {
livenessWebSocket.webSocket = mockk()

val version = BuildConfig.VERSION_NAME
val os = Build.VERSION.SDK_INT
val baseline = "amplify-android:$version md/unknown/robolectric md/locale/en_UNKNOWN os/Android/$os " +
"md/device/robolectric md/device-manufacturer/unknown api/rekognitionstreaming/$version"
val additional = "api/liveness/1.1.1"
assertEquals(livenessWebSocket.getUserAgent("1.1.1"), "$baseline $additional")
}

@Test
@Ignore("Need to work on parsing the onMessage byteString from ServerWebSocketListener")
fun `sendInitialFaceDetectedEvent test`() {
Expand Down Expand Up @@ -303,7 +329,7 @@ internal class LivenessWebSocketTest {

class LatchingWebSocketResponseListener(
private val webSocketListener: WebSocketListener,
private val openLatch: CountDownLatch = CountDownLatch(1),
private val openLatch: CountDownLatch = CountDownLatch(1)
) : WebSocketListener() {

override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
Expand Down
Loading