Skip to content

Commit

Permalink
feat: implement android attestation module (#1016)
Browse files Browse the repository at this point in the history
Signed-off-by: Bryce McMath <[email protected]>
  • Loading branch information
bryce-mcmath authored Oct 31, 2023
1 parent 71c84dd commit 374b616
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 16 deletions.
14 changes: 11 additions & 3 deletions packages/react-native-attestation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ npm install react-native-attestation
import {
generateKey,
appleAttestation,
isPlayIntegrityAvailable,
googleAttestation,
} from '@hyperledger/aries-react-native-attestation';

// ...

const keyId = await generateKey();
const attestationAsBuffer = await appleAttestation(keyId, nonce);
if (Platform.OS === 'ios') {
const keyId = await generateKey();
const attestationAsBuffer = await appleAttestation(keyId, nonce);
} else if (Platform.OS === 'android') {
const available = await isPlayIntegrityAvailable()
if (available) {
const integrityToken = await googleAttestation(nonce)
}
}
```

## Contributing
Expand Down
2 changes: 2 additions & 0 deletions packages/react-native-attestation/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ dependencies {
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "com.google.android.gms:play-services-base:18.2.0"
implementation "com.google.android.play:integrity:1.2.0"
}

if (isNewArchitectureEnabled()) {
Expand Down
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ import com.facebook.react.bridge.Promise
abstract class AttestationSpec internal constructor(context: ReactApplicationContext) :
ReactContextBaseJavaModule(context) {

abstract fun multiply(a: Double, b: Double, promise: Promise)
abstract fun isPlayIntegrityAvailable(promise: Promise)
abstract fun googleAttestation(nonce: String, promise: Promise)
}
2 changes: 2 additions & 0 deletions packages/react-native-attestation/src/NativeAttestation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export interface Spec extends TurboModule {
generateKey(): Promise<string>;
sha256(stringToHash: string): Promise<Buffer>;
appleAttestation(keyId: string, challenge: string): Promise<Buffer>;
isPlayIntegrityAvailable(): Promise<boolean>;
googleAttestation(nonce: string): Promise<string>;
}

export default TurboModuleRegistry.getEnforcing<Spec>('Attestation');
9 changes: 9 additions & 0 deletions packages/react-native-attestation/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ export const appleAttestation = async (

return Buffer.from(bytes);
};

export const googleAttestation = async (nonce: string): Promise<string> => {
const token: string = await Attestation.googleAttestation(nonce);
return token;
};

export const isPlayIntegrityAvailable = async (): Promise<boolean> => {
return Attestation.isPlayIntegrityAvailable();
};

0 comments on commit 374b616

Please sign in to comment.