Skip to content

Commit

Permalink
tests with keychain
Browse files Browse the repository at this point in the history
  • Loading branch information
phuesler committed Nov 25, 2009
1 parent 5a38edd commit 6aceba4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 34 deletions.
5 changes: 3 additions & 2 deletions HallenprojektStatusAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import <Cocoa/Cocoa.h>
#import "PreferencesController.h";

@interface HallenprojektStatusAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
Expand All @@ -15,12 +16,12 @@
NSImage *statusImage;
NSImage *statusAltImage;
NSMenuItem *placesMenuItem;
NSObject *preferencesController;
PreferencesController *preferencesController;
}

@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSMenu *sbMenu;
@property (assign) IBOutlet NSMenuItem *placesMenuItem;
@property (assign) IBOutlet NSObject *preferencesController;
@property (assign) IBOutlet PreferencesController *preferencesController;

@end
34 changes: 14 additions & 20 deletions HallenprojektStatusAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,37 +56,31 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[self fetchPlaces];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
NSString *responseString = [request responseString];
NSLog(@"%@", responseString);
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"%@",[error localizedDescription]);
NSLog(@"handle errors found in error object");
}

- (void)setLocation:(NSString *) place_id {
- (NSError *)setLocation:(NSString *) place_id {
NSURL *url = [NSURL URLWithString:@"http://localhost:3000/set_current_place"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:place_id forKey:@"current_place_id"];
[request setPostValue:@"json" forKey:@"format"];
[request setUseKeychainPersistance:YES];
[request setUsername:@"joe"];
[request setPassword:@"testtest"];
[request setUsername:[preferencesController getUsername]];
[request setPassword:[preferencesController getPassword]];
[request addRequestHeader:@"Accept" value:@"application/json"];
[request setDelegate:self];
[request start];
[request start];
return [request error];
}

- (void)selectedItem:(id) sender {
NSMenuItem *item = (NSMenuItem *) sender;
NSLog(@"%d", [item tag]);
[self setLocation: [NSString stringWithFormat:@"%d", [item tag]]];
[item setTitle: [[item title] stringByAppendingString:@" (logging in)"]];
NSError *error = [self setLocation: [NSString stringWithFormat:@"%d", [item tag]]];
if(error){
NSLog(@"something went wrong");
NSLog(@"%@",[error localizedDescription]);
[preferencesController loadPreferences:self];
}else {
[item setTitle: [[item title] stringByAppendingString:@" (logged in)"]];
}

}

- (IBAction) listPlaces: (id) sender {
Expand Down
4 changes: 4 additions & 0 deletions PreferencesController.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
NSPanel *panel;
NSSecureTextField *passwordTextField;
NSTextField *usernameTextField;
NSString * username;
NSString * password;
}

@property(assign) IBOutlet NSPanel *panel;
@property(assign) IBOutlet NSSecureTextField *passwordTextField;
@property(assign) IBOutlet NSTextField *usernameTextField;
-(IBAction) saveCredentials: (id) sender;
-(IBAction) loadPreferences: (id) sender;
-(NSString *) getUsername;
-(NSString *) getPassword;

@end
43 changes: 31 additions & 12 deletions PreferencesController.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,53 @@
@implementation PreferencesController
@synthesize panel, passwordTextField, usernameTextField;

- (NSString *) getUsername{
return @"test";
}

- (NSString *) getPassword{
return @"testtest";
}

-(IBAction) saveCredentials: (id) sender {
OSStatus status = 0;
NSString *serviceName = @"hallenprojekt.de status app";
NSString *accountName = [usernameTextField stringValue];
NSString *password = [passwordTextField stringValue];
SecKeychainRef keychain = nil;
SecKeychainItemRef item = nil;

status = SecKeychainFindGenericPassword( keychain,
[serviceName lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
[serviceName UTF8String],
[accountName lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
[accountName UTF8String], NULL, NULL, &item);
NSLog(@"SecKeychainFindGenericPassword: %d", status);

[[usernameTextField stringValue] lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
[[usernameTextField stringValue] UTF8String], NULL, NULL, &item);
if (status == errSecItemNotFound){
status = SecKeychainAddGenericPassword( keychain,
[serviceName lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
[serviceName UTF8String],
[accountName lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
[accountName UTF8String],
[password lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
[password UTF8String], &item);
[[usernameTextField stringValue] lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
[[usernameTextField stringValue] UTF8String],
[[passwordTextField stringValue] lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
[[passwordTextField stringValue] UTF8String], &item);
NSLog(@"SecKeychainAddGenericPassword: %d", status);

}else {
NSString *newpassword = [[NSString alloc] initWithString:@"newpassword"];
status = SecKeychainItemModifyContent(item, NULL, [newpassword lengthOfBytesUsingEncoding:NSUTF8StringEncoding], [newpassword UTF8String]);
status = SecKeychainItemModifyContent(item, NULL, [[passwordTextField stringValue] lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
[[passwordTextField stringValue] UTF8String]);
SecKeychainAttribute attr;
SecKeychainAttributeList attrList;

// The attribute we want is the account name
attr.tag = kSecAccountItemAttr;
attr.length = strlen(username);
attr.data = (void*)username;

attrList.count = 1;
attrList.attr = &attr;

// Want to modify so that Keychain entry metadata is preserved.
SecKeychainItemModifyContent(itemRef, &attrList, strlen(password), (void *)password);
CFRelease(itemRef);

NSLog(@"SecKeychainItemModifyContent: %d", status); // wrPermErr = -61, /*write permissions status*/
}

Expand Down

0 comments on commit 6aceba4

Please sign in to comment.