Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added authentication with custom credential provider and removed authentication with API key #9

Merged
merged 20 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/test-android.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Run Unit Tests for Android
on:
workflow_dispatch:
pull_request:
branches: [ main ]
jobs:
test-android:
name: Test Android
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Setup Java
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '17'

- name: Run Unit Tests
run: |
./gradlew testDebugUnitTest

- name: Upload test results
uses: actions/upload-artifact@v2
if: always()
with:
name: test-results
path: library/build/reports/tests/testDebugUnitTest/
retention-days: 1
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Amazon Location Service Mobile Authentication SDK for Android

These utilities help you authenticate when making [Amazon Location Service](https://aws.amazon.com/location/) API calls from your Android applications. This specifically helps when using [Amazon Cognito](https://docs.aws.amazon.com/location/latest/developerguide/authenticating-using-cognito.html) or [API keys](https://docs.aws.amazon.com/location/latest/developerguide/using-apikeys.html) as the authentication method.
These utilities help you authenticate when making [Amazon Location Service](https://aws.amazon.com/location/) API calls from your Android applications. This specifically helps when using [Amazon Cognito](https://docs.aws.amazon.com/location/latest/developerguide/authenticating-using-cognito.html) as the authentication method.

## Installation

This authentication SDK works with the overall AWS SDK. Both SDKs are published to Maven Central.
This authentication SDK works with the overall AWS Kotlin SDK. Both SDKs are published to Maven Central.
Check the [latest version](https://mvnrepository.com/artifact/software.amazon.location/auth) of auth
SDK on Maven Central.

Add the following lines to the dependencies section of your build.gradle file in Android Studio:

```
implementation("software.amazon.location:auth:0.0.2")
implementation("software.amazon.location:auth:0.2.4")
implementation("aws.sdk.kotlin:location:1.2.21")
implementation("org.maplibre.gl:android-sdk:11.0.0-pre5")
implementation("com.squareup.okhttp3:okhttp:4.12.0")
Expand All @@ -34,19 +34,19 @@ import okhttp3.OkHttpClient
You can create an AuthHelper and use it with the AWS Kotlin SDK:

```
// Create an authentication helper instance using an Amazon Location API Key
private fun exampleAPIKeyLogin() {
// Create a credentail provider using Identity Pool Id with AuthHelper
private fun exampleCognitoLogin() {
var authHelper = AuthHelper(applicationContext)
var locationCredentialsProvider : LocationCredentialsProvider = authHelper.authenticateWithApiKey("My-Amazon-Location-API-Key")
var locationCredentialsProvider : LocationCredentialsProvider = authHelper.authenticateWithCognitoIdentityPool("My-Cognito-Identity-Pool-Id")
var locationClient = locationCredentialsProvider?.getLocationClient()
}
```

```
// Create an authentication helper using credentials from Cognito
private fun exampleCognitoLogin() {
OR

// Create a credentail provider using custom credential provider with AuthHelper
private fun exampleCustomCredentialLogin() {
var authHelper = AuthHelper(applicationContext)
var locationCredentialsProvider : LocationCredentialsProvider = authHelper.authenticateWithCognitoIdentityPool("My-Cognito-Identity-Pool-Id")
var locationCredentialsProvider : LocationCredentialsProvider = authHelper.authenticateWithCredentialsProvider("MY-AWS-REGION", "MY-CUSTOM-CREDENTIAL-PROVIDER")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second argument in the authenticateWithCredentialsProvider is not as string, it is a CredentialsProvider

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved

var locationClient = locationCredentialsProvider?.getLocationClient()
}
```
Expand All @@ -58,7 +58,7 @@ HttpRequestUtil.setOkHttpClient(
.addInterceptor(
AwsSignerInterceptor(
"geo",
"My-aws-region",
"MY-AWS-REGION",
locationCredentialsProvider
)
)
Expand Down

This file was deleted.

53 changes: 44 additions & 9 deletions library/src/main/java/software/amazon/location/auth/AuthHelper.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package software.amazon.location.auth

import android.content.Context
import aws.smithy.kotlin.runtime.auth.awscredentials.CredentialsProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import software.amazon.location.auth.utils.AwsRegions
Expand Down Expand Up @@ -73,14 +74,48 @@ class AuthHelper(private val context: Context) {
}

/**
* Authenticates using an API key.
* @param apiKey The API key for authentication.
* @return A LocationCredentialsProvider instance.
* Authenticates using a region and a specified CredentialsProvider.
*
* For example, to get credentials from AWS Kotlin SDK:
* 1. Use `CognitoIdentityClient` to call `.getId` to get the identity ID:
* ``` kotlin
* val identityId = cognitoIdentityClient.getId(GetIdRequest { this.identityPoolId = identityPoolId }).identityId
* ```
*
* 2. Use `CognitoIdentityClient` to call `.getCredentialsForIdentity` with the identity ID to get the credentials:
* ``` kotlin
* val credentials = cognitoIdentityClient.getCredentialsForIdentity(GetCredentialsForIdentityRequest { this.identityId = identityId }).credentials
* ```
*
*
* To create a `StaticCredentialsProvider` as a `CredentialsProvider` from the obtained credentials:
* 1. Use the credentials obtained in the previous steps:
* ``` kotlin
* val staticCredentialsProvider = StaticCredentialsProvider(
* aws.smithy.kotlin.runtime.auth.awscredentials.Credentials.invoke(
* accessKeyId = credentials.accessKeyId,
* secretAccessKey = credentials.secretKey,
* sessionToken = credentials.sessionToken,
* expiration = credentials.expiration
* )
* )
* ```
*
* @param region The AWS region as a string.
* @param credentialsProvider The `CredentialsProvider` from AWS Kotlin SDK (`aws.smithy.kotlin.runtime.auth.awscredentials`).
* @return A `LocationCredentialsProvider` object.
*/
fun authenticateWithApiKey(
apiKey: String,
): LocationCredentialsProvider = LocationCredentialsProvider(
context,
apiKey,
)
suspend fun authenticateWithCredentialsProvider(
region: String,
credentialsProvider: CredentialsProvider
): LocationCredentialsProvider {
return withContext(Dispatchers.IO) {
val locationCredentialsProvider = LocationCredentialsProvider(
context,
AwsRegions.fromName(region),
)
locationCredentialsProvider.initializeLocationClient(credentialsProvider)
locationCredentialsProvider
}
}
}
Loading
Loading