-
Notifications
You must be signed in to change notification settings - Fork 1
Integrate Prove Bot Detection SDK for SMS fraud prevention #56
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
Open
Copilot
wants to merge
7
commits into
main
Choose a base branch
from
copilot/integrate-prove-bot-detection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
84e4d7e
Initial plan
Copilot 17f3146
Add Prove Bot Detection integration
Copilot 264df6a
Fix Prove repository configuration to use allprojects block
Copilot 03eb88f
Add Prove manager
ToluwalaseCato a7fde1b
Add Prove manager
ToluwalaseCato d9be0cc
Merge branch 'main-prove' into copilot/integrate-prove-bot-detection
ToluwalaseCato 361cd1c
Update fixes
ToluwalaseCato File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,6 @@ | ||
| { | ||
| "Prove": { | ||
| "clientId": "nativeauthfraudcheck-poc-e7971c6c-c35b-4e17-80f8-99aefdb611c0-1770372962304", | ||
| "clientSecret": "CgvbgUQpZ95TXZOkYgi3TgOs1MITJj6L" | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/azuresamples/msalnativeauthandroidkotlinsampleapp/MainApplication.kt
This file contains hidden or 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,43 @@ | ||
| package com.azuresamples.msalnativeauthandroidkotlinsampleapp | ||
|
|
||
| import android.app.Application | ||
| import android.util.Log | ||
| import org.json.JSONObject | ||
|
|
||
| class MainApplication : Application() { | ||
|
|
||
| override fun onCreate() { | ||
| super.onCreate() | ||
|
|
||
| Log.d("MS", "Application onCreate called") | ||
| initializeProveIfConfigured() | ||
| } | ||
|
|
||
| private fun initializeProveIfConfigured() { | ||
| // Read Prove credentials directly from the packaged config file: | ||
| // `app/src/main/assets/config.json` | ||
| val (clientId, clientSecret) = try { | ||
| val jsonText = assets.open("config.json").bufferedReader().use { it.readText() } | ||
| val proveJson = JSONObject(jsonText).optJSONObject("Prove") | ||
| val id = proveJson?.optString("clientId").orEmpty().trim() | ||
| val secret = proveJson?.optString("clientSecret").orEmpty().trim() | ||
| id to secret | ||
| } catch (t: Throwable) { | ||
| Log.e("MS", "Failed to read Prove config from assets/config.json", t) | ||
| "" to "" | ||
| } | ||
|
|
||
| if (clientId.isBlank() || clientSecret.isBlank()) { | ||
| Log.w( | ||
| "MS", | ||
| "Prove not initialized. Set Prove.clientId and Prove.clientSecret in app/src/main/assets/config.json." | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| ProveManager.getInstance().initialize( | ||
| clientId = clientId, | ||
| clientSecret = clientSecret | ||
| ) | ||
| } | ||
| } |
140 changes: 140 additions & 0 deletions
140
...ain/java/com/azuresamples/msalnativeauthandroidkotlinsampleapp/ProveBotDetectionHelper.kt
This file contains hidden or 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,140 @@ | ||
| package com.azuresamples.msalnativeauthandroidkotlinsampleapp | ||
|
|
||
| import android.content.Context | ||
| import android.util.Log | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.withContext | ||
| import okhttp3.MediaType.Companion.toMediaType | ||
| import okhttp3.OkHttpClient | ||
| import okhttp3.Request | ||
| import okhttp3.RequestBody.Companion.toRequestBody | ||
| import org.json.JSONObject | ||
|
|
||
| /** | ||
| * Helper class for integrating Prove Bot Detection into the sign-in/sign-up flows. | ||
| * | ||
| * Flow overview (per https://developer.prove.com/docs/check-for-prove-key#prompt-for-phone-number-3): | ||
| * 1. Initialize the Prove SDK on app start to obtain a device-level "Prove Key". | ||
| * 2. Before sending an OTP / completing sign-in, call YOUR backend with the phone number + Prove Key. | ||
| * 3. Your backend calls Prove's /v3/verify API with verificationType="bot". | ||
| * 4. The backend returns the bot-detection result to the app. | ||
| * 5. The app decides whether to proceed with authentication or block the attempt. | ||
| */ | ||
| object ProveBotDetectionHelper { | ||
|
|
||
| private const val TAG = "ProveBotDetection" | ||
|
|
||
| // IMPORTANT: Replace with your own backend endpoint that proxies to Prove's API. | ||
| // NEVER call Prove's API directly from the client — your access token must stay server-side. | ||
| private const val BOT_DETECTION_BACKEND_URL = "https://your-backend.example.com/api/prove/bot-detect" | ||
|
|
||
| private val httpClient = OkHttpClient() | ||
| private var proveKey: String? = null | ||
|
|
||
| /** | ||
| * Initialize the Prove SDK and retrieve the Prove Key (device fingerprint). | ||
| * Call this once during app startup (e.g., in AuthClient.initialize). | ||
| */ | ||
| fun initialize(context: Context) { | ||
| try { | ||
| // Initialize the Prove SDK — the exact API depends on the SDK version. | ||
| // Refer to: https://developer.prove.com/reference/unify-android-sdk | ||
| // | ||
| // Example (pseudocode — adapt to actual SDK): | ||
| // ProveAuth.initialize(context, "YOUR_PROVE_CLIENT_ID") | ||
| // proveKey = ProveAuth.getDeviceId() | ||
|
|
||
| Log.i(TAG, "Prove SDK initialized. Prove Key obtained.") | ||
|
|
||
| // TODO: Replace the line below with real SDK call | ||
| // proveKey = ProveAuth.getDeviceId() | ||
| proveKey = "PLACEHOLDER_PROVE_KEY" // Remove this after real integration | ||
| } catch (e: Exception) { | ||
| Log.e(TAG, "Failed to initialize Prove SDK", e) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check whether the Prove Key is available. | ||
| */ | ||
| fun hasProveKey(): Boolean { | ||
| return !proveKey.isNullOrBlank() | ||
| } | ||
|
|
||
| /** | ||
| * Perform bot detection by calling YOUR backend with the phone number and Prove Key. | ||
| * | ||
| * Your backend should: | ||
| * 1. Obtain a Prove access token (OAuth2 client_credentials grant). | ||
| * 2. POST to https://platform.prove.com/v3/verify with: | ||
| * { | ||
| * "verificationType": "bot", | ||
| * "phoneNumber": "<phone>", | ||
| * "deviceId": "<proveKey>", | ||
| * "clientRequestId": "<unique-session-id>" | ||
| * } | ||
| * 3. Return the result to the mobile app. | ||
| * | ||
| * @param phoneNumber The phone number entered by the user (E.164 format recommended) | ||
| * @return BotDetectionResult indicating whether the user passed or failed bot detection | ||
| */ | ||
| suspend fun performBotDetection(phoneNumber: String): BotDetectionResult { | ||
| if (!hasProveKey()) { | ||
| Log.w(TAG, "Prove Key not available. Cannot perform bot detection.") | ||
| return BotDetectionResult.Error("Prove Key not available. Please restart the app.") | ||
| } | ||
|
|
||
| return withContext(Dispatchers.IO) { | ||
| try { | ||
| val requestBody = JSONObject().apply { | ||
| put("phoneNumber", phoneNumber) | ||
| put("deviceId", proveKey) | ||
| put("clientRequestId", java.util.UUID.randomUUID().toString()) | ||
| }.toString() | ||
|
|
||
| val request = Request.Builder() | ||
| .url(BOT_DETECTION_BACKEND_URL) | ||
| .post(requestBody.toRequestBody("application/json".toMediaType())) | ||
| .build() | ||
|
|
||
| val response = httpClient.newCall(request).execute() | ||
|
|
||
| if (response.isSuccessful) { | ||
| val responseBody = response.body?.string() ?: "{}" | ||
| val json = JSONObject(responseBody) | ||
|
|
||
| val isHuman = json.optBoolean("success", false) | ||
| val score = json.optDouble("score", 0.0) | ||
|
|
||
| if (isHuman) { | ||
| Log.i(TAG, "Bot detection passed. Score: $score") | ||
| BotDetectionResult.Passed(score) | ||
| } else { | ||
| Log.w(TAG, "Bot detection failed. Score: $score") | ||
| BotDetectionResult.Failed(score, "Phone number flagged as potential fraud.") | ||
| } | ||
| } else { | ||
| Log.e(TAG, "Bot detection API returned ${response.code}") | ||
| BotDetectionResult.Error("Bot detection service unavailable (HTTP ${response.code}).") | ||
| } | ||
| } catch (e: Exception) { | ||
| Log.e(TAG, "Bot detection request failed", e) | ||
| BotDetectionResult.Error("Bot detection failed: ${e.message}") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sealed class representing the result of a bot detection check. | ||
| */ | ||
| sealed class BotDetectionResult { | ||
| /** The phone number passed bot detection (appears to be a real human). */ | ||
| data class Passed(val score: Double) : BotDetectionResult() | ||
|
|
||
| /** The phone number failed bot detection (potential bot/fraud). */ | ||
| data class Failed(val score: Double, val reason: String) : BotDetectionResult() | ||
|
|
||
| /** An error occurred during bot detection. */ | ||
| data class Error(val message: String) : BotDetectionResult() | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we remove
clientSecretfrom public sample?