/** * Utility methods for custom usage of react-native-keychain. * We save both the password and access_token as "password" in the keychain. * * Based on https://material.io/design/typography/the-type-system.html#type-scale * @format * @flow */ import * as Keychain from "react-native-keychain"; import { AuthTypes } from "../Enums"; type UserCredentials = | false | { username: string; password: string; accessToken: string }; /** * Saves username, password, and token in the keychain. * Password and token are saved as stringified JSON object in the "password" field. * * @param {string} username * @param {string} password * @param {string} accessToken * @param {string} authType */ export async function saveUserCredentials( username: string, password: string, authType: string, accessToken: string ) { const passwordAndToken = { password, accessToken, }; await Keychain.setGenericPassword( username, JSON.stringify(passwordAndToken), authType ); } /** * Gets the user credentials from the keychain. * * Returns false or the object itself, with the structure being * { username: string, password: string, accessToken: string } */ export async function getUserCredentials( authType: string ): Promise { try { // $FlowFixMe suppressing this error because flow can't interpret the Keychain method const credentials = await Keychain.getGenericPassword(authType); if (credentials) { const { username, password } = credentials; const passwordAndTokenObject = JSON.parse(password); const actualPassword = passwordAndTokenObject.password; const { accessToken } = passwordAndTokenObject; return { username, password: actualPassword, accessToken }; } // // console.log('No credentials stored'); return false; } catch (error) { // // console.log('Error accessing keychain or parsing result', error); return false; } } /** * Forgets user credentials in Keychain */ export async function forgetUserCredentials() { await Keychain.resetGenericPassword(AuthTypes.USER_AUTH); } /* reset password keychain */ export async function resetGenericPassword() { await Keychain.resetGenericPassword(AuthTypes.USER_AUTH); } /** * Saves the client token in the keychain, with the clientID being the 'username' * and the the token being the 'password' * @param {*} clientID * @param {*} accessToken */ export async function saveClientToken(clientID: string, accessToken: string) { await Keychain.setGenericPassword( clientID, accessToken, AuthTypes.CLIENT_AUTH ); } /** * Returns token string or empty string. */ export async function getClientToken(): Promise { try { // $FlowFixMe suppressing this error because flow can't interpret the Keychain method const credentials = await Keychain.getGenericPassword( AuthTypes.CLIENT_AUTH ); if (credentials) { // the access token is saved in the password field of keychain const { password } = credentials; return password; } return ""; } catch (error) { // // console.log('Error accessing keychain or parsing result', error); return ""; } } export async function getSavedToken( authType: "USER_AUTH" | "CLIENT_AUTH" | "USER_ORG_AUTH" | "ORGID_USER_AUTH" ): Promise { if ( authType === AuthTypes.USER_AUTH || authType === AuthTypes.USER_ORG_AUTH || authType === AuthTypes.ORGID_USER_AUTH ) { const userCredentials = await getUserCredentials(authType); if (userCredentials) { return userCredentials.accessToken; } } if (authType === AuthTypes.CLIENT_AUTH) { return getClientToken(); } return ""; } /** * Forgets client access token in Keychain */ export async function forgetClientCredentials() { await Keychain.resetGenericPassword(AuthTypes.CLIENT_AUTH); } /** * Forgets username, password, and accessToken saved in Keychain * * Currently, this is only used for testing. */ export async function forgetCredentials() { await Keychain.resetGenericPassword(AuthTypes.USER_AUTH); await Keychain.resetGenericPassword(AuthTypes.CLIENT_AUTH); }