chore: Move NCKeyChainController to swift
Assisted-by: ClaudeCode:claude-opus-5.5 Signed-off-by: Marcel Müller <[email protected]>
This commit is contained in:
1 parent
aebdec2c9a
commit
7ea0c64aba
7 files changed
+106
-179
No files matched your search
@@ -742,7 +742,7 @@
|
||||
CurrentUserAbsence.swift,
|
||||
NCAppBranding.m,
|
||||
NCAppBrandingExtensions.swift,
|
||||
NCKeyChainController.m,
|
||||
NCKeyChainController.swift,
|
||||
NCLog.swift,
|
||||
NCUserDefaults.swift,
|
||||
NCUserStatus.swift,
|
||||
@@ -757,7 +757,7 @@
|
||||
CurrentUserAbsence.swift,
|
||||
NCAppBranding.m,
|
||||
NCAppBrandingExtensions.swift,
|
||||
NCKeyChainController.m,
|
||||
NCKeyChainController.swift,
|
||||
NCLog.swift,
|
||||
NCUserDefaults.swift,
|
||||
NCUserStatus.swift,
|
||||
@@ -772,7 +772,7 @@
|
||||
CurrentUserAbsence.swift,
|
||||
NCAppBranding.m,
|
||||
NCAppBrandingExtensions.swift,
|
||||
NCKeyChainController.m,
|
||||
NCKeyChainController.swift,
|
||||
NCLog.swift,
|
||||
NCUserStatus.swift,
|
||||
NCUtils.swift,
|
||||
@@ -786,7 +786,7 @@
|
||||
CurrentUserAbsence.swift,
|
||||
NCAppBranding.m,
|
||||
NCAppBrandingExtensions.swift,
|
||||
NCKeyChainController.m,
|
||||
NCKeyChainController.swift,
|
||||
NCLog.swift,
|
||||
NCUserDefaults.swift,
|
||||
NCUserStatus.swift,
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#import "TalkAccount.h"
|
||||
|
||||
#import "SLKTextViewController.h"
|
||||
#import "NCKeyChainController.h"
|
||||
#import "CCCertificate.h"
|
||||
|
||||
|
||||
|
||||
@@ -35,7 +35,6 @@
|
||||
#import "UIResponder+SLKAdditions.h"
|
||||
|
||||
#import "ShareViewController.h"
|
||||
#import "NCKeyChainController.h"
|
||||
#import "CCCertificate.h"
|
||||
|
||||
#import "ScreenCapturer.h"
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class UICKeyChainStore;
|
||||
|
||||
extern NSString * const kNCNormalPushTokenKey;
|
||||
extern NSString * const kNCPushKitTokenKey;
|
||||
|
||||
@interface NCKeyChainController : NSObject
|
||||
|
||||
@property (nonatomic, copy) UICKeyChainStore *keychain;
|
||||
|
||||
+ (instancetype)sharedInstance;
|
||||
- (void)setToken:(NSString *)token forAccountId:(NSString *)accountId;
|
||||
- (NSString * _Nullable)tokenForAccountId:(NSString *)accountId;
|
||||
- (void)setPushNotificationPublicKey:(NSData *)privateKey forAccountId:(NSString *)accountId;
|
||||
- (NSData * _Nullable)pushNotificationPublicKeyForAccountId:(NSString *)accountId;
|
||||
- (void)setPushNotificationPrivateKey:(NSData *)privateKey forAccountId:(NSString *)accountId;
|
||||
- (NSData * _Nullable)pushNotificationPrivateKeyForAccountId:(NSString *)accountId;
|
||||
- (NSString *)pushTokenSHA512;
|
||||
- (void)logCombinedPushToken;
|
||||
- (NSString * _Nullable)combinedPushToken;
|
||||
- (void)removeAllItems;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -1,139 +0,0 @@
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#import "NCKeyChainController.h"
|
||||
|
||||
#import <CommonCrypto/CommonDigest.h>
|
||||
|
||||
#import "NCAppBranding.h"
|
||||
#import "NextcloudTalk-Swift.h"
|
||||
|
||||
@import UICKeyChainStore;
|
||||
|
||||
@implementation NCKeyChainController
|
||||
|
||||
NSString * const kNCTokenKey = @"ncToken";
|
||||
NSString * const kNCNormalPushTokenKey = @"ncNormalPushToken";
|
||||
NSString * const kNCPushKitTokenKey = @"ncPushKitToken";
|
||||
NSString * const kNCPNPublicKey = @"ncPNPublicKey";
|
||||
NSString * const kNCPNPrivateKey = @"ncPNPrivateKey";
|
||||
|
||||
+ (NCKeyChainController *)sharedInstance
|
||||
{
|
||||
static dispatch_once_t once;
|
||||
static NCKeyChainController *sharedInstance;
|
||||
dispatch_once(&once, ^{
|
||||
sharedInstance = [[self alloc] init];
|
||||
});
|
||||
return sharedInstance;
|
||||
}
|
||||
|
||||
- (id)init
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_keychain = [UICKeyChainStore keyChainStoreWithService:bundleIdentifier accessGroup:groupIdentifier];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setToken:(NSString *)token forAccountId:(NSString *)accountId
|
||||
{
|
||||
[_keychain setString:token forKey:[NSString stringWithFormat:@"%@-%@", kNCTokenKey, accountId]];
|
||||
}
|
||||
|
||||
- (NSString *)tokenForAccountId:(NSString *)accountId
|
||||
{
|
||||
return [_keychain stringForKey:[NSString stringWithFormat:@"%@-%@", kNCTokenKey, accountId]];
|
||||
}
|
||||
|
||||
- (void)setPushNotificationPublicKey:(NSData *)privateKey forAccountId:(NSString *)accountId
|
||||
{
|
||||
[_keychain setData:privateKey forKey:[NSString stringWithFormat:@"%@-%@", kNCPNPublicKey, accountId]];
|
||||
}
|
||||
|
||||
- (NSData *)pushNotificationPublicKeyForAccountId:(NSString *)accountId
|
||||
{
|
||||
return [_keychain dataForKey:[NSString stringWithFormat:@"%@-%@", kNCPNPublicKey, accountId]];
|
||||
}
|
||||
|
||||
- (void)setPushNotificationPrivateKey:(NSData *)privateKey forAccountId:(NSString *)accountId
|
||||
{
|
||||
[_keychain setData:privateKey forKey:[NSString stringWithFormat:@"%@-%@", kNCPNPrivateKey, accountId]];
|
||||
}
|
||||
|
||||
- (NSData *)pushNotificationPrivateKeyForAccountId:(NSString *)accountId
|
||||
{
|
||||
return [_keychain dataForKey:[NSString stringWithFormat:@"%@-%@", kNCPNPrivateKey, accountId]];
|
||||
}
|
||||
|
||||
- (NSString *)pushTokenSHA512
|
||||
{
|
||||
NSString *token = [self combinedPushToken];
|
||||
|
||||
if (!token) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
return [self createSHA512:token];
|
||||
}
|
||||
|
||||
- (void)logCombinedPushToken
|
||||
{
|
||||
NSString *normalPushToken = [_keychain stringForKey:kNCNormalPushTokenKey];
|
||||
NSString *pushKitToken = [_keychain stringForKey:kNCPushKitTokenKey];
|
||||
|
||||
if (normalPushToken && [normalPushToken length] >= 10) {
|
||||
[NCLog log:[NSString stringWithFormat:@"Push notification, normal push token: %@... length %ld", [normalPushToken substringToIndex:10], [normalPushToken length]]];
|
||||
} else {
|
||||
[NCLog log:@"Push notification, normal push token length < 10"];
|
||||
}
|
||||
|
||||
if (pushKitToken && [pushKitToken length] >= 10) {
|
||||
[NCLog log:[NSString stringWithFormat:@"Push notification, pushKit token: %@... length %ld", [pushKitToken substringToIndex:10], [pushKitToken length]]];
|
||||
} else {
|
||||
[NCLog log:@"Push notification, pushKit token length < 10"];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString *)combinedPushToken
|
||||
{
|
||||
NSString *normalPushToken = [_keychain stringForKey:kNCNormalPushTokenKey];
|
||||
NSString *pushKitToken = [_keychain stringForKey:kNCPushKitTokenKey];
|
||||
|
||||
if (!normalPushToken || !pushKitToken) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
if ([NCUtils isiOSAppOnMac]) {
|
||||
// As CallKit is not supported on MacOS, we only supply the
|
||||
// normal push token, to generate local notifications for calls
|
||||
return normalPushToken;
|
||||
}
|
||||
|
||||
return [NSString stringWithFormat:@"%@ %@", normalPushToken, pushKitToken];
|
||||
}
|
||||
|
||||
- (void)removeAllItems
|
||||
{
|
||||
[UICKeyChainStore removeAllItemsForService:bundleIdentifier accessGroup:groupIdentifier];
|
||||
}
|
||||
|
||||
#pragma mark - Utils
|
||||
|
||||
- (NSString *)createSHA512:(NSString *)string
|
||||
{
|
||||
const char *cstr = [string cStringUsingEncoding:NSUTF8StringEncoding];
|
||||
NSData *data = [NSData dataWithBytes:cstr length:string.length];
|
||||
uint8_t digest[CC_SHA512_DIGEST_LENGTH];
|
||||
CC_SHA512(data.bytes, (unsigned int)data.length, digest);
|
||||
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2];
|
||||
|
||||
for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++)
|
||||
[output appendFormat:@"%02x", digest[i]];
|
||||
return output;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,102 @@
|
||||
//
|
||||
// SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CommonCrypto
|
||||
import UICKeyChainStore
|
||||
|
||||
let kNCNormalPushTokenKey = "ncNormalPushToken"
|
||||
let kNCPushKitTokenKey = "ncPushKitToken"
|
||||
|
||||
class NCKeyChainController: NSObject {
|
||||
|
||||
private static let tokenKey = "ncToken"
|
||||
private static let pushNotificationPublicKey = "ncPNPublicKey"
|
||||
private static let pushNotificationPrivateKey = "ncPNPrivateKey"
|
||||
|
||||
private static let shared = NCKeyChainController()
|
||||
|
||||
static func sharedInstance() -> NCKeyChainController {
|
||||
return shared
|
||||
}
|
||||
|
||||
private let keychain = UICKeyChainStore(service: bundleIdentifier, accessGroup: groupIdentifier)
|
||||
|
||||
func setToken(_ token: String, forAccountId accountId: String) {
|
||||
keychain.setString(token, forKey: "\(NCKeyChainController.tokenKey)-\(accountId)")
|
||||
}
|
||||
|
||||
func token(forAccountId accountId: String) -> String? {
|
||||
return keychain.string(forKey: "\(NCKeyChainController.tokenKey)-\(accountId)")
|
||||
}
|
||||
|
||||
func setPushNotificationPublicKey(_ publicKey: Data, forAccountId accountId: String) {
|
||||
keychain.setData(publicKey, forKey: "\(NCKeyChainController.pushNotificationPublicKey)-\(accountId)")
|
||||
}
|
||||
|
||||
func pushNotificationPublicKey(forAccountId accountId: String) -> Data? {
|
||||
return keychain.data(forKey: "\(NCKeyChainController.pushNotificationPublicKey)-\(accountId)")
|
||||
}
|
||||
|
||||
func setPushNotificationPrivateKey(_ privateKey: Data, forAccountId accountId: String) {
|
||||
keychain.setData(privateKey, forKey: "\(NCKeyChainController.pushNotificationPrivateKey)-\(accountId)")
|
||||
}
|
||||
|
||||
func pushNotificationPrivateKey(forAccountId accountId: String) -> Data? {
|
||||
return keychain.data(forKey: "\(NCKeyChainController.pushNotificationPrivateKey)-\(accountId)")
|
||||
}
|
||||
|
||||
// Empty when no push token is stored, which is what the server received before as well
|
||||
func pushTokenSHA512() -> String {
|
||||
guard let token = combinedPushToken() else { return "" }
|
||||
|
||||
return sha512(token)
|
||||
}
|
||||
|
||||
func logCombinedPushToken() {
|
||||
let normalPushToken = keychain.string(forKey: kNCNormalPushTokenKey) ?? ""
|
||||
let pushKitToken = keychain.string(forKey: kNCPushKitTokenKey) ?? ""
|
||||
|
||||
if normalPushToken.count >= 10 {
|
||||
NCLog.log("Push notification, normal push token: \(normalPushToken.prefix(10))... length \(normalPushToken.count)")
|
||||
} else {
|
||||
NCLog.log("Push notification, normal push token length < 10")
|
||||
}
|
||||
|
||||
if pushKitToken.count >= 10 {
|
||||
NCLog.log("Push notification, pushKit token: \(pushKitToken.prefix(10))... length \(pushKitToken.count)")
|
||||
} else {
|
||||
NCLog.log("Push notification, pushKit token length < 10")
|
||||
}
|
||||
}
|
||||
|
||||
func combinedPushToken() -> String? {
|
||||
guard let normalPushToken = keychain.string(forKey: kNCNormalPushTokenKey),
|
||||
let pushKitToken = keychain.string(forKey: kNCPushKitTokenKey)
|
||||
else { return nil }
|
||||
|
||||
if NCUtils.isiOSAppOnMac() {
|
||||
// As CallKit is not supported on MacOS, we only supply the
|
||||
// normal push token, to generate local notifications for calls
|
||||
return normalPushToken
|
||||
}
|
||||
|
||||
return "\(normalPushToken) \(pushKitToken)"
|
||||
}
|
||||
|
||||
func removeAllItems() {
|
||||
UICKeyChainStore.removeAllItems(forService: bundleIdentifier, accessGroup: groupIdentifier)
|
||||
}
|
||||
|
||||
private func sha512(_ string: String) -> String {
|
||||
let data = Data(string.utf8)
|
||||
var digest = [UInt8](repeating: 0, count: Int(CC_SHA512_DIGEST_LENGTH))
|
||||
data.withUnsafeBytes {
|
||||
_ = CC_SHA512($0.baseAddress, CC_LONG(data.count), &digest)
|
||||
}
|
||||
|
||||
return digest.map { String(format: "%02hhx", $0) }.joined()
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,6 @@
|
||||
#import "NCAppBranding.h"
|
||||
#import "TalkAccount.h"
|
||||
#import "NCRoom.h"
|
||||
#import "NCKeyChainController.h"
|
||||
|
||||
#import "NextcloudTalk-Swift.h"
|
||||
|
||||
|
||||
Reference in new issue
Block a user