Skip to content
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

docs: [SIW-1136] Update README.md #14

Merged
merged 6 commits into from
May 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 171 additions & 6 deletions README.md
hevelius marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,23 +1,188 @@
# @pagopa/io-react-native-integrity

React Native package which bridges Andoird Play Integrity API and iOS App Attest
![CI workflow](https://github.com/pagopa/io-react-native-integrity/actions/workflows/ci.yml/badge.svg)
![Publish workflow](https://github.com/pagopa/io-react-native-integrity/actions/workflows/publish.yml/badge.svg)
![Code QL workflow](https://github.com/pagopa/io-react-native-integrity/actions/workflows/github-code-scanning/codeql/badge.svg)

React Native interfaces for managing secure storage in iOS and Android.

## Installation

```sh
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](https://developer.android.com/google/play/integrity/overview) 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](example/backend) directory. Make sure to follow the instructions in the [example/README.md](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](https://developer.android.com/google/play/integrity/standard?hl=it).
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.

```ts
try {
const isServiceAvailable = await isPlayServicesAvailable();
if (isServiceAvailable) {
// Proceed with the following steps
} else {
// Return an error message
}
} catch (e) {
const error = e as IntegrityError;
setStatus(`Error: ${error.message}`);
console.log(JSON.stringify(e));
}
```

## Usage
### `isPlayServicesAvailable`

Returns a boolean value indicating whether the Play Services are available on the device or not.

```ts
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](https://developer.android.com/google/play/integrity/setup).

```ts
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.

```ts
try {
const token = await requestIntegrityToken();
console.log(token);
} catch (e) {
const error = e as IntegrityError;
console.log(JSON.stringify(error));
}
```

```js
import { multiply } from '@pagopa/io-react-native-integrity';
### `getAttestation`

// ...
Returns a (Key Attestation)[https://developer.android.com/privacy-and-security/security-key-attestation] which can later be verified by the backend server.

const result = await multiply(3, 7);
```ts
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)[https://developer.apple.com/documentation/devicecheck/establishing-your-app-s-integrity] 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`.

```ts
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.

```ts
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.

```ts
try {
const signature = await generateHardwareSignatureWithAssertion(
clientData,
key
);
console.log(signature);
} 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 |

hevelius marked this conversation as resolved.
Show resolved Hide resolved
## Contributing

See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
Expand Down
Loading