Быстрый обмен токеном AWS на учетные данные
Я пытаюсь получить доступ к службам AWS с помощью пула удостоверений после входа в систему этого документа AWS https://docs.aws.amazon.com/en_us/cognito/latest/developerguide/amazon-cognito-integrating-user-pools-with-identity-pools.html
но при интеграции пула пользователей с частью пула удостоверений я не могу добавить токен в код swift
let serviceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: nil)
let userPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: "YOUR_CLIENT_ID", clientSecret: "YOUR_CLIENT_SECRET", poolId: "YOUR_USER_POOL_ID")
AWSCognitoIdentityUserPool.registerCognitoIdentityUserPoolWithConfiguration(serviceConfiguration, userPoolConfiguration: userPoolConfiguration, forKey: "UserPool")
let pool = AWSCognitoIdentityUserPool(forKey: "UserPool")
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "YOUR_IDENTITY_POOL_ID", identityProviderManager:pool)
В JavaScript и Android оба имеют учетные данные Provider.logins .
я получил сообщение как это:
Message: The security token included in the request is invalid.
или мне нужно позвонить в AWS STS API по этому документу? https://docs.aws.amazon.com/en_us/IAM/latest/UserGuide/id_credentials_temp_request.html
Спасибо за помощь.
0 ответов
Сначала я попробую это
let identity = AWSCognitoIdentity.default()
let input = AWSCognitoIdentityGetCredentialsForIdentityInput()
input?.identityId = identityid
input?.logins = ["cognito-idp.us-east-1.amazonaws.com/us-east-1_XXXXX":identityToken]
identity.getCredentialsForIdentity(input!).continueWith(block: { (task:AWSTask<AWSCognitoIdentityGetCredentialsForIdentityResponse>) -> Any? in
if (task.error != nil) {
print("Error: " + task.error!.localizedDescription)
}
else {
self.accessKey = (task.result?.credentials?.accessKeyId)!
self.secrectKey = (task.result?.credentials?.secretKey)!
self.sessionToken = (task.result?.credentials?.sessionToken)
}
return task;
})
но это не работа, я не уверен, что произойдет. я использую https, чтобы получить aws iot shadow, он вернет нулевое значение и получит сообщение 403. Затем я изменяю официальный код документа, как это
let serviceConfiguration = AWSServiceConfiguration(region: .USEast1,credentialsProvider: nil)
let userPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: "YOUR_CLIENT_ID", clientSecret: "YOUR_CLIENT_SECRET", poolId: "YOUR_USER_POOL_ID")
AWSCognitoIdentityUserPool.registerCognitoIdentityUserPoolWithConfiguration(serviceConfiguration, userPoolConfiguration: userPoolConfiguration, forKey: "UserPool")
let pool = AWSCognitoIdentityUserPool(forKey: "UserPool")
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "YOUR_IDENTITY_POOL_ID", identityProviderManager:pool)
затем используйте этот код
credentialsProvider.credentials().continueWith(block: { (task) -> AnyObject? in
if (task.error != nil) {
print("Error: " + task.error!.localizedDescription)
}
else {
let credentials = task.result!
self.accessKey = credentials.accessKey
self.secrectKey = credentials.secretKey
self.session = credentials.sessionKey! }
return task;
})
но этот код не использует idtoken, который я получил при входе в систему cognito, возможно, это просто позволяет мне получить доступ к сервису, а затем я могу проанализировать idtoken, чтобы получить подробное сообщение idtoken.
надеюсь, это кому-нибудь поможет.
в дополнение к этому, импорт заключается в том, чтобы прикрепить временные учетные данные iPrincipalPolicy к iot; при входе в систему cognito вы получите идентификационные данные удостоверения личности, а затем использовали этот идентификатор удостоверения для подключения.
или вы получите 403 запрещенных и нулевых возвращаемых значений.