Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Remove the keychain id stuff from SPDatabaseDocument. It was only pro…
…xying SPConnectionController anyway
  • Loading branch information
dmoagx committed Feb 23, 2018
1 parent 770c189 commit ca864a1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 35 deletions.
2 changes: 2 additions & 0 deletions Source/SPConnectionController.h
Expand Up @@ -208,6 +208,7 @@
@property (readwrite, assign) NSInteger sshKeyLocationEnabled;
@property (readwrite, retain) NSString *sshKeyLocation;
@property (readwrite, retain) NSString *sshPort;
@property (readwrite, copy, nonatomic) NSString *connectionKeychainID;
@property (readwrite, retain) NSString *connectionKeychainItemName;
@property (readwrite, retain) NSString *connectionKeychainItemAccount;
@property (readwrite, retain) NSString *connectionSSHKeychainItemName;
Expand All @@ -222,6 +223,7 @@
@property (readonly, assign) BOOL isEditingConnection;

- (NSString *)keychainPassword;
- (NSString *)keychainPasswordForSSH;

// Connection processes
- (IBAction)initiateConnection:(id)sender;
Expand Down
20 changes: 14 additions & 6 deletions Source/SPConnectionController.m
Expand Up @@ -168,6 +168,7 @@ @implementation SPConnectionController
@synthesize dbDocument;
#endif

@synthesize connectionKeychainID = connectionKeychainID;
@synthesize connectionKeychainItemName;
@synthesize connectionKeychainItemAccount;
@synthesize connectionSSHKeychainItemName;
Expand All @@ -188,6 +189,16 @@ - (NSString *)keychainPassword
return kcPassword;
}

- (NSString *)keychainPasswordForSSH
{
if (![self connectionKeychainItemName]) return nil;

// Otherwise, pull the password from the keychain using the details from this connection
NSString *kcSSHPassword = [keychain getPasswordForName:connectionSSHKeychainItemName account:connectionSSHKeychainItemAccount];

return kcSSHPassword;
}

#pragma mark -
#pragma mark Connection processes

Expand Down Expand Up @@ -749,7 +760,7 @@ - (void)updateFavoriteSelection:(id)sender
#ifndef SP_CODA

// Clear the keychain referral items as appropriate
if (connectionKeychainID) SPClear(connectionKeychainID);
[self setConnectionKeychainID:nil];
if (connectionKeychainItemName) SPClear(connectionKeychainItemName);
if (connectionKeychainItemAccount) SPClear(connectionKeychainItemAccount);
if (connectionSSHKeychainItemName) SPClear(connectionSSHKeychainItemName);
Expand Down Expand Up @@ -816,7 +827,7 @@ - (void)updateFavoriteSelection:(id)sender
}

// Store the selected favorite ID for use with the document on connection
if ([fav objectForKey:SPFavoriteIDKey]) connectionKeychainID = [[[fav objectForKey:SPFavoriteIDKey] stringValue] retain];
if ([fav objectForKey:SPFavoriteIDKey]) [self setConnectionKeychainID:[[fav objectForKey:SPFavoriteIDKey] stringValue]];

// And the same for the SSH password
connectionSSHKeychainItemName = [[keychain nameForSSHForFavoriteName:[fav objectForKey:SPFavoriteNameKey] id:[fav objectForKey:SPFavoriteIDKey]] retain];
Expand Down Expand Up @@ -2384,8 +2395,6 @@ - (void)addConnectionToDocument
for (NSUInteger i = 0; i < [toolbarItems count]; i++) [[toolbarItems objectAtIndex:i] setEnabled:YES];
#endif

if (connectionKeychainID) [dbDocument setKeychainID:connectionKeychainID];

// Pass the connection to the table document, allowing it to set
// up the other classes and the rest of the interface.
[dbDocument setConnection:mySQLConnection];
Expand Down Expand Up @@ -3179,7 +3188,6 @@ - (id)initWithDocument:(SPDatabaseDocument *)document
#endif

// Keychain references
connectionKeychainID = nil;
connectionKeychainItemName = nil;
connectionKeychainItemAccount = nil;
connectionSSHKeychainItemName = nil;
Expand Down Expand Up @@ -3633,7 +3641,7 @@ - (void)dealloc

SPClear(nibObjectsToRelease);

if (connectionKeychainID) SPClear(connectionKeychainID);
[self setConnectionKeychainID:nil];
if (connectionKeychainItemName) SPClear(connectionKeychainItemName);
if (connectionKeychainItemAccount) SPClear(connectionKeychainItemAccount);
if (connectionSSHKeychainItemName) SPClear(connectionSSHKeychainItemName);
Expand Down
3 changes: 0 additions & 3 deletions Source/SPDatabaseDocument.h
Expand Up @@ -262,8 +262,6 @@
NSMutableArray *runningActivitiesArray;
#endif

NSString *keychainID;

#ifndef SP_CODA /* ivars */
NSThread *printThread;

Expand Down Expand Up @@ -342,7 +340,6 @@
#endif
- (void)setConnection:(SPMySQLConnection *)theConnection;
- (SPMySQLConnection *)getConnection;
@property(readwrite, copy, nonatomic) NSString *keychainID;

