- 28,932 files - Full Java source code - Smali files - Resources Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
230 lines
9.8 KiB
Java
230 lines
9.8 KiB
Java
package com.amazonaws.auth;
|
|
|
|
import com.amazonaws.AmazonServiceException;
|
|
import com.amazonaws.AmazonWebServiceRequest;
|
|
import com.amazonaws.ClientConfiguration;
|
|
import com.amazonaws.SDKGlobalConfiguration;
|
|
import com.amazonaws.logging.Log;
|
|
import com.amazonaws.logging.LogFactory;
|
|
import com.amazonaws.regions.Region;
|
|
import com.amazonaws.regions.Regions;
|
|
import com.amazonaws.services.cognitoidentity.AmazonCognitoIdentity;
|
|
import com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient;
|
|
import com.amazonaws.services.cognitoidentity.model.Credentials;
|
|
import com.amazonaws.services.cognitoidentity.model.GetCredentialsForIdentityRequest;
|
|
import com.amazonaws.services.cognitoidentity.model.GetCredentialsForIdentityResult;
|
|
import com.amazonaws.services.securitytoken.AWSSecurityTokenService;
|
|
import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClient;
|
|
import com.amazonaws.services.securitytoken.model.AssumeRoleWithWebIdentityRequest;
|
|
import com.firemint.realracing.Platform;
|
|
import java.util.Date;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|
|
|
/* loaded from: classes.dex */
|
|
public class CognitoCredentialsProvider implements AWSCredentialsProvider {
|
|
public static final Log log = LogFactory.getLog(AWSCredentialsProviderChain.class);
|
|
public String authRoleArn;
|
|
public AmazonCognitoIdentity cib;
|
|
public final ReentrantReadWriteLock credentialsLock;
|
|
public String customRoleArn;
|
|
public final AWSCognitoIdentityProvider identityProvider;
|
|
public int refreshThreshold;
|
|
public final String region;
|
|
public AWSSecurityTokenService securityTokenService;
|
|
public AWSSessionCredentials sessionCredentials;
|
|
public Date sessionCredentialsExpiration;
|
|
public int sessionDuration;
|
|
public String token;
|
|
public String unauthRoleArn;
|
|
public final boolean useEnhancedFlow;
|
|
|
|
public String getUserAgent() {
|
|
return "";
|
|
}
|
|
|
|
public CognitoCredentialsProvider(String str, String str2, String str3, String str4, Regions regions) {
|
|
this(str, str2, str3, str4, regions, new ClientConfiguration());
|
|
}
|
|
|
|
public CognitoCredentialsProvider(String str, String str2, String str3, String str4, Regions regions, ClientConfiguration clientConfiguration) {
|
|
this(str, str2, str3, str4, createIdentityClient(clientConfiguration, regions), (str3 == null && str4 == null) ? null : new AWSSecurityTokenServiceClient(new AnonymousAWSCredentials(), clientConfiguration));
|
|
}
|
|
|
|
public static AmazonCognitoIdentityClient createIdentityClient(ClientConfiguration clientConfiguration, Regions regions) {
|
|
AmazonCognitoIdentityClient amazonCognitoIdentityClient = new AmazonCognitoIdentityClient(new AnonymousAWSCredentials(), clientConfiguration);
|
|
amazonCognitoIdentityClient.setRegion(Region.getRegion(regions));
|
|
return amazonCognitoIdentityClient;
|
|
}
|
|
|
|
public CognitoCredentialsProvider(String str, String str2, String str3, String str4, AmazonCognitoIdentityClient amazonCognitoIdentityClient, AWSSecurityTokenService aWSSecurityTokenService) {
|
|
this.cib = amazonCognitoIdentityClient;
|
|
this.region = amazonCognitoIdentityClient.getRegions().getName();
|
|
this.securityTokenService = aWSSecurityTokenService;
|
|
this.unauthRoleArn = str3;
|
|
this.authRoleArn = str4;
|
|
this.sessionDuration = Platform.INTERNET_CONNECTION_DELAY;
|
|
this.refreshThreshold = 500;
|
|
boolean z = str3 == null && str4 == null;
|
|
this.useEnhancedFlow = z;
|
|
if (z) {
|
|
this.identityProvider = new AWSEnhancedCognitoIdentityProvider(str, str2, amazonCognitoIdentityClient);
|
|
} else {
|
|
this.identityProvider = new AWSBasicCognitoIdentityProvider(str, str2, amazonCognitoIdentityClient);
|
|
}
|
|
this.credentialsLock = new ReentrantReadWriteLock(true);
|
|
}
|
|
|
|
public String getIdentityId() {
|
|
return this.identityProvider.getIdentityId();
|
|
}
|
|
|
|
public void setSessionCredentialsExpiration(Date date) {
|
|
this.credentialsLock.writeLock().lock();
|
|
try {
|
|
this.sessionCredentialsExpiration = date;
|
|
} finally {
|
|
this.credentialsLock.writeLock().unlock();
|
|
}
|
|
}
|
|
|
|
public String getIdentityPoolId() {
|
|
return this.identityProvider.getIdentityPoolId();
|
|
}
|
|
|
|
@Override // com.amazonaws.auth.AWSCredentialsProvider
|
|
public AWSSessionCredentials getCredentials() {
|
|
this.credentialsLock.writeLock().lock();
|
|
try {
|
|
if (needsNewSession()) {
|
|
startSession();
|
|
}
|
|
AWSSessionCredentials aWSSessionCredentials = this.sessionCredentials;
|
|
this.credentialsLock.writeLock().unlock();
|
|
return aWSSessionCredentials;
|
|
} catch (Throwable th) {
|
|
this.credentialsLock.writeLock().unlock();
|
|
throw th;
|
|
}
|
|
}
|
|
|
|
public void setIdentityId(String str) {
|
|
this.identityProvider.identityChanged(str);
|
|
}
|
|
|
|
public Map getLogins() {
|
|
return this.identityProvider.getLogins();
|
|
}
|
|
|
|
public void refresh() {
|
|
this.credentialsLock.writeLock().lock();
|
|
try {
|
|
startSession();
|
|
} finally {
|
|
this.credentialsLock.writeLock().unlock();
|
|
}
|
|
}
|
|
|
|
public void clearCredentials() {
|
|
this.credentialsLock.writeLock().lock();
|
|
try {
|
|
this.sessionCredentials = null;
|
|
this.sessionCredentialsExpiration = null;
|
|
} finally {
|
|
this.credentialsLock.writeLock().unlock();
|
|
}
|
|
}
|
|
|
|
public void startSession() {
|
|
try {
|
|
this.token = this.identityProvider.refresh();
|
|
} catch (AmazonServiceException e) {
|
|
if (e.getErrorCode().equals("ValidationException")) {
|
|
this.token = retryRefresh();
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
if (this.useEnhancedFlow) {
|
|
populateCredentialsWithCognito(this.token);
|
|
} else {
|
|
populateCredentialsWithSts(this.token);
|
|
}
|
|
}
|
|
|
|
public final String retryRefresh() {
|
|
setIdentityId(null);
|
|
String refresh = this.identityProvider.refresh();
|
|
this.token = refresh;
|
|
return refresh;
|
|
}
|
|
|
|
public String getLoginsKey() {
|
|
return Regions.CN_NORTH_1.getName().equals(this.region) ? "cognito-identity.cn-north-1.amazonaws.com.cn" : "cognito-identity.amazonaws.com";
|
|
}
|
|
|
|
public final GetCredentialsForIdentityResult retryGetCredentialsForIdentity() {
|
|
Map logins;
|
|
String retryRefresh = retryRefresh();
|
|
this.token = retryRefresh;
|
|
if (retryRefresh != null && !retryRefresh.isEmpty()) {
|
|
logins = new HashMap();
|
|
logins.put(getLoginsKey(), this.token);
|
|
} else {
|
|
logins = getLogins();
|
|
}
|
|
return this.cib.getCredentialsForIdentity(new GetCredentialsForIdentityRequest().withIdentityId(getIdentityId()).withLogins(logins).withCustomRoleArn(this.customRoleArn));
|
|
}
|
|
|
|
public final void populateCredentialsWithCognito(String str) {
|
|
Map logins;
|
|
GetCredentialsForIdentityResult retryGetCredentialsForIdentity;
|
|
if (str != null && !str.isEmpty()) {
|
|
logins = new HashMap();
|
|
logins.put(getLoginsKey(), str);
|
|
} else {
|
|
logins = getLogins();
|
|
}
|
|
try {
|
|
retryGetCredentialsForIdentity = this.cib.getCredentialsForIdentity(new GetCredentialsForIdentityRequest().withIdentityId(getIdentityId()).withLogins(logins).withCustomRoleArn(this.customRoleArn));
|
|
} catch (AmazonServiceException e) {
|
|
if (e.getErrorCode().equals("ValidationException")) {
|
|
retryGetCredentialsForIdentity = retryGetCredentialsForIdentity();
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
Credentials credentials = retryGetCredentialsForIdentity.getCredentials();
|
|
this.sessionCredentials = new BasicSessionCredentials(credentials.getAccessKeyId(), credentials.getSecretKey(), credentials.getSessionToken());
|
|
setSessionCredentialsExpiration(credentials.getExpiration());
|
|
if (retryGetCredentialsForIdentity.getIdentityId().equals(getIdentityId())) {
|
|
return;
|
|
}
|
|
setIdentityId(retryGetCredentialsForIdentity.getIdentityId());
|
|
}
|
|
|
|
public final void populateCredentialsWithSts(String str) {
|
|
AssumeRoleWithWebIdentityRequest withDurationSeconds = new AssumeRoleWithWebIdentityRequest().withWebIdentityToken(str).withRoleArn(this.identityProvider.isAuthenticated() ? this.authRoleArn : this.unauthRoleArn).withRoleSessionName("ProviderSession").withDurationSeconds(Integer.valueOf(this.sessionDuration));
|
|
appendUserAgent(withDurationSeconds, getUserAgent());
|
|
com.amazonaws.services.securitytoken.model.Credentials credentials = this.securityTokenService.assumeRoleWithWebIdentity(withDurationSeconds).getCredentials();
|
|
this.sessionCredentials = new BasicSessionCredentials(credentials.getAccessKeyId(), credentials.getSecretAccessKey(), credentials.getSessionToken());
|
|
setSessionCredentialsExpiration(credentials.getExpiration());
|
|
}
|
|
|
|
public boolean needsNewSession() {
|
|
if (this.sessionCredentials == null) {
|
|
return true;
|
|
}
|
|
return this.sessionCredentialsExpiration.getTime() - (System.currentTimeMillis() - (SDKGlobalConfiguration.getGlobalTimeOffset() * 1000)) < ((long) (this.refreshThreshold * 1000));
|
|
}
|
|
|
|
public final void appendUserAgent(AmazonWebServiceRequest amazonWebServiceRequest, String str) {
|
|
amazonWebServiceRequest.getRequestClientOptions().appendUserAgent(str);
|
|
}
|
|
|
|
public void registerIdentityChangedListener(IdentityChangedListener identityChangedListener) {
|
|
this.identityProvider.registerIdentityChangedListener(identityChangedListener);
|
|
}
|
|
}
|