mirror of
https://github.com/kennethreitz-archive/gitx.git
synced 2026-06-05 23:40:18 +00:00
new AskPasswd
This commit is contained in:
@@ -66,10 +66,12 @@
|
||||
[[SUUpdater sharedUpdater] setSendsSystemProfile:YES];
|
||||
[[SUUpdater sharedUpdater] setDelegate:self];
|
||||
|
||||
if ([PBGitDefaults useAskPasswd]) {
|
||||
// Make sure Git's SSH password requests get forwarded to our little UI tool:
|
||||
setenv( "SSH_ASKPASS", [[[NSBundle mainBundle] pathForResource: @"gitx_askpasswd" ofType: @""] UTF8String], 1 );
|
||||
setenv( "DISPLAY", "localhost:0", 1 );
|
||||
|
||||
}
|
||||
|
||||
[self registerServices];
|
||||
|
||||
BOOL hasOpenedDocuments = NO;
|
||||
|
||||
+133
-742
File diff suppressed because it is too large
Load Diff
@@ -19,6 +19,7 @@
|
||||
+ (BOOL) isGistPublic;
|
||||
+ (BOOL)showWhitespaceDifferences;
|
||||
+ (BOOL)refreshAutomatically;
|
||||
+ (BOOL) useAskPasswd;
|
||||
+ (BOOL)openCurDirOnLaunch;
|
||||
+ (BOOL)showOpenPanelOnLaunch;
|
||||
+ (BOOL) shouldCheckoutBranch;
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#define kPublicGist @"PBGistPublic"
|
||||
#define kShowWhitespaceDifferences @"PBShowWhitespaceDifferences"
|
||||
#define kRefreshAutomatically @"PBRefreshAutomatically"
|
||||
#define kUseAskPasswd @"PBUseAskPasswd"
|
||||
#define kOpenCurDirOnLaunch @"PBOpenCurDirOnLaunch"
|
||||
#define kShowOpenPanelOnLaunch @"PBShowOpenPanelOnLaunch"
|
||||
#define kShouldCheckoutBranch @"PBShouldCheckoutBranch"
|
||||
@@ -53,6 +54,8 @@
|
||||
forKey:kShowWhitespaceDifferences];
|
||||
[defaultValues setObject:[NSNumber numberWithBool:YES]
|
||||
forKey:kRefreshAutomatically];
|
||||
[defaultValues setObject:[NSNumber numberWithBool:YES]
|
||||
forKey:kUseAskPasswd];
|
||||
[defaultValues setObject:[NSNumber numberWithBool:YES]
|
||||
forKey:kOpenCurDirOnLaunch];
|
||||
[defaultValues setObject:[NSNumber numberWithBool:YES]
|
||||
@@ -110,6 +113,11 @@
|
||||
return [[NSUserDefaults standardUserDefaults] boolForKey:kRefreshAutomatically];
|
||||
}
|
||||
|
||||
+ (BOOL) useAskPasswd
|
||||
{
|
||||
return [[NSUserDefaults standardUserDefaults] boolForKey:kUseAskPasswd];
|
||||
}
|
||||
|
||||
+ (BOOL)openCurDirOnLaunch
|
||||
{
|
||||
return [[NSUserDefaults standardUserDefaults] boolForKey:kOpenCurDirOnLaunch];
|
||||
|
||||
+86
-190
@@ -32,16 +32,8 @@
|
||||
|
||||
@interface GAPAppDelegate : NSObject <NSApplicationDelegate>
|
||||
{
|
||||
NSPanel* mPasswordPanel;
|
||||
NSSecureTextField* mPasswordField;
|
||||
NSButton* rememberCheck;
|
||||
}
|
||||
|
||||
-(NSPanel*)passwordPanel:(NSString *)prompt remember:(BOOL)remember;
|
||||
|
||||
-(IBAction) doOKButton: (id)sender;
|
||||
-(IBAction) doCancelButton: (id)sender;
|
||||
|
||||
@end
|
||||
|
||||
NSString* url;
|
||||
@@ -50,139 +42,59 @@ OSStatus StorePasswordKeychain (const char *url, UInt32 urlLength, void* passw
|
||||
|
||||
@implementation GAPAppDelegate
|
||||
|
||||
-(NSPanel*)passwordPanel:(NSString *)prompt remember:(BOOL)remember
|
||||
{
|
||||
if( !mPasswordPanel )
|
||||
{
|
||||
NSRect box = NSMakeRect( 100, 100, 400, 134 );
|
||||
mPasswordPanel = [[NSPanel alloc] initWithContentRect: box
|
||||
styleMask: NSTitledWindowMask
|
||||
backing: NSBackingStoreBuffered defer: NO];
|
||||
[mPasswordPanel setHidesOnDeactivate: NO];
|
||||
[mPasswordPanel setLevel: NSFloatingWindowLevel];
|
||||
[mPasswordPanel setTitle: @"GitX SSH Remote Login"];
|
||||
if (![mPasswordPanel setFrameUsingName: WINDOWAUTOSAVENAME]) {
|
||||
[mPasswordPanel center];
|
||||
[mPasswordPanel setFrameAutosaveName: WINDOWAUTOSAVENAME];
|
||||
-(void)yesNo:(NSString *)prompt url:(NSString *)url{
|
||||
NSAlert *alert = [[NSAlert alloc] init];
|
||||
[alert addButtonWithTitle:@"YES"];
|
||||
[alert addButtonWithTitle:@"NO"];
|
||||
[alert setMessageText:[NSString stringWithFormat:@"%@?",url]];
|
||||
[alert setInformativeText:prompt];
|
||||
[alert setAlertStyle:NSWarningAlertStyle];
|
||||
NSInteger result = [alert runModal];
|
||||
|
||||
Boolean yes=NO;
|
||||
if ( result == NSAlertFirstButtonReturn ) {
|
||||
yes=YES;
|
||||
}
|
||||
[alert release];
|
||||
printf("%s",yes?"yes":"no");
|
||||
}
|
||||
|
||||
|
||||
- (void)pasword:(NSString *)prompt url:(NSString *)url{
|
||||
|
||||
NSRect box = NSMakeRect(0, 0, 200, 24);
|
||||
|
||||
NSTextField * passView = [[NSTextField alloc] initWithFrame: box];
|
||||
[passView setSelectable: YES];
|
||||
[passView setEditable: YES];
|
||||
[passView setBordered: YES];
|
||||
[passView setBezeled: YES];
|
||||
[passView setBezelStyle: NSTextFieldSquareBezel];
|
||||
[passView selectText: self];
|
||||
|
||||
|
||||
NSAlert *alert = [[NSAlert alloc] init];
|
||||
[alert addButtonWithTitle:@"Ok"];
|
||||
[alert addButtonWithTitle:@"cancel"];
|
||||
[alert setMessageText:[NSString stringWithFormat:@"%@?",url]];
|
||||
[alert setInformativeText:prompt];
|
||||
[alert setAlertStyle:NSWarningAlertStyle];
|
||||
[alert setAccessoryView:passView];
|
||||
[alert setShowsSuppressionButton:YES];
|
||||
[[alert suppressionButton] setTitle:@"Save on keychain"];
|
||||
NSInteger result = [alert runModal];
|
||||
if ( result == NSAlertFirstButtonReturn ) {
|
||||
NSString *pas=[passView stringValue];
|
||||
printf( "%s", [pas UTF8String] );
|
||||
if ([[alert suppressionButton] state] == NSOnState) {
|
||||
StorePasswordKeychain ([url cStringUsingEncoding:NSASCIIStringEncoding],
|
||||
[url lengthOfBytesUsingEncoding:NSASCIIStringEncoding],
|
||||
(void *)[pas cStringUsingEncoding:NSASCIIStringEncoding],
|
||||
[pas lengthOfBytesUsingEncoding:NSASCIIStringEncoding]);
|
||||
}
|
||||
|
||||
box.origin = NSZeroPoint; // Only need local coords from now on.
|
||||
|
||||
// OK:
|
||||
NSRect okBox = box;
|
||||
okBox.origin.x = NSMaxX( box ) -OKBUTTONWIDTH -20;
|
||||
okBox.size.width = OKBUTTONWIDTH;
|
||||
okBox.origin.y += 20;
|
||||
okBox.size.height = OKBUTTONHEIGHT;
|
||||
NSButton *okButton = [[NSButton alloc] initWithFrame: okBox];
|
||||
[okButton setTarget: self];
|
||||
[okButton setAction: @selector(doOKButton:)];
|
||||
[okButton setTitle: @"OK"]; // +++ Localize.
|
||||
[okButton setKeyEquivalent: @"\r"];
|
||||
[okButton setBordered: YES];
|
||||
[okButton setBezelStyle: NSRoundedBezelStyle];
|
||||
[[mPasswordPanel contentView] addSubview: okButton];
|
||||
|
||||
// Cancel:
|
||||
NSRect cancelBox = box;
|
||||
cancelBox.origin.x = NSMinX( okBox ) -CANCELBUTTONWIDTH -6;
|
||||
cancelBox.size.width = CANCELBUTTONWIDTH;
|
||||
cancelBox.origin.y += 20;
|
||||
cancelBox.size.height = CANCELBUTTONHEIGHT;
|
||||
NSButton *cancleButton = [[NSButton alloc] initWithFrame: cancelBox];
|
||||
[cancleButton setTarget: self];
|
||||
[cancleButton setAction: @selector(doCancelButton:)];
|
||||
[cancleButton setTitle: @"Cancel"]; // +++ Localize.
|
||||
[cancleButton setBordered: YES];
|
||||
[cancleButton setBezelStyle: NSRoundedBezelStyle];
|
||||
[[mPasswordPanel contentView] addSubview: cancleButton];
|
||||
|
||||
// Password field:
|
||||
NSRect passBox = box;
|
||||
passBox.origin.y = NSMaxY(okBox) + 24;
|
||||
passBox.size.height = PASSHEIGHT;
|
||||
passBox.origin.x += 104;
|
||||
passBox.size.width -= 104 + 20;
|
||||
mPasswordField = [[NSSecureTextField alloc] initWithFrame: passBox];
|
||||
[mPasswordField setSelectable: YES];
|
||||
[mPasswordField setEditable: YES];
|
||||
[mPasswordField setBordered: YES];
|
||||
[mPasswordField setBezeled: YES];
|
||||
[mPasswordField setBezelStyle: NSTextFieldSquareBezel];
|
||||
[mPasswordField selectText: self];
|
||||
[[mPasswordPanel contentView] addSubview: mPasswordField];
|
||||
|
||||
// Password label:
|
||||
NSRect passLabelBox = box;
|
||||
passLabelBox.origin.y = NSMaxY(passBox) + 8;
|
||||
passLabelBox.size.height = PASSLABELHEIGHT;
|
||||
passLabelBox.origin.x += 100;
|
||||
passLabelBox.size.width -= 100 + 20;
|
||||
NSTextField *passwordLabel = [[NSTextField alloc] initWithFrame: passLabelBox];
|
||||
[passwordLabel setSelectable: YES];
|
||||
[passwordLabel setEditable: NO];
|
||||
[passwordLabel setBordered: NO];
|
||||
[passwordLabel setBezeled: NO];
|
||||
[passwordLabel setDrawsBackground: NO];
|
||||
[passwordLabel setStringValue: prompt];
|
||||
[[mPasswordPanel contentView] addSubview: passwordLabel];
|
||||
|
||||
// remember buton:
|
||||
if(remember){
|
||||
NSRect rememberBox = box;
|
||||
rememberBox.origin.x = 100;
|
||||
rememberBox.size.width = CANCELBUTTONWIDTH;
|
||||
rememberBox.origin.y += 20;
|
||||
rememberBox.size.height = CANCELBUTTONHEIGHT;
|
||||
rememberCheck = [[NSButton alloc] initWithFrame: rememberBox];
|
||||
[rememberCheck setButtonType:NSSwitchButton];
|
||||
[rememberCheck setTarget: self];
|
||||
[rememberCheck setTitle: @"Remenber"]; // +++ Localize.
|
||||
[[mPasswordPanel contentView] addSubview: rememberCheck];
|
||||
}
|
||||
|
||||
// GitX icon:
|
||||
NSRect gitxIconBox = box;
|
||||
gitxIconBox.origin.y = NSMaxY(box) - 78;
|
||||
gitxIconBox.size.height = 64;
|
||||
gitxIconBox.origin.x += 20;
|
||||
gitxIconBox.size.width = 64;
|
||||
NSImageView *gitxIconView = [[NSImageView alloc] initWithFrame: gitxIconBox];
|
||||
[gitxIconView setEditable: NO];
|
||||
NSString *gitxIconPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent: @"gitx.icns"];
|
||||
NSImage *gitxIcon = [[NSImage alloc] initWithContentsOfFile: gitxIconPath];
|
||||
[gitxIconView setImage: gitxIcon];
|
||||
[[mPasswordPanel contentView] addSubview: gitxIconView];
|
||||
}
|
||||
|
||||
return mPasswordPanel;
|
||||
}
|
||||
|
||||
|
||||
-(IBAction) doOKButton: (id)sender
|
||||
{
|
||||
NSString *pas=[mPasswordField stringValue];
|
||||
printf( "%s", [pas UTF8String] );
|
||||
|
||||
if ((rememberCheck!=nil) && [rememberCheck state]==NSOnState) {
|
||||
OSStatus status = StorePasswordKeychain ([url cStringUsingEncoding:NSASCIIStringEncoding],
|
||||
[url lengthOfBytesUsingEncoding:NSASCIIStringEncoding],
|
||||
(void *)[pas cStringUsingEncoding:NSASCIIStringEncoding],
|
||||
[pas lengthOfBytesUsingEncoding:NSASCIIStringEncoding]); //Call
|
||||
if (status != noErr) {
|
||||
[[NSApplication sharedApplication] stopModalWithCode:-1];
|
||||
}
|
||||
}
|
||||
[[NSApplication sharedApplication] stopModalWithCode:0];
|
||||
}
|
||||
|
||||
|
||||
// TODO: Need to find out how to get SSH to cancel.
|
||||
// When the user cancels the window it is opened again for however
|
||||
// many times the remote server allows failed attempts.
|
||||
-(IBAction) doCancelButton: (id)sender
|
||||
{
|
||||
[[NSApplication sharedApplication] stopModalWithCode:-1];
|
||||
}
|
||||
|
||||
[alert release];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -295,61 +207,45 @@ int main( int argc, const char* argv[] )
|
||||
{
|
||||
// close stderr to stop cocoa log messages from being picked up by GitX
|
||||
close(STDERR_FILENO);
|
||||
|
||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||
|
||||
|
||||
ProcessSerialNumber myPSN = { 0, kCurrentProcess };
|
||||
TransformProcessType( &myPSN, kProcessTransformToForegroundApplication );
|
||||
|
||||
NSApplication *app = [NSApplication sharedApplication];
|
||||
GAPAppDelegate *appDel = [[GAPAppDelegate alloc] init];
|
||||
[app setDelegate: appDel];
|
||||
|
||||
|
||||
char args[4024];
|
||||
getproclline(getppid(),args);
|
||||
NSString *cmd=[NSString stringWithFormat:@"%@",[NSString stringWithUTF8String:args]];
|
||||
|
||||
NSString *prompt=@"???";
|
||||
|
||||
url=@"poipoi";
|
||||
|
||||
BOOL remember=NO;
|
||||
|
||||
if([cmd hasPrefix:@"git-remote-https"]){
|
||||
NSArray *args=[cmd componentsSeparatedByString:@" "];
|
||||
url=[args objectAtIndex:[args count]-1];
|
||||
prompt=[NSString stringWithFormat:@"%@ %@",[NSString stringWithCString:argv[1] encoding:NSASCIIStringEncoding],url];
|
||||
remember=YES;
|
||||
}else if((sizeof(argv)/sizeof(char*))>1){
|
||||
prompt=[NSString stringWithCString:argv[1] encoding:NSASCIIStringEncoding];
|
||||
}else{ // only for test
|
||||
remember=YES;
|
||||
prompt=[NSString stringWithFormat:@"%???? %@",url];
|
||||
}
|
||||
|
||||
void *passwordData = nil;
|
||||
SecKeychainItemRef itemRef = nil;
|
||||
UInt32 passwordLength = 0;
|
||||
|
||||
OSStatus status = GetPasswordKeychain ([url cStringUsingEncoding:NSASCIIStringEncoding],[url lengthOfBytesUsingEncoding:NSASCIIStringEncoding],&passwordData,&passwordLength,&itemRef);
|
||||
if (status == noErr) {
|
||||
SecKeychainItemFreeContent (NULL,passwordData);
|
||||
NSString *pas=[[NSString stringWithCString:passwordData encoding:NSASCIIStringEncoding] substringToIndex:passwordLength];
|
||||
printf( "%s", [pas UTF8String] );
|
||||
//DLog(@"--> '%@'",pas);
|
||||
return 0;
|
||||
}else if (status != errSecItemNotFound) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
NSInteger code;
|
||||
|
||||
NSWindow *passPanel = [appDel passwordPanel:prompt remember:remember];
|
||||
[app activateIgnoringOtherApps: YES];
|
||||
[passPanel makeKeyAndOrderFront: nil];
|
||||
code = [app runModalForWindow: passPanel];
|
||||
|
||||
[defaults synchronize];
|
||||
|
||||
return code;
|
||||
char args[4024];
|
||||
getproclline(getppid(),args);
|
||||
NSString *cmd=[NSString stringWithFormat:@"%@",[NSString stringWithUTF8String:args]];
|
||||
|
||||
NSLog(@"cmd: '%@'",cmd);
|
||||
|
||||
if([cmd hasPrefix:@"git-remote"]){
|
||||
NSArray *args=[cmd componentsSeparatedByString:@" "];
|
||||
NSString *url=[args objectAtIndex:[args count]-1];
|
||||
|
||||
void *passwordData = nil;
|
||||
SecKeychainItemRef itemRef = nil;
|
||||
UInt32 passwordLength = 0;
|
||||
|
||||
OSStatus status = GetPasswordKeychain ([url cStringUsingEncoding:NSASCIIStringEncoding],[url lengthOfBytesUsingEncoding:NSASCIIStringEncoding],&passwordData,&passwordLength,&itemRef);
|
||||
if (status == noErr) {
|
||||
SecKeychainItemFreeContent (NULL,passwordData);
|
||||
NSString *pas=[[NSString stringWithCString:passwordData encoding:NSASCIIStringEncoding] substringToIndex:passwordLength];
|
||||
printf( "%s", [pas UTF8String] );
|
||||
return 0;
|
||||
}
|
||||
|
||||
NSString *prompt=[NSString stringWithFormat:@"%@",[NSString stringWithCString:argv[1] encoding:NSASCIIStringEncoding]];
|
||||
[appDel pasword:prompt url:url];
|
||||
}else{ // yes/no?
|
||||
NSString *prompt=[NSString stringWithFormat:@"%@",[NSString stringWithCString:argv[1] encoding:NSASCIIStringEncoding]];
|
||||
NSArray *args=[cmd componentsSeparatedByString:@" "];
|
||||
NSString *url=[args objectAtIndex:1];
|
||||
[appDel yesNo:prompt url:url];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user