-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from makeen-project/main
Added authentication with API key and log added for generating credential failure
- Loading branch information
Showing
15 changed files
with
540 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
library/src/main/java/software/amazon/location/auth/ApiKeyCredentialsProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package software.amazon.location.auth | ||
|
||
import android.content.Context | ||
import software.amazon.location.auth.utils.Constants.API_KEY | ||
|
||
/** | ||
* Provides API key credentials for accessing services and manages their storage. | ||
*/ | ||
class ApiKeyCredentialsProvider { | ||
private var securePreferences: EncryptedSharedPreferences? = null | ||
|
||
/** | ||
* Initializes the provider and saves the provided API key. | ||
* @param context The application context. | ||
* @param apiKey The API key to save. | ||
*/ | ||
constructor(context: Context, apiKey: String) { | ||
initialize(context) | ||
saveCredentials(apiKey) | ||
} | ||
|
||
/** | ||
* Initializes the provider and retrieves the API key from the cache. | ||
* @param context The application context. | ||
* @throws Exception If no credentials are found in the cache. | ||
*/ | ||
constructor(context: Context) { | ||
initialize(context) | ||
val apiKey = getCachedCredentials() | ||
if (apiKey === null) throw Exception("No credentials found") | ||
} | ||
|
||
private fun initialize(context: Context) { | ||
securePreferences = EncryptedSharedPreferences(context, PREFS_NAME) | ||
securePreferences?.initEncryptedSharedPreferences() | ||
} | ||
|
||
private fun saveCredentials(apiKey: String) { | ||
if (securePreferences === null) throw Exception("Not initialized") | ||
securePreferences!!.put(API_KEY, apiKey) | ||
} | ||
|
||
/** | ||
* Retrieves the cached API key credentials. | ||
* @return The API key or null if not found. | ||
* @throws Exception If the AWSKeyValueStore is not initialized. | ||
*/ | ||
fun getCachedCredentials(): String? { | ||
if (securePreferences === null) throw Exception("Not initialized") | ||
return securePreferences!!.get(API_KEY) | ||
} | ||
|
||
/** | ||
* Clears the stored credentials. | ||
*/ | ||
fun clearCredentials() { | ||
securePreferences?.remove(API_KEY) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
library/src/main/java/software/amazon/location/auth/ApiKeyInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package software.amazon.location.auth | ||
|
||
import aws.smithy.kotlin.runtime.client.ProtocolRequestInterceptorContext | ||
import aws.smithy.kotlin.runtime.http.interceptors.HttpInterceptor | ||
import aws.smithy.kotlin.runtime.http.request.HttpRequest | ||
import aws.smithy.kotlin.runtime.http.request.toBuilder | ||
import aws.smithy.kotlin.runtime.http.request.url | ||
import aws.smithy.kotlin.runtime.net.url.Url | ||
import software.amazon.location.auth.utils.Constants.QUERY_PARAM_KEY | ||
|
||
class ApiKeyInterceptor( | ||
private val apiKey: String, | ||
) : HttpInterceptor { | ||
override suspend fun modifyBeforeSigning(context: ProtocolRequestInterceptorContext<Any, HttpRequest>): HttpRequest { | ||
val req = context.protocolRequest.toBuilder() | ||
|
||
if (!context.protocolRequest.url.toString().contains("$QUERY_PARAM_KEY=")) { | ||
req.url(Url.parse(context.protocolRequest.url.toString()+"?$QUERY_PARAM_KEY=$apiKey")) | ||
return req.build() | ||
} else { | ||
return super.modifyBeforeSigning(context) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.