Skip to content

Commit b2e1d44

Browse files
committed
add startFaceLivenessSession spi (#136)
1 parent 0fe0c44 commit b2e1d44

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import Foundation
9+
import Amplify
10+
import AWSPluginsCore
11+
12+
extension AWSPredictionsPlugin {
13+
@_spi(PredictionsFaceLiveness)
14+
public static func startFaceLivenessSession(
15+
withID sessionID: String,
16+
credentialsProvider: AWSCredentialsProvider? = nil,
17+
region: String,
18+
options: FaceLivenessSession.Options,
19+
completion: @escaping (Result<Void, FaceLivenessSessionError>) -> Void
20+
) async throws -> FaceLivenessSession {
21+
22+
let credential = try await credential(from: credentialsProvider)
23+
24+
let signer = SigV4Signer(
25+
credential: .init(
26+
accessKey: credential.accessKey,
27+
secretKey: credential.secretKey,
28+
sessionToken: credential.sessionToken
29+
),
30+
serviceName: "rekognition",
31+
region: region
32+
)
33+
34+
let url = try streamingSessionURL(for: region)
35+
36+
let session = FaceLivenessSession(
37+
websocket: WebSocketSession(),
38+
signer: signer,
39+
baseURL: url
40+
)
41+
42+
return session
43+
}
44+
}
45+
46+
extension FaceLivenessSession {
47+
@_spi(PredictionsFaceLiveness)
48+
public struct Options {
49+
public init() {}
50+
}
51+
}
52+
53+
@_spi(PredictionsFaceLiveness)
54+
public struct FaceLivenessSessionError: Swift.Error, Equatable {
55+
public let code: UInt8
56+
57+
public static let accessDenied = Self(code: 0)
58+
public static let invalidRegion = Self(code: 1)
59+
public static let invalidURL = Self(code: 2)
60+
}
61+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import Foundation
9+
import Amplify
10+
import AWSPluginsCore
11+
12+
func credential(from credentialsProvider: AWSCredentialsProvider?) async throws -> SigV4Signer.Credential {
13+
let credential: SigV4Signer.Credential
14+
15+
if let credentialsProvider = credentialsProvider {
16+
let providedCredentials = try await credentialsProvider.fetchAWSCredentials()
17+
credential = .init(
18+
accessKey: providedCredentials.accessKeyId,
19+
secretKey: providedCredentials.secretAccessKey,
20+
sessionToken: (providedCredentials as? AWSTemporaryCredentials)?.sessionToken
21+
)
22+
} else {
23+
let authSession = try await Amplify.Auth.fetchAuthSession()
24+
if let authAWSCredentialsProvider = authSession as? AuthAWSCredentialsProvider {
25+
let awsCredentials = try authAWSCredentialsProvider.getAWSCredentials().get()
26+
credential = .init(
27+
accessKey: awsCredentials.accessKeyId,
28+
secretKey: awsCredentials.secretAccessKey,
29+
sessionToken: (awsCredentials as? AWSTemporaryCredentials)?.sessionToken
30+
)
31+
} else {
32+
throw FaceLivenessSessionError.accessDenied
33+
}
34+
}
35+
36+
return credential
37+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import Foundation
9+
10+
func streamingSessionURL(for region: String) throws -> URL {
11+
let urlString = "wss://streaming-rekognition.\(region).amazonaws.com/start-face-liveness-session-websocket"
12+
guard let url = URL(string: urlString) else {
13+
throw FaceLivenessSessionError.invalidRegion
14+
}
15+
return url
16+
}

0 commit comments

Comments
 (0)