package com.amazonaws.auth; import android.content.Context; import com.amazonaws.internal.keyvaluestore.AWSKeyValueStore; import com.amazonaws.logging.Log; import com.amazonaws.logging.LogFactory; import com.amazonaws.regions.Regions; import com.amazonaws.util.VersionInfoUtils; import csdk.gluads.Consts; import java.util.Date; /* loaded from: classes.dex */ public class CognitoCachingCredentialsProvider extends CognitoCredentialsProvider { public AWSKeyValueStore awsKeyValueStore; public String identityId; public boolean isPersistenceEnabled; public final IdentityChangedListener listener; public volatile boolean needIdentityRefresh; public String userAgentOverride; public static final String USER_AGENT = CognitoCachingCredentialsProvider.class.getName() + "/" + VersionInfoUtils.getVersion(); public static final Log LOG = LogFactory.getLog(CognitoCachingCredentialsProvider.class); public static final String AWS_KEY_VALUE_STORE_NAMESPACE_IDENTIFIER = "com.amazonaws.android.auth"; public static final String ID_KEY = "identityId"; public static final String AK_KEY = "accessKey"; public static final String SK_KEY = "secretKey"; public static final String ST_KEY = "sessionToken"; public static final String EXP_KEY = "expirationDate"; @Override // com.amazonaws.auth.CognitoCredentialsProvider public String getUserAgent() { String str = this.userAgentOverride; return str != null ? str : USER_AGENT; } public CognitoCachingCredentialsProvider(Context context, String str, String str2, String str3, String str4, Regions regions) { super(str, str2, str3, str4, regions); this.needIdentityRefresh = false; this.listener = new IdentityChangedListener() { // from class: com.amazonaws.auth.CognitoCachingCredentialsProvider.1 @Override // com.amazonaws.auth.IdentityChangedListener public void identityChanged(String str5, String str6) { CognitoCachingCredentialsProvider.LOG.debug("Identity id is changed"); CognitoCachingCredentialsProvider.this.saveIdentityId(str6); CognitoCachingCredentialsProvider.this.clearCredentials(); } }; this.isPersistenceEnabled = true; if (context == null) { throw new IllegalArgumentException("context can't be null"); } initialize(context); } public final void initialize(Context context) { this.awsKeyValueStore = new AWSKeyValueStore(context, AWS_KEY_VALUE_STORE_NAMESPACE_IDENTIFIER, this.isPersistenceEnabled); checkUpgrade(); this.identityId = getCachedIdentityId(); loadCachedCredentials(); registerIdentityChangedListener(this.listener); } @Override // com.amazonaws.auth.CognitoCredentialsProvider public String getIdentityId() { if (this.needIdentityRefresh) { this.needIdentityRefresh = false; refresh(); String identityId = super.getIdentityId(); this.identityId = identityId; saveIdentityId(identityId); } String cachedIdentityId = getCachedIdentityId(); this.identityId = cachedIdentityId; if (cachedIdentityId == null) { String identityId2 = super.getIdentityId(); this.identityId = identityId2; saveIdentityId(identityId2); } return this.identityId; } @Override // com.amazonaws.auth.CognitoCredentialsProvider, com.amazonaws.auth.AWSCredentialsProvider public AWSSessionCredentials getCredentials() { AWSSessionCredentials aWSSessionCredentials; this.credentialsLock.writeLock().lock(); try { if (this.sessionCredentials == null) { loadCachedCredentials(); } if (this.sessionCredentialsExpiration == null || needsNewSession()) { LOG.debug("Making a network call to fetch credentials."); super.getCredentials(); Date date = this.sessionCredentialsExpiration; if (date != null) { saveCredentials(this.sessionCredentials, date.getTime()); } aWSSessionCredentials = this.sessionCredentials; } else { aWSSessionCredentials = this.sessionCredentials; } this.credentialsLock.writeLock().unlock(); return aWSSessionCredentials; } catch (Throwable th) { this.credentialsLock.writeLock().unlock(); throw th; } } @Override // com.amazonaws.auth.CognitoCredentialsProvider public void refresh() { this.credentialsLock.writeLock().lock(); try { super.refresh(); Date date = this.sessionCredentialsExpiration; if (date != null) { saveCredentials(this.sessionCredentials, date.getTime()); } } finally { this.credentialsLock.writeLock().unlock(); } } @Override // com.amazonaws.auth.CognitoCredentialsProvider public void clearCredentials() { this.credentialsLock.writeLock().lock(); try { super.clearCredentials(); LOG.debug("Clearing credentials from SharedPreferences"); this.awsKeyValueStore.remove(namespace(AK_KEY)); this.awsKeyValueStore.remove(namespace(SK_KEY)); this.awsKeyValueStore.remove(namespace(ST_KEY)); this.awsKeyValueStore.remove(namespace(EXP_KEY)); } finally { this.credentialsLock.writeLock().unlock(); } } public String getCachedIdentityId() { String str = this.awsKeyValueStore.get(namespace(ID_KEY)); if (str != null && this.identityId == null) { super.setIdentityId(str); } return str; } public final void loadCachedCredentials() { Log log = LOG; log.debug("Loading credentials from SharedPreferences"); String str = this.awsKeyValueStore.get(namespace(EXP_KEY)); if (str == null) { this.sessionCredentialsExpiration = null; return; } try { this.sessionCredentialsExpiration = new Date(Long.parseLong(str)); if (!hasCredentials()) { this.sessionCredentialsExpiration = null; return; } String str2 = this.awsKeyValueStore.get(namespace(AK_KEY)); String str3 = this.awsKeyValueStore.get(namespace(SK_KEY)); String str4 = this.awsKeyValueStore.get(namespace(ST_KEY)); if (str2 == null || str3 == null || str4 == null) { log.debug("No valid credentials found in SharedPreferences"); this.sessionCredentialsExpiration = null; } else { this.sessionCredentials = new BasicSessionCredentials(str2, str3, str4); } } catch (NumberFormatException unused) { this.sessionCredentialsExpiration = null; } } public final boolean hasCredentials() { boolean contains = this.awsKeyValueStore.contains(namespace(AK_KEY)); boolean contains2 = this.awsKeyValueStore.contains(namespace(SK_KEY)); boolean contains3 = this.awsKeyValueStore.contains(namespace(ST_KEY)); if (!contains && !contains2 && !contains3) { return false; } LOG.debug("No valid credentials found in SharedPreferences"); return true; } public final void saveCredentials(AWSSessionCredentials aWSSessionCredentials, long j) { LOG.debug("Saving credentials to SharedPreferences"); if (aWSSessionCredentials != null) { this.awsKeyValueStore.put(namespace(AK_KEY), aWSSessionCredentials.getAWSAccessKeyId()); this.awsKeyValueStore.put(namespace(SK_KEY), aWSSessionCredentials.getAWSSecretKey()); this.awsKeyValueStore.put(namespace(ST_KEY), aWSSessionCredentials.getSessionToken()); this.awsKeyValueStore.put(namespace(EXP_KEY), String.valueOf(j)); } } public final void saveIdentityId(String str) { LOG.debug("Saving identity id to SharedPreferences"); this.identityId = str; this.awsKeyValueStore.put(namespace(ID_KEY), str); } public final void checkUpgrade() { AWSKeyValueStore aWSKeyValueStore = this.awsKeyValueStore; String str = ID_KEY; if (aWSKeyValueStore.contains(str)) { LOG.info("Identity id without namespace is detected. It will be saved under new namespace."); String str2 = this.awsKeyValueStore.get(str); this.awsKeyValueStore.clear(); this.awsKeyValueStore.put(namespace(str), str2); } } public final String namespace(String str) { return getIdentityPoolId() + Consts.STRING_PERIOD + str; } }