Skip to content

Commit

Permalink
Merge pull request #20 from makeen-project/pr_comment
Browse files Browse the repository at this point in the history
PR comment addressed
  • Loading branch information
olegfilimonov authored Jul 5, 2024
2 parents 8036766 + 1bbca8a commit 7bdacce
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ OR
// Create a credentail provider using custom credential provider with AuthHelper
private fun exampleCustomCredentialLogin() {
var authHelper = AuthHelper(applicationContext)
var locationCredentialsProvider : LocationCredentialsProvider = authHelper.authenticateWithCredentialsProvider("MY-AWS-REGION", "MY-CUSTOM-CREDENTIAL-PROVIDER")
var locationCredentialsProvider : LocationCredentialsProvider = authHelper.authenticateWithCredentialsProvider("MY-AWS-REGION", MY-CUSTOM-CREDENTIAL-PROVIDER)
var locationClient = locationCredentialsProvider?.getLocationClient()
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class LocationCredentialsProvider {
private var customCredentials: aws.sdk.kotlin.services.cognitoidentity.model.Credentials? = null
private var context: Context
private var cognitoCredentialsProvider: CognitoCredentialsProvider? = null
private var securePreferences: EncryptedSharedPreferences? = null
private var securePreferences: EncryptedSharedPreferences
private var locationClient: LocationClient? = null
private var cognitoIdentityClient: CognitoIdentityClient? = null

Expand All @@ -37,10 +37,10 @@ class LocationCredentialsProvider {
*/
constructor(context: Context, identityPoolId: String, region: AwsRegions) {
this.context = context
initPreference(context)
securePreferences?.put(METHOD, "cognito")
securePreferences?.put(IDENTITY_POOL_ID, identityPoolId)
securePreferences?.put(REGION, region.regionName)
securePreferences = initPreference(context)
securePreferences.put(METHOD, "cognito")
securePreferences.put(IDENTITY_POOL_ID, identityPoolId)
securePreferences.put(REGION, region.regionName)
}

/**
Expand All @@ -50,9 +50,9 @@ class LocationCredentialsProvider {
*/
constructor(context: Context, region: AwsRegions) {
this.context = context
initPreference(context)
securePreferences?.put(METHOD, "custom")
securePreferences?.put(REGION, region.regionName)
securePreferences = initPreference(context)
securePreferences.put(METHOD, "custom")
securePreferences.put(REGION, region.regionName)
}


Expand All @@ -63,13 +63,13 @@ class LocationCredentialsProvider {
*/
constructor(context: Context) {
this.context = context
initPreference(context)
val method = securePreferences?.get(METHOD)
securePreferences = initPreference(context)
val method = securePreferences.get(METHOD)
if (method === null) throw Exception("No credentials found")
when (method) {
"cognito" -> {
val identityPoolId = securePreferences?.get(IDENTITY_POOL_ID)
val region = securePreferences?.get(REGION)
val identityPoolId = securePreferences.get(IDENTITY_POOL_ID)
val region = securePreferences.get(REGION)
if (identityPoolId === null || region === null) throw Exception("No credentials found")
cognitoCredentialsProvider = CognitoCredentialsProvider(context)
}
Expand All @@ -79,9 +79,8 @@ class LocationCredentialsProvider {
}
}

fun initPreference(context: Context) {
securePreferences = EncryptedSharedPreferences(context, PREFS_NAME)
securePreferences?.initEncryptedSharedPreferences()
fun initPreference(context: Context): EncryptedSharedPreferences {
return EncryptedSharedPreferences(context, PREFS_NAME).apply { initEncryptedSharedPreferences() }
}

/**
Expand All @@ -94,8 +93,8 @@ class LocationCredentialsProvider {
* @throws Exception if the identity pool ID or region is not found, or if credential generation fails.
*/
suspend fun verifyAndRefreshCredentials() {
val identityPoolId = securePreferences?.get(IDENTITY_POOL_ID)
val region = securePreferences?.get(REGION)
val identityPoolId = securePreferences.get(IDENTITY_POOL_ID)
val region = securePreferences.get(REGION)
if (identityPoolId === null || region === null) throw Exception("No credentials found")
val isCredentialsAvailable = try {
cognitoCredentialsProvider = CognitoCredentialsProvider(context)
Expand All @@ -121,7 +120,7 @@ class LocationCredentialsProvider {
* @param credentialsProvider The provider for AWS credentials.
*/
suspend fun initializeLocationClient(credentialsProvider: CredentialsProvider) {
val region = securePreferences?.get(REGION)
val region = securePreferences.get(REGION)
if (region === null) throw Exception("No credentials found")

this.credentialsProvider = credentialsProvider
Expand Down Expand Up @@ -156,7 +155,7 @@ class LocationCredentialsProvider {
* @throws Exception if the AWS region is not found in secure preferences.
*/
fun getLocationClient(): LocationClient? {
val region = securePreferences?.get(REGION)
val region = securePreferences.get(REGION)
if (region === null) throw Exception("No credentials found")
if (locationClient == null) {
val credentialsProvider = createCredentialsProvider()
Expand Down Expand Up @@ -278,7 +277,7 @@ class LocationCredentialsProvider {
* @throws Exception If the Cognito provider is not initialized.
*/
fun getCredentialsProvider(): aws.sdk.kotlin.services.cognitoidentity.model.Credentials? {
val method = securePreferences?.get(METHOD)
val method = securePreferences.get(METHOD)
if (method == "custom" && customCredentials != null) {
return customCredentials
}
Expand All @@ -291,10 +290,10 @@ class LocationCredentialsProvider {
* @throws Exception If the Cognito provider is not initialized.
*/
suspend fun refresh() {
val region = securePreferences?.get(REGION)
val region = securePreferences.get(REGION)
if (region === null) throw Exception("No credentials found")

val method = securePreferences?.get(METHOD)
val method = securePreferences.get(METHOD)
if (method == "custom" && customCredentials != null) {
credentialsProvider?.let { setCustomCredentials(it, region) }
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ class AuthHelperTest {
private lateinit var context: Context
private lateinit var authHelper: AuthHelper
private lateinit var credentialsProvider: CredentialsProvider
private lateinit var encryptedSharedPreferences: EncryptedSharedPreferences
private lateinit var cognitoIdentityClient: CognitoIdentityClient
@Before
fun setUp() {
context = mockk(relaxed = true)
authHelper = AuthHelper(context)
credentialsProvider = mockk(relaxed = true)
encryptedSharedPreferences = mockk(relaxed = true)
cognitoIdentityClient = mockk(relaxed = true)
mockkConstructor(EncryptedSharedPreferences::class)
mockkConstructor(LocationCredentialsProvider::class)
Expand All @@ -46,7 +48,7 @@ class AuthHelperTest {
every { anyConstructed<LocationCredentialsProvider>().generateCognitoIdentityClient("us-east-1") } returns cognitoIdentityClient
coEvery { anyConstructed<LocationCredentialsProvider>().initializeLocationClient(any()) } just runs
coEvery { anyConstructed<LocationCredentialsProvider>().isCredentialsValid() } returns true
every { anyConstructed<LocationCredentialsProvider>().initPreference(context) } just runs
every { anyConstructed<LocationCredentialsProvider>().initPreference(context) } returns encryptedSharedPreferences
every { anyConstructed<EncryptedSharedPreferences>().initEncryptedSharedPreferences() } just runs
}

Expand Down

0 comments on commit 7bdacce

Please sign in to comment.