forked from pagopa/io-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: [IOAPPCIT-24] Add a crypto key pair generation test at applica…
…tion startup. (pagopa#4295) ## Short description This PR adds a crypto key pair generation test at application startup using [io-react-native-crypto](https://github.com/pagopa/io-react-native-crypto) library. ## List of changes proposed in this pull request -⚠️ .circleci/config.yml: bumps `node` version from `14.17.4` to `16.19.0`. -⚠️ .node-version: bumps `node` version from `14.17.4` to `16.19.0`. -⚠️ android/build.gradle: bumps `kotlin` version from `1.5.21` to `1.7.0`. - yarn.lock: adds `io-react-natiive-crypto` library. - ios/Podfile.lock: adds `io-react-natiive-crypto` library pod. - package.json: adds `io-react-natiive-crypto` library. - ts/sagas/startup.ts: adds the key pair generation. - ts/sagas/startup/generateCryptoKeyPair.ts: this is the new key generation saga. - ts/store/reducers/__mock__/backendStatus.ts: update the mock with the new definitions. - ts/store/reducers/backendStatus.ts: add the selector for the new feature flag to enable/disable the key generation. - ts/utils/crypto.ts: utility functions for the `io-react-natiive-crypto` library. ## How to test Run the application on both Android and iOS physical devices against the [io-dev-api-server](https://github.com/pagopa/io-dev-api-server). Try running the application also on simulators: it's not guaranteed that the key generation will be successful, but also a crash-free unsuccessful event is good. You must set to `true` this [flag](https://github.com/pagopa/io-dev-api-server/blob/c9485701686abc0f47c4272f05d6fd5246a6cd7e/src/payloads/backend.ts#L78) on [io-dev-api-server](https://github.com/pagopa/io-dev-api-server) to enable the key generation. Try with different scenarios: - Try with the flag above in a disabled state with a first onboarding user. No key generation should happen. - Then enable the flag and restart the application. You should see a key generation attempt. - Try with the flag above in an enabled state with an already logged-in user: you should see a key generation attempt. - More up to you ;) Co-authored-by: Mario Perrotta <[email protected]> Co-authored-by: Cristiano Tofani <[email protected]>
- Loading branch information
1 parent
b5ec480
commit 7938ce2
Showing
10 changed files
with
118 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
14.17.4 | ||
16.19.0 |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
generate, | ||
deleteKey, | ||
CryptoError | ||
} from "@pagopa/io-react-native-crypto"; | ||
import { call } from "typed-redux-saga/macro"; | ||
import { | ||
checkPublicKeyExists, | ||
isKeyAlreadyGenerated, | ||
setKeyAlreadyGenerated | ||
} from "../../utils/crypto"; | ||
import { mixpanelTrack } from "./../../mixpanel"; | ||
|
||
const KEY_NAME = "lp-temp-key"; | ||
|
||
export function* generateCryptoKeyPair() { | ||
try { | ||
const keyAlreadyExistsOnKeystore = yield* call( | ||
checkPublicKeyExists, | ||
KEY_NAME | ||
); | ||
const keyHasBeenAlreadyGenerated = yield* call( | ||
isKeyAlreadyGenerated, | ||
KEY_NAME | ||
); | ||
const shouldWeGenerateKey = !( | ||
keyHasBeenAlreadyGenerated || keyAlreadyExistsOnKeystore | ||
); | ||
|
||
if (shouldWeGenerateKey) { | ||
const key = yield* call(generate, KEY_NAME); | ||
void mixpanelTrack("LOLLIPOP_KEY_GENERATION_SUCCESS", { | ||
kty: key.kty | ||
}); | ||
yield* call(setKeyAlreadyGenerated, KEY_NAME, key.kty); | ||
yield* call(deleteKey, KEY_NAME); | ||
} | ||
} catch (e) { | ||
const errorMessage = (e as CryptoError).message; | ||
void mixpanelTrack("LOLLIPOP_KEY_GENERATION_FAILURE", { | ||
reason: errorMessage | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import AsyncStorage from "@react-native-async-storage/async-storage"; | ||
import { pipe } from "fp-ts/lib/function"; | ||
import * as T from "fp-ts/lib/Task"; | ||
import * as TE from "fp-ts/lib/TaskEither"; | ||
import { getPublicKey } from "@pagopa/io-react-native-crypto"; | ||
|
||
export const setKeyAlreadyGenerated = async (keyTag: string, value: string) => | ||
pipe( | ||
TE.tryCatch( | ||
() => AsyncStorage.setItem(keyTag, value), | ||
() => false | ||
), | ||
TE.map(_ => true), | ||
TE.getOrElse(() => T.of(false)) | ||
)(); | ||
|
||
export const isKeyAlreadyGenerated = async (keyTag: string) => | ||
pipe( | ||
TE.tryCatch( | ||
() => AsyncStorage.getItem(keyTag), | ||
() => false | ||
), | ||
TE.map(value => value !== null), | ||
TE.getOrElse(() => T.of(false)) | ||
)(); | ||
|
||
export const checkPublicKeyExists = (keyId: string) => | ||
pipe( | ||
TE.tryCatch( | ||
() => getPublicKey(keyId), | ||
() => false | ||
), | ||
TE.map(_ => true), | ||
TE.getOrElse(() => T.of(false)) | ||
)(); |
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