Skip to content

Commit

Permalink
Merge pull request #5 from bayonetio/develop
Browse files Browse the repository at this point in the history
RC v3.2.0
  • Loading branch information
imran-arshad authored Jun 20, 2023
2 parents 14cdf22 + d9042cc commit a20e7b1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ package io.bayonet.fingerprint.core.domain
* IFingerprintService is the interface that the Fingerprint Service has to implement
*/
interface IFingerprintService {
suspend fun analyze(): Token
fun analyze(listener: (Token) -> Unit, errorListener: (Error) -> Unit)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.bayonet.fingerprint.services

import android.content.Context
import androidx.annotation.WorkerThread
import java.io.IOException
import kotlin.jvm.Throws
import kotlinx.serialization.json.Json
Expand All @@ -10,6 +11,8 @@ import kotlinx.serialization.decodeFromString
import io.bayonet.fingeprint.R
import io.bayonet.fingerprint.core.domain.*
import io.bayonet.fingerprint.services.android.AndroidFingerprintJSService
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

val STORE_KEY = "bayonet"
val STORE_TOKEN_KEY = "token"
Expand All @@ -30,6 +33,8 @@ class FingerprintService(
// The RestAPI Service
private var restAPIService: IRestAPI;

private val executor: ExecutorService = Executors.newSingleThreadExecutor()

init {
// Validate the parameters
require(apiKey.isNotBlank()) { "The api key cannot be empty" }
Expand All @@ -51,44 +56,53 @@ class FingerprintService(
/**
* analyze fetch a token generated by the backend and then do the analysis
* of the device fingerprint with the external services
*
* @returns a token
*/
@Throws(IOException::class, InterruptedException::class)
override suspend fun analyze(): Token {
var token: Token

val storedToken = this.getStoreToken()
// The token not exists
if (storedToken == null) {

// Generate token from the RestAPI
val tokenResponse: GetTokenResponse = this.restAPIService.getToken()

token = Token(tokenResponse.bayonetID, tokenResponse.environment)

// Save the generated token
this.setStoreToken(token)

// Start third services
// Build the services configuration
val fingerprintjsServiceConfiguration = FingerprintJSServiceConfiguration(tokenResponse.services.fingerprintjs.apiKey)

// Initialize the FingerprintJS service
val fingerprintjsService = AndroidFingerprintJSService(ctx, fingerprintjsServiceConfiguration, token)

// Analyze with the FingerprintJS service
fingerprintjsService.analyze()
} else {
token = storedToken
try {
restAPIService.refresh(token)
} catch (err: Exception) {
//
@WorkerThread
override fun analyze(
listener: (Token) -> Unit,
errorListener: (Error) -> Unit
) {
executor.execute {
var token: Token

val storedToken = this.getStoreToken()

// The token not exists
if (storedToken == null) {
// Generate token from the RestAPI
try {
val tokenResponse = this.restAPIService.getToken()

token = Token(tokenResponse.bayonetID, tokenResponse.environment)

// Save the generated token
this.setStoreToken(token)

// Start third services
// Build the services configuration
val fingerprintjsServiceConfiguration = FingerprintJSServiceConfiguration(tokenResponse.services.fingerprintjs.apiKey)

// Initialize the FingerprintJS service
val fingerprintjsService = AndroidFingerprintJSService(ctx, fingerprintjsServiceConfiguration, token)

// Analyze with the FingerprintJS service
fingerprintjsService.analyze()

listener.invoke(token)
} catch (err: Exception) {
errorListener.invoke(Error("there is an error '${err.message}'"))
}
} else {
token = storedToken
try {
restAPIService.refresh(token)
} catch (err: Exception) {
println("error on refresh ${err}")
}

listener.invoke(token)
}
}

return token
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class RestAPIService(
override fun refresh(token: Token) {
val url = URL("${params.url}/${REST_API_REFRESH_TOKEN_PATH}/${token.bayonetID}")
val restApiHttpConnection = url.openConnection() as HttpURLConnection
restApiHttpConnection.requestMethod = "POST"
restApiHttpConnection.setRequestProperty("Authorization", "Bearer ${params.apiKey}")

val unauthorizedRequestCodes = listOf<Int>(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/res/values/backend.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
<string name="sandbox">sandbox</string>
<string name="live_url">https://api.bayonet.io/v3/fp</string>
<string name="sandbox_url">https://staging-api.bayonet.io/v3/fp</string>
<string name="develop_url">http://10.0.2.2:9000/v3</string>
<string name="develop_url">http://10.0.2.2:9000/v3/fp</string>
</resources>

0 comments on commit a20e7b1

Please sign in to comment.