Skip to content

Instantly share code, notes, and snippets.

@gjerlow
Last active June 25, 2019 23:02
Show Gist options
  • Save gjerlow/57e0b980ad23ed6df2f02d6022adafc7 to your computer and use it in GitHub Desktop.
Save gjerlow/57e0b980ad23ed6df2f02d6022adafc7 to your computer and use it in GitHub Desktop.
Using ti.identity for keychain functionality in Titanium applications
const TiIdentity = require('ti.identity');
//Identifier of the application. The same as development team on provisioning profile
const appIdentifierPrefix = 'YOUR_IDENTIFIER';
//Create the Keychain Items
const keychainUn = TiIdentity.createKeychainItem({
identifier: Alloy.CFG.kc_user_identifier,
accessGroup: appIdentifierPrefix + '.' + Ti.App.id
});
const keychainPw = TiIdentity.createKeychainItem({
identifier: Alloy.CFG.kc_pw_identifier,
accessGroup: appIdentifierPrefix + '.' + Ti.App.id
});
//Listeners for verifiyng that save/read works (remove from production apps!)
// Triggered when the keychain item was successfully saved
keychainUn.addEventListener('save', function(e) {
// Notify the user that the operation succeeded or failed
console.log('USERNAME SAVED');
});
// Triggered when the keychain item was successfully saved
keychainUn.addEventListener('read', function(e) {
// Notify the user that the operation succeeded or failed
console.log('USERNAME READ:', e);
});
/**
* Check if the provided Keychain Item exits, and returns it's value
* @param {Object} keychainItem the keychain item to get value from
* @return {String} value of the keychain item
*/
const getKeychainValue = (keychainItem) => {
return new Promise((resolve, reject) => {
//Check if keychain item with identifier exists
keychainItem.fetchExistence((e) => {
if (e.exists) {
//Read value of the keychain item
keychainItem.addEventListener('read', (e) => {
if (!e.success) {
Ti.API.error('Error: ' + e.error);
reject(null);
}
//Return the value
resolve(e.value);
});
keychainItem.read();
} else {
console.error('Could not find KeychainItem. Incorrect appIdentifierPrefix?');
reject(null);
}
});
});
};
/**
* Saves a value to a Keychain Item. If it already exist, it gets updated
* @param {Object} keychainItem keychain item to set value to
* @param {String} val value to save
*/
const saveKeychainValue = (keychainItem, val) => {
keychainItem.fetchExistence((e) => {
if (e.exists) {
keychainItem.update(val);
} else {
keychainItem.save(val);
}
});
};
//Get a 'user credentials' object
exports.getCredentials = async () => {
return {
un: await getKeychainValue(keychainUn),
pw: await getKeychainValue(keychainPw)
};
};
//Save functions
exports.setUsername = (email) => saveKeychainValue(keychainUn, email);
exports.setPassword = (pw) => saveKeychainValue(keychainPw, pw);
//Reset functions
exports.resetUsername = () => keychainUn.reset();
exports.resetPassword = () => keychainPw.reset();
const keychain = require('/keychainService');
//Save username/password to keychain
keychain.setUsername('SOME_USERNAME');
keychain.setPassword('SOME_PASSWORD');
//Get credentials object ({username, password})
keychain.getCredentials();
//Reset keychain storage
keychain.resetUsername();
keychain.resetPassword();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment