Skip to content

Instantly share code, notes, and snippets.

@bob-codingdutchmen
Last active August 29, 2015 14:07
Show Gist options
  • Save bob-codingdutchmen/a36a031415a2f5477ae5 to your computer and use it in GitHub Desktop.
Save bob-codingdutchmen/a36a031415a2f5477ae5 to your computer and use it in GitHub Desktop.
Simple password authentication
-(void)checkPassword {
[[MSAPIController sharedAPIController] authenticateWithPassword:self.passField.text
onComplete:^(BOOL success, NSInteger statusCode) {
if(success) {
[self proceed];
} else {
[self loginFailed];
}
}];
}
-(void)authenticateWithPassword:(NSString*)password
onComplete:(void(^)(BOOL success, NSInteger statusCode))onComplete {
// Setup the session
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = @{ @"Content-Type" : @"application/x-www-form-urlencoded; charset=utf-8" };
// Create the session
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:kMSAuthenticationURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.HTTPBody = [[NSString stringWithFormat:@"pass=%@",password] dataUsingEncoding:NSUTF8StringEncoding];
[[session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if([response isKindOfClass:NSHTTPURLResponse.class]) {
NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse*)response;
if(urlResponse.statusCode == 200) {
onComplete(YES, urlResponse.statusCode);
} else {
onComplete(NO, urlResponse.statusCode);
}
return;
}
onComplete(NO, 0);
[session invalidateAndCancel];
});
}] resume];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment