@pagopa/io-react-native-integrity
v0.3.0
Published
React Native package which bridges Andoird Play Integrity API and iOS App Attest
Downloads
2,278
Readme
@pagopa/io-react-native-integrity
React Native interfaces for managing secure storage in iOS and Android.
Installation
npm install @pagopa/io-react-native-integrity
# or
yarn add @pagopa/io-react-native-integrity
Android
The Android implementation is based on the Play Integrity API which provides a set of APIs to help developers protect their apps from tampering. The usage of this API also requires a backend server to verify the integrity token generated by the app.
An example is provided in the example/backend directory. Make sure to follow the instructions in the example/README.md file to set up the backend server and update the .env
file with the correct values to test the library.
The flow provided by the library is the standard one.
The main difference from iOS is that Android relies on Google Play Services and a remote service provided by Google.
A (Key Attestation)[https://developer.android.com/privacy-and-security/security-key-attestation] can be generated using the getAttestation
method.
During key attestation, a key pair is generated along with its certificate chain hich can be used to verify the properties of that key pair.
If the device supports hardware-level key attestation, the root certificate of the chain is signed using an attestation root key protected by the device's hardware-backed keystore.
isPlayServicesAvailable
Returns a boolean value indicating whether the Play Services are available on the device or not.
try {
const isServiceAvailable = await isPlayServicesAvailable();
if (isServiceAvailable) {
// Proceed with the following steps
} else {
// Return an error message
}
} catch (e) {
const error = e as IntegrityError;
console.log(JSON.stringify(error));
}
prepareIntegrityToken
Prepares the integrity token provider before obtaining the integrity verdict. This function can be called well before the moment an integrity verdict is needed, for example when starting the application. It can also be called time to time to refresh it. A Google Cloud Project Number is required to use this API. Follow the official documentation provided by Google here.
try {
await prepareIntegrityToken(GOOGLE_CLOUD_PROJECT_NUMBER);
} catch (e) {
const error = e as IntegrityError;
console.log(JSON.stringify(error));
}
requestIntegrityToken
Requests an integrity token which is then attached to the request to be protected.
It must be called AFTER prepareIntegrityToken
has been called and resolved successfully.
The token is a base64 encoded string.
try {
const token = await requestIntegrityToken();
console.log(token);
} catch (e) {
const error = e as IntegrityError;
console.log(JSON.stringify(error));
}
getAttestation
Returns a Key Attestation which can later be verified by the backend server.
try {
const attestation = await getAttestation();
console.log(attestation);
} catch (e) {
const error = e as IntegrityError;
console.log(JSON.stringify(error));
}
iOS
The iOS implementation is based on the DCAppAttestService which is availabe from iOS 14.0 onwards. It's a framework provided by Apple for iOS that allows apps to verify if a specific device has been used to perform a particular action, without revealing the identity of the device itself or its owner. It is useful for preventing abuse by users who try to manipulate the system or use the app fraudulently. This implementation doesn't relay on any remote service.
generateHardwareKey
Generates a hardware key that can be used into the attestation process when calling getAttestaiton
.
try {
const key = await generateHardwareKey();
console.log(key);
} catch (e) {
const error = e as IntegrityError;
console.log(JSON.stringify(error));
}
getAttestation
Generates an attestation for the given challenge and hardware key. The hardware key generated at the previous step can be used here.
try {
const attestation = await getAttestation(challenge, key);
console.log(attestation);
} catch (e) {
const error = e as IntegrityError;
console.log(JSON.stringify(error));
}
generateHardwareSignatureWithAssertion
Generates a signature for the given client data given an hardware key. The hardware key generate at the previous step can be used here.
try {
const signature = await generateHardwareSignatureWithAssertion(
clientData,
key
);
console.log(signature);
} catch (e) {
const error = e as IntegrityError;
console.log(JSON.stringify(error));
}
decodeAttestation
Decodes the CBOR encoded attestation returned by the getAttestation
method. Returns an object containing signature
and authenticatorData
as base64 encoded strings.
try {
const decoded = await decodeAttestation(attestation);
console.log(decoded);
} catch (e) {
const error = e as IntegrityError;
console.log(JSON.stringify(error));
}
Types
| TypeName | Description | | :----------------: | --------------------------------------------------------------------------------------------------------------------------------- | | SecureStorageError | This type defines the error returned by the secure storage engine and includes an error code and an additional information object |
Error Codes
| TypeName | Platform | Description |
| :--------------------------------------: | :---------: | ---------------------------------------------------------------------------------------------------- |
| WRONG_GOOGLE_CLOUD_PROJECT_NUMBER_FORMAT | Android | A wrong value for GOOGLE_CLOUD_PROJECT_NUMBER
has been provided to prepareIntegrityToken
|
| PREPARE_FAILED | Android | A critical error occurred during the prepareIntegrityToken
operation |
| PREPARE_NOT_CALLED | Android | The requestIntegrityToken
has been called without calling prepareIntegrityToken
beforehand |
| REQUEST_TOKEN_FAILED | Android | A critical error occurred during the requestIntegrityToken
operation |
| REQUEST_ATTESTATION_FAILED | Android | A critical error occurred during the getAttestation
operation |
| KEY_IS_NOT_HARDWARE_BACKED | Android | The device doesn't support hardware backed keys, thus it cannot be trusted |
| KEYSTORE_NOT_INITIALIZED | Android | A critical error occurred while initializing the keystore service |
| GENERATION_KEY_FAILED | iOS | A critical error occurred during the generateHardwareKey
operation |
| ATTESTATION_ERROR | iOS | A critical error occurred during the getAttestation
operation |
| UNSUPPORTED_IOS_VERSION | iOS | The device has a version of iOS that doesn't support the DeviceCheck App Attestation Service (<14.0) |
| CHALLENGE_ERROR | iOS | An error occured while encoding the provided challenge to getAttestation
|
| CLIENT_DATA_ENCODING_ERROR | iOS | An error occured while encoding the provided client data to generateHardwareSignatureWithAssertion
|
| GENERATION_ASSERTION_FAILED | iOS | A critical error occurred during the generateHardwareSignatureWithAssertion
operation |
| UNSUPPORTED_DEVICE | iOS/Android | The device doesn't support the requested functionality |
Contributing
See the contributing guide to learn how to contribute to the repository and the development workflow.
License
MIT
Made with create-react-native-library