Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Apple NSNotificationCenter
- Loading branch information
1 parent
f1dd8ad
commit 296d954
Showing
4 changed files
with
108 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef _SSH_AGENT_NOTIFY_H_ | ||
#define _SSH_AGENT_NOTIFY_H_ | ||
|
||
void notify_user_macos(char *key, char *comment); | ||
|
||
#endif /* _SSH_AGENT_NOTIFY_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#import <Foundation/Foundation.h> | ||
#import <Foundation/NSUserNotification.h> | ||
#import <objc/runtime.h> | ||
#import "ssh-agent-notify.h" | ||
|
||
@implementation NSBundle(sshagent) | ||
- (NSString *)__bundleIdentifier | ||
{ | ||
return (self == [NSBundle mainBundle] ? @"com.apple.keychainaccess" : | ||
[self __bundleIdentifier]); | ||
} | ||
@end | ||
|
||
BOOL | ||
installNSBundleHook() | ||
{ | ||
Class class = objc_getClass("NSBundle"); | ||
if (class) { | ||
method_exchangeImplementations( | ||
class_getInstanceMethod(class, @selector(bundleIdentifier)), | ||
class_getInstanceMethod(class, @selector(__bundleIdentifier)) | ||
); | ||
return YES; | ||
} | ||
return NO; | ||
} | ||
|
||
#pragma mark - NotificationCenterDelegate | ||
|
||
@interface NotificationCenterDelegate:NSObject<NSUserNotificationCenterDelegate> | ||
@property (nonatomic, assign) BOOL keepRunning; | ||
@end | ||
|
||
@implementation NotificationCenterDelegate | ||
- (void)userNotificationCenter:(NSUserNotificationCenter *)center | ||
didDeliverNotification:(NSUserNotification *)notification | ||
{ | ||
self.keepRunning = NO; | ||
} | ||
@end | ||
|
||
#pragma mark - | ||
|
||
void | ||
notify_user_macos(char *key, char *comment) | ||
{ | ||
@autoreleasepool | ||
{ | ||
if (!installNSBundleHook()) return; | ||
|
||
NSUserNotificationCenter *center = | ||
[NSUserNotificationCenter defaultUserNotificationCenter]; | ||
NotificationCenterDelegate *ncDelegate = | ||
[[NotificationCenterDelegate alloc] init]; | ||
ncDelegate.keepRunning = YES; | ||
center.delegate = ncDelegate; | ||
|
||
NSUserNotification *notification = | ||
[[NSUserNotification alloc] init]; | ||
[notification setTitle: | ||
@"Key challenge signed for fingerprint"]; | ||
[notification setSubtitle: | ||
[NSString stringWithUTF8String:key]]; | ||
[notification setInformativeText: | ||
[NSString stringWithUTF8String:comment]]; | ||
[notification setSoundName:@"Submarine"]; | ||
[center scheduleNotification:notification]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters