-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement android attestation module (#1016)
Signed-off-by: Bryce McMath <[email protected]>
- Loading branch information
1 parent
71c84dd
commit 374b616
Showing
6 changed files
with
103 additions
and
16 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
89 changes: 77 additions & 12 deletions
89
packages/react-native-attestation/android/src/main/java/com/attestation/AttestationModule.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 |
---|---|---|
@@ -1,24 +1,89 @@ | ||
package com.attestation | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.ReactMethod | ||
import com.facebook.react.bridge.Promise | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactMethod; | ||
import com.facebook.react.bridge.Promise; | ||
import com.google.android.gms.common.ConnectionResult; | ||
import com.google.android.gms.common.GooglePlayServicesUtil; | ||
import com.google.android.gms.tasks.Task; | ||
import com.google.android.play.core.integrity.IntegrityManager; | ||
import com.google.android.play.core.integrity.IntegrityManagerFactory; | ||
import com.google.android.play.core.integrity.IntegrityServiceException; | ||
import com.google.android.play.core.integrity.IntegrityTokenRequest; | ||
import com.google.android.play.core.integrity.IntegrityTokenResponse; | ||
|
||
class AttestationModule internal constructor(context: ReactApplicationContext) : | ||
AttestationSpec(context) { | ||
class AttestationModule : AttestationSpec { | ||
|
||
private val reactContext: ReactApplicationContext; | ||
private val baseContext: ReactApplicationContext; | ||
|
||
constructor(context: ReactApplicationContext) : super(context) { | ||
reactContext = context; | ||
baseContext = getReactApplicationContext(); | ||
} | ||
|
||
override fun getName(): String { | ||
return NAME | ||
} | ||
|
||
// Example method | ||
// See https://reactnative.dev/docs/native-modules-android | ||
companion object { | ||
const val NAME = "Attestation" | ||
} | ||
|
||
/** | ||
* Checks if Google Play Integrity API is available | ||
* See https://developer.android.com/google/play/integrity/overview | ||
* | ||
* @param promise | ||
*/ | ||
@ReactMethod | ||
override fun multiply(a: Double, b: Double, promise: Promise) { | ||
promise.resolve(a * b) | ||
override fun isPlayIntegrityAvailable(promise: Promise) { | ||
try { | ||
val apiAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(baseContext) == ConnectionResult.SUCCESS; | ||
promise.resolve(apiAvailable); | ||
} catch (e: Throwable) { | ||
promise.reject("Error checking Play Integrity availability", e) | ||
} | ||
} | ||
|
||
companion object { | ||
const val NAME = "Attestation" | ||
/** | ||
* Request an integrity verdict | ||
* See https://developer.android.com/google/play/integrity/verdict#request | ||
* | ||
* @param nonce | ||
* @param promise | ||
*/ | ||
@ReactMethod | ||
override fun googleAttestation(nonce: String, promise: Promise) { | ||
try { | ||
// Create an instance of a manager | ||
val integrityManager: IntegrityManager = | ||
IntegrityManagerFactory.create(baseContext) | ||
|
||
// Request the integrity token by providing a nonce | ||
val integrityTokenResponse: Task<IntegrityTokenResponse> = | ||
integrityManager.requestIntegrityToken( | ||
IntegrityTokenRequest.builder() | ||
.setNonce(nonce) | ||
.build()) | ||
|
||
// Success listener | ||
integrityTokenResponse.addOnSuccessListener { response: IntegrityTokenResponse -> promise.resolve(response.token()) }; | ||
|
||
// Cancelled listener | ||
integrityTokenResponse.addOnCanceledListener { -> promise.reject("Integrity token request cancelled") }; | ||
|
||
// Failure listener | ||
integrityTokenResponse.addOnFailureListener { e: Exception -> | ||
if (e is IntegrityServiceException) { | ||
promise.reject(e.getErrorCode().toString(), e) | ||
} else { | ||
promise.reject("Unexpected failure during integrity token request", e) | ||
} | ||
}; | ||
|
||
} catch (e: Throwable) { | ||
promise.reject("Error requesting integrity token", e); | ||
} | ||
} | ||
} | ||
} |
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