I am currently working on AWS STS to access the S3 resources.I am able to access S3 resources using get object with the help of STS credentials for the first time.After 15 minutes, i have obtained new credentials using getAssumeRoleForUser.This time i am unable to access S3 resources due to token expired exception. Following is the code used to follow the above said procedure.
class CustomIdentityProvider: NSObject, AWSCredentialsProvider{
var token:String!
var awsAccessKey: String!
var awsSecretKey: String!
var awsExpiration: NSDate!
var awsRegion:String!
func credentials() -> AWSTask<AWSCredentials>{
// AWSPinpoint.
let credentials = AWSCredentials(accessKey:self.awsAccessKey, secretKey:self.awsSecretKey, sessionKey :self.token, expiration:self.awsExpiration as! Date)
return AWSTask(result:credentials)
}
func invalidateCachedTemporaryCredentials() {
print("")
}
init(accessKey: String, secretKey: String, sessionKey: String?, expiration: NSDate?) {
self.token = sessionKey
self.awsAccessKey = accessKey
self.awsSecretKey = secretKey
self.awsExpiration = expiration
}
}
//MARK - Using above provider to reset credentilas linke this- AppSessionManager.shared.checkAndApplyRefreshMechanismOn1HrsTimeAndSchedulecompletion(completion: ){ (Roledata) in
print(Roledata)
let expirationTime = Roledata.expiration
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from: expirationTime )
let awsExpiration = date as NSDate?
let ProviderManager = CustomIdentityProvider(accessKey:Roledata.accessKeyId , secretKey: Roledata.secretAccessKey , sessionKey: Roledata.sessionToken , expiration: awsExpiration)
let config = AWSServiceConfiguration(region: AWSRegionType.APSouth1, credentialsProvider: ProviderManager)
AWSServiceManager.default().defaultServiceConfiguration = config
}
AWS STS token update issue while accessing the S3 resources
What is required to handle this token expired issue? Is there any more setting or code missing from my side?