Skip to content

Commit

Permalink
Ensure ObjC API's are not used unless needed.
Browse files Browse the repository at this point in the history
Fixes #377
  • Loading branch information
leonjza committed May 26, 2020
1 parent ac94e70 commit 8e53e4b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
12 changes: 5 additions & 7 deletions agent/src/ios/filesystem.ts
Expand Up @@ -4,8 +4,6 @@ import { getNSFileManager } from "./lib/helpers";
import { IIosFilePath, IIosFileSystem } from "./lib/interfaces";
import { NSDictionary, NSFileManager, NSString as NSStringType } from "./lib/types";

const { NSString } = ObjC.classes;

export namespace iosfilesystem {

// a resolved nsfilemanager instance
Expand All @@ -29,7 +27,7 @@ export namespace iosfilesystem {
// }

const fm: NSFileManager = getFileManager();
const p: NSStringType = NSString.stringWithString_(path);
const p: NSStringType = ObjC.classes.NSString.stringWithString_(path);

return fm.fileExistsAtPath_(p);
};
Expand All @@ -41,7 +39,7 @@ export namespace iosfilesystem {
// NSLog(@"%d / readable?", [fm isReadableFileAtPath:@"/"]);

const fm: NSFileManager = getFileManager();
const p: NSStringType = NSString.stringWithString_(path);
const p: NSStringType = ObjC.classes.NSString.stringWithString_(path);

return fm.isReadableFileAtPath_(p);
};
Expand All @@ -53,7 +51,7 @@ export namespace iosfilesystem {
// NSLog(@"%d / readable?", [fm isReadableFileAtPath:@"/"]);

const fm: NSFileManager = getFileManager();
const p: NSStringType = NSString.stringWithString_(path);
const p: NSStringType = ObjC.classes.NSString.stringWithString_(path);

return fm.isWritableFileAtPath_(p);
};
Expand Down Expand Up @@ -124,7 +122,7 @@ export namespace iosfilesystem {
// }

const fm: NSFileManager = getFileManager();
const p: NSStringType = NSString.stringWithString_(path);
const p: NSStringType = ObjC.classes.NSString.stringWithString_(path);

const response: IIosFileSystem = {
files: {},
Expand Down Expand Up @@ -154,7 +152,7 @@ export namespace iosfilesystem {

// generate a full path to the file
const filePath: string = [path, "/", file].join("");
const currentFilePath: NSStringType = NSString.stringWithString_(filePath);
const currentFilePath: NSStringType = ObjC.classes.NSString.stringWithString_(filePath);

// check read / write
pathFileData.readable = fm.isReadableFileAtPath_(currentFilePath);
Expand Down
10 changes: 4 additions & 6 deletions agent/src/ios/keychain.ts
Expand Up @@ -12,8 +12,6 @@ import {
NSString as NSStringType,
} from "./lib/types";

const { NSMutableDictionary, NSString } = ObjC.classes;

// keychain item times to query for
const itemClasses = [
kSec.kSecClassKey,
Expand Down Expand Up @@ -64,7 +62,7 @@ export namespace ioskeychain {
const kCFBooleanTrue = ObjC.classes.__NSCFBoolean.numberWithBool_(true);

// the base query dictionary to use for the keychain lookups
const searchDictionary: NSMutableDictionaryType = NSMutableDictionary.alloc().init();
const searchDictionary: NSMutableDictionaryType = ObjC.classes.NSMutableDictionary.alloc().init();
searchDictionary.setObject_forKey_(kCFBooleanTrue, kSec.kSecReturnAttributes);
searchDictionary.setObject_forKey_(kCFBooleanTrue, kSec.kSecReturnData);
searchDictionary.setObject_forKey_(kCFBooleanTrue, kSec.kSecReturnRef);
Expand Down Expand Up @@ -151,7 +149,7 @@ export namespace ioskeychain {

// clean out the keychain
export const empty = (): void => {
const searchDictionary: NSMutableDictionaryType = NSMutableDictionary.alloc().init();
const searchDictionary: NSMutableDictionaryType = ObjC.classes.NSMutableDictionary.alloc().init();
itemClasses.forEach((clazz) => {

// set the class-type we are querying for now & delete
Expand All @@ -164,7 +162,7 @@ export namespace ioskeychain {
export const add = (account: string, service: string, data: string): boolean => {

// prepare the dictionary for SecItemAdd()
const itemDict: NSMutableDictionaryType = NSMutableDictionary.alloc().init();
const itemDict: NSMutableDictionaryType = ObjC.classes.NSMutableDictionary.alloc().init();
itemDict.setObject_forKey_(kSec.kSecClassGenericPassword, kSec.kSecClass);

[
Expand All @@ -173,7 +171,7 @@ export namespace ioskeychain {
{ "type": "data", "value": data, "ksec": kSec.kSecValueData }
].forEach(e => {
if (e.value == null) return;
const v: NSStringType = NSString.stringWithString_(e.value)
const v: NSStringType = ObjC.classes.NSString.stringWithString_(e.value)
.dataUsingEncoding_(NSUTF8StringEncoding);

itemDict.setObject_forKey_(v, e.ksec);
Expand Down
4 changes: 1 addition & 3 deletions agent/src/ios/lib/helpers.ts
@@ -1,8 +1,6 @@
import { NSUTF8StringEncoding } from "./constants";
import { NSBundle, NSDictionary, NSFileManager, NSString as NSStringType } from "./types";

const NSString = ObjC.classes.NSString;

// Attempt to unarchive data. Returning a string of `` indicates that the
// unarchiving failed.
export const unArchiveDataAndGetString = (data: ObjC.Object | any): string => {
Expand Down Expand Up @@ -104,7 +102,7 @@ export const bytesToUTF8 = (data: any): string => {
return data.toString();
}

const s: NSStringType = NSString.alloc().initWithBytes_length_encoding_(
const s: NSStringType = ObjC.classes.NSString.alloc().initWithBytes_length_encoding_(
data.bytes(), data.length(), NSUTF8StringEncoding);

if (s) {
Expand Down

0 comments on commit 8e53e4b

Please sign in to comment.