Skip to content

Commit

Permalink
ios keychain added
Browse files Browse the repository at this point in the history
  • Loading branch information
Musab Hussain committed Jan 29, 2023
1 parent 8f2a58a commit 8ddcf2a
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 15 deletions.
9 changes: 8 additions & 1 deletion Assets/Plugins/Web3AuthSDK/Editor/BuildPostProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProj
urlSchemes.AddString(uri.Scheme);

infoPlist.WriteToFile(infoPlistPath);




using (StreamWriter sw = File.AppendText(pathToBuiltProject + "/Podfile"))
{
sw.WriteLine("pod 'Firebase/Messaging', '6.6.0'\n");
}


#endif
}
Expand Down
30 changes: 25 additions & 5 deletions Assets/Plugins/Web3AuthSDK/Keystore/KeyStoreManagerUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@
using Org.BouncyCastle.Crypto.Signers;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities.Encoders;
using System.Runtime.InteropServices;

public class KeyStoreManagerUtils
{
#if UNITY_IOS
[DllImport("__Internal")]
extern static int web3auth_keystore_set(string key, string value);

[DllImport("__Internal")]
extern static string web3auth_keystore_get(string key);

[DllImport("__Internal")]
extern static int web3auth_keystore_delete(string key);
#endif

public static string SESSION_ID = "sessionId";
public static string IV_KEY = "ivKey";
Expand All @@ -27,26 +38,35 @@ public static string getPubKey(string sessionId)

static KeyStoreManagerUtils()
{
#if !UNITY_IOS
SecurePlayerPrefs.Init();
#endif
}

public static void savePreferenceData(string key, string value)
{
#if UNITY_IOS
web3auth_keystore_set(key, value);
#else
SecurePlayerPrefs.SetString(key, value);
#endif
}

public static string getPreferencesData(string key)
{
#if UNITY_IOS
return web3auth_keystore_get(key);
#else
return SecurePlayerPrefs.GetString(key);
#endif
}
public static void deletePreferencesData(string key)
{
#if UNITY_IOS
web3auth_keystore_delete(key);
#else
SecurePlayerPrefs.DeleteKey(key);
}

public static void clearPreferencesData()
{
SecurePlayerPrefs.DeleteAll();
#endif
}

public static string getECDSASignature(string privateKey, string data){
Expand Down
13 changes: 12 additions & 1 deletion Assets/Plugins/Web3AuthSDK/Web3Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,18 @@ private void sessionTimeOutAPI()

if (result != null)
{
KeyStoreManagerUtils.clearPreferencesData();
try
{
KeyStoreManagerUtils.deletePreferencesData(KeyStoreManagerUtils.EPHEM_PUBLIC_Key);
KeyStoreManagerUtils.deletePreferencesData(KeyStoreManagerUtils.IV_KEY);
KeyStoreManagerUtils.deletePreferencesData(KeyStoreManagerUtils.MAC);
KeyStoreManagerUtils.deletePreferencesData(KeyStoreManagerUtils.SESSION_ID);
KeyStoreManagerUtils.deletePreferencesData(web3AuthOptions.loginConfig?.Values.First()?.verifier);
}
catch (Exception ex)
{
Debug.LogError(ex.Message);
}
}
}
}
Expand Down
65 changes: 65 additions & 0 deletions Assets/Plugins/Web3AuthSDK/iOS/WebAuthenticate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,74 @@
#import <Foundation/Foundation.h>
#import <AuthenticationServices/ASWebAuthenticationSession.h>
#import <UnityFramework/UnityFramework-Swift.h>
#import <Security/Security.h>

extern "C" {
void web3auth_launch(const char *url, const char *redirectUri, const char *objectName) {
[WebAuthenticate launch:[NSString stringWithUTF8String:url] :[NSString stringWithUTF8String:redirectUri] :[NSString stringWithUTF8String:objectName]];
}

int web3auth_keystore_set(const char* dataType, const char* value) {
NSMutableDictionary* attributes = nil;
NSMutableDictionary* query = [NSMutableDictionary dictionary];
NSData* sata = [[NSString stringWithCString:value encoding:NSUTF8StringEncoding] dataUsingEncoding:NSUTF8StringEncoding];

[query setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[query setObject:(id)[NSString stringWithCString:dataType encoding:NSUTF8StringEncoding] forKey:(id)kSecAttrAccount];

OSStatus err = SecItemCopyMatching((CFDictionaryRef)query, NULL);

if (err == noErr) {
attributes = [NSMutableDictionary dictionary];
[attributes setObject:sata forKey:(id)kSecValueData];
[attributes setObject:[NSDate date] forKey:(id)kSecAttrModificationDate];

err = SecItemUpdate((CFDictionaryRef)query, (CFDictionaryRef)attributes);
return (int)err;
} else if (err == errSecItemNotFound) {
attributes = [NSMutableDictionary dictionary];
[attributes setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[attributes setObject:(id)[NSString stringWithCString:dataType encoding:NSUTF8StringEncoding] forKey:(id)kSecAttrAccount];
[attributes setObject:sata forKey:(id)kSecValueData];
[attributes setObject:[NSDate date] forKey:(id)kSecAttrCreationDate];
[attributes setObject:[NSDate date] forKey:(id)kSecAttrModificationDate];
err = SecItemAdd((CFDictionaryRef)attributes, NULL);
return (int)err;
} else {
return (int)err;
}
}

char* web3auth_keystore_get(const char* dataType) {
NSMutableDictionary* query = [NSMutableDictionary dictionary];
[query setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[query setObject:(id)[NSString stringWithCString:dataType encoding:NSUTF8StringEncoding] forKey:(id)kSecAttrAccount];
[query setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];

CFDataRef cfresult = NULL;
OSStatus err = SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef*)&cfresult);

if (err == noErr) {
NSData* passwordData = (__bridge_transfer NSData *)cfresult;
const char* value = [[[NSString alloc] initWithData:passwordData encoding:NSUTF8StringEncoding] UTF8String];
char *str = strdup(value);
return str;
} else {
return NULL;
}
}

int web3auth_keystore_delete(const char* dataType) {
NSMutableDictionary* query = [NSMutableDictionary dictionary];
[query setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[query setObject:(id)[NSString stringWithCString:dataType encoding:NSUTF8StringEncoding] forKey:(id)kSecAttrAccount];

OSStatus err = SecItemDelete((CFDictionaryRef)query);

if (err == noErr) {
return 0;
} else {
return (int)err;
}
}
}
8 changes: 0 additions & 8 deletions Assets/StreamingAssets.meta

This file was deleted.

8 changes: 8 additions & 0 deletions ProjectSettings/GvhProjectSettings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<projectSettings>
<projectSetting name="com.google.external-dependency-managerAnalyticsCookie" value="d216e42890f745b0936cd4fa63f52507" />
<projectSetting name="com.google.external-dependency-managerAnalyticsEnabled" value="True" />
<projectSetting name="Google.IOSResolver.VerboseLoggingEnabled" value="False" />
<projectSetting name="Google.PackageManagerResolver.VerboseLoggingEnabled" value="False" />
<projectSetting name="Google.VersionHandler.VerboseLoggingEnabled" value="False" />
</projectSettings>

0 comments on commit 8ddcf2a

Please sign in to comment.