// Database methods
- (IBAction)setDatabases:(id)sender;
Expand Down
37 changes: 11 additions & 26 deletions Source/SPDatabaseDocument.m
Expand Up @@ -135,7 +135,6 @@ @implementation SPDatabaseDocument
@synthesize processID;
@synthesize instanceId;
@synthesize dbTablesTableView = dbTablesTableView;
@synthesize keychainID = keychainID;

#pragma mark -

Expand Down Expand Up @@ -4659,7 +4658,8 @@ - (NSDictionary *) stateIncludingDetails:(NSDictionary *)detailsToReturn
}
[connection setObject:connectionType forKey:@"type"];

if ([[self keychainID] length]) [connection setObject:[self keychainID] forKey:@"kcid"];
NSString *kcid = [connectionController connectionKeychainID];
if ([kcid length]) [connection setObject:kcid forKey:@"kcid"];
[connection setObject:[self name] forKey:@"name"];
[connection setObject:[self host] forKey:@"host"];
[connection setObject:[self user] forKey:@"user"];
Expand Down Expand Up @@ -4860,9 +4860,10 @@ - (BOOL)setState:(NSDictionary *)stateDetails fromFile:(BOOL)spfBased
[connectionController setSslCACertFileLocation:[connection objectForKey:@"sslCACertFileLocation"]];

// Set the keychain details if available
if ([connection objectForKey:@"kcid"] && [(NSString *)[connection objectForKey:@"kcid"] length]) {
[self setKeychainID:[connection objectForKey:@"kcid"]];
[connectionController setConnectionKeychainItemName:[keychain nameForFavoriteName:[connectionController name] id:[self keychainID]]];
NSString *kcid = (NSString *)[connection objectForKey:@"kcid"];
if ([kcid length]) {
[connectionController setConnectionKeychainID:kcid];
[connectionController setConnectionKeychainItemName:[keychain nameForFavoriteName:[connectionController name] id:kcid]];
[connectionController setConnectionKeychainItemAccount:[keychain accountForUser:[connectionController user] host:[connectionController host] database:[connection objectForKey:@"database"]]];
}

Expand Down Expand Up @@ -4894,8 +4895,8 @@ - (BOOL)setState:(NSDictionary *)stateDetails fromFile:(BOOL)spfBased
if ([connection objectForKey:@"ssh_password"])
[connectionController setSshPassword:[connection objectForKey:@"ssh_password"]];
else {
if ([connection objectForKey:@"kcid"] && [(NSString *)[connection objectForKey:@"kcid"] length]) {
[connectionController setConnectionSSHKeychainItemName:[keychain nameForSSHForFavoriteName:[connectionController name] id:[self keychainID]]];
if ([kcid length]) {
[connectionController setConnectionSSHKeychainItemName:[keychain nameForSSHForFavoriteName:[connectionController name] id:kcid]];
[connectionController setConnectionSSHKeychainItemAccount:[keychain accountForSSHUser:[connectionController sshUser] sshHost:[connectionController sshHost]]];
}
NSString *sshpw = [self keychainPasswordForSSHConnection:nil];
Expand Down Expand Up @@ -7148,25 +7149,10 @@ - (NSString *)keychainPasswordForConnection:(SPMySQLConnection *)connection auth
- (NSString *)keychainPasswordForSSHConnection:(SPMySQLConnection *)connection
{
// If no keychain item is available, return an empty password
if (![connectionController connectionKeychainItemName]) return @"";
NSString *password = [connectionController keychainPasswordForSSH];
if (!password) return @"";

// Otherwise, pull the password from the keychain using the details from this connection
SPKeychain *keychain = [[SPKeychain alloc] init];

NSString *connectionSSHKeychainItemName = [[keychain nameForSSHForFavoriteName:[connectionController name] id:[self keychainID]] retain];
NSString *connectionSSHKeychainItemAccount = [[keychain accountForSSHUser:[connectionController sshUser] sshHost:[connectionController sshHost]] retain];
NSString *sshPassword = [keychain getPasswordForName:connectionSSHKeychainItemName account:connectionSSHKeychainItemAccount];

if (!sshPassword || ![sshPassword length]) {
sshPassword = @"";
}

if (connectionSSHKeychainItemName) [connectionSSHKeychainItemName release];
if (connectionSSHKeychainItemAccount) [connectionSSHKeychainItemAccount release];

[keychain release];

return sshPassword;
return password;
}

/**
Expand Down Expand Up @@ -7729,7 +7715,6 @@ - (void)dealloc
if (spfPreferences) SPClear(spfPreferences);
if (spfSession) SPClear(spfSession);
if (spfDocData) SPClear(spfDocData);
[self setKeychainID:nil];
if (mainToolbar) SPClear(mainToolbar);
if (titleAccessoryView) SPClear(titleAccessoryView);
if (taskProgressWindow) SPClear(taskProgressWindow);
Expand Down

0 comments on commit ca864a1

Please sign in to comment.