-
Notifications
You must be signed in to change notification settings - Fork 82
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
Create cognito credentials provider #324
Comments
When developing iOS or macOS apps without using Amplify, there are use cases where the app needs anonymous AWS credentials to call AWS API. Examples might be to send events to PinPoint, to send logs to cloudwatch, to fetch configuration from AppConfig, to interact with DynamoDB or Lambda. In scenario where the app user is not authenticated, the simplest secure way to obtain AWS credentials is to use Getting this right involves ~30-40 lines of undifferentiated heavy lifting code. It would be great to have these factored out as an Why do we need SDK Support ? Because of the sandbox, macOS and iOS ap do not have access to ~/.aws directory where the SDK tries to fetch default credentials. What Other SDK are proposing ? I did not check all SDK, but the ones I am using regularly have manually crafted, higher-level credentials providers :
Prof of Concept Here is a DynamoDB Example: One time setup on the AWS side
Swift function to create an Identity ID if it does not exist and then obtain credentials for that identity id This function is equivalent to this command line
func getTempCredentials() async throws -> AWSClientRuntime.AWSCredentialsProvider {
struct InvalidCredentialsError : Error {}
print("get AWS credentials")
do {
let cognitoClient = try CognitoIdentityClient(region: "eu-central-1")
// get a cognito identity id, only one per user and we cache it in user preferences
var identityId = UserDefaults.standard.string(forKey: "identity-id")
if identityId == nil {
let cognitoGetIdRequest = GetIdInput(identityPoolId: "eu-central-1:dc01c701-95c1-4c5a-915f-52bc5a3cdef5")
let cognitoGetIdResponse = try await cognitoClient.getId(input: cognitoGetIdRequest)
identityId = cognitoGetIdResponse.identityId
UserDefaults.standard.setValue(identityId, forKey: "identity-id")
}
// get aws credentials for that identity
let cognitoRequest = GetCredentialsForIdentityInput(identityId: identityId)
let cognitoResponse = try await cognitoClient.getCredentialsForIdentity(input: cognitoRequest)
guard let credentials = cognitoResponse.credentials,
let accessKeyId = credentials.accessKeyId,
let secretKey = credentials.secretKey,
let sessionToken = credentials.sessionToken else {
print("no credentials returned")
throw InvalidCredentialsError()
}
let tempCredentials = AWSCredentialsProviderStaticConfig(accessKey: accessKeyId,
secret: secretKey,
sessionToken: sessionToken)
return try AWSClientRuntime.AWSCredentialsProvider.fromStatic(tempCredentials)
} catch {
throw error
}
} The code can use this credential provider to configure clients: let credentialsProvider = try await getTempCredentials()
print("add item")
let dynamoDBConfig
= try DynamoDBClient.DynamoDBClientConfiguration(credentialsProvider: credentialsProvider,
region: "eu-central-1")
let dynamoDBClient = DynamoDBClient(config: dynamoDBConfig) |
This work will be tracked in Issue #1082 |
No description provided.
The text was updated successfully, but these errors were encountered: