Permalink
Browse files

added a somewhat useful auth check

  • Loading branch information...
1 parent abd79e5 commit 5d03d5b235aa444fd219930b29476fef2e79a3c1 @sschroed committed Jan 27, 2009
Showing with 42 additions and 7 deletions.
  1. +4 −3 Classes/Connector.h
  2. +28 −0 Classes/Connector.m
  3. +7 −2 Classes/LoginViewController.m
  4. +2 −2 Classes/git_phoneAppDelegate.m
  5. +1 −0 git_phone_Prefix.pch
View
@@ -11,11 +11,12 @@
@interface Connector : NSObject
++ (BOOL)didAuthenticateUser:(NSString *)user withToken:(NSString *)token;
++ (NSString *)getJSONFromURL:(NSString *)url; // issue a GET request
++ (NSString *)getJSONFromURL:(NSString *)url withDictionary:(NSDictionary *)dictionary; // issues a POST request
+ (NSMutableURLRequest *)jsonURLRequest:(NSString *)url;
+ (NSMutableURLRequest *)jsonURLRequest:(NSString *)url withDictionary:(NSDictionary *)dictionary;
-
-+ (NSString *) getJSONFromURL:(NSString *)url; // issue a GET request
-+ (NSString *) getJSONFromURL:(NSString *)url withDictionary:(NSDictionary *)dictionary; // issues a POST request
+ (NSString *)postToURL:(NSString *)url;
+
@end
View
@@ -112,4 +112,32 @@ + (NSString *)postToURL:(NSString *)url {
return resp;
}
+// Make sure username & token authenticate
+// TODO: Refactor this call
++ (BOOL)didAuthenticateUser:(NSString *)user withToken:(NSString *)token {
+ NSString *post = [NSString stringWithFormat:@"login=%@&token=%@", user, token];
+ NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
+ NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
+ NSMutableURLRequest *postRequest = [[[NSMutableURLRequest alloc] init] autorelease];
+ [postRequest setURL:[NSURL URLWithString:@"http://github.com"]];
+ [postRequest setHTTPMethod:@"POST"];
+ [postRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
+ [postRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
+ [postRequest setHTTPBody:postData];
+
+ NSData *urlData;
+ NSURLResponse *response;
+ NSError *error = nil;
+
+ urlData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
+
+ if (error == nil) {
+ DevLog(@"Authenticated");
+ return YES;
+ } else {
+ DevLog(@"Not Authenticated");
+ return NO;
+ }
+}
+
@end
@@ -51,8 +51,13 @@ - (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField == apiToken) {
[[Config instance] setGitHubUserName:[userName text]];
[[Config instance] setGitHubToken:[apiToken text]];
- [Repository loadAll];
- [self dismissModalViewControllerAnimated:YES];
+ if (![Connector didAuthenticateUser:[[Config instance] gitHubUserName] withToken:[[Config instance] gitHubToken]]) {
+ [self showAlert:@"Unable to authenticate using the credentials you provided." withTitle:@"Octocat FAIL"];
+ [userName becomeFirstResponder];
+ } else {
+ [Repository loadAll];
+ [self dismissModalViewControllerAnimated:YES];
+ }
}
}
@@ -64,8 +64,8 @@ - (void) loadPreferences {
[window addSubview:[loginViewController view]];
[navigationController presentModalViewController:loginViewController animated:YES];
} else {
- //TODO: Autologin
- DevLog(@"NOT NULL");
+ //TODO: use conntector didAuthenticate User here
+ [Repository loadAll];
}
//LOAD MAIN APP
}
View
@@ -8,6 +8,7 @@
#endif
#import <JSON/JSON.h>
+#import "Connector.h"
#import "Models.h"
// -----------------------------------------------------------------------------

0 comments on commit 5d03d5b

Please sign in to comment.