 |
 |
|
 |
08-25-2008, 04:13 AM
|
#1 (permalink)
|
|
New Member
Join Date: Aug 2008
Posts: 5
|
NSURLCredential and HTTPBasic
Hi,
i'm trying to build an application that interact with a Ruby on Rails backend.
Actually i have a problem with authentication.
I've create a controller that populate a table from an xml downloaded file. If you click on edit button you can delete rows and there's a call to the sever to delete the record on the database. Everything works, but if i reactivate the authentication filter on the server i cannot authenticate with the code written below.
Actually the server do a redirect to compile a form with login and password.
The authentication requested is HTTP Basic and i've confurated the server to accept every combination on username and password.
Code:
authenticate_or_request_with_http_basic do |username, password|
true
end
This is my controller code:
Code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString *objectId = [[stories objectAtIndex: storyIndex] objectForKey: @"id"];
NSUInteger index = [objectId intValue];
NSString *preUrl = [NSString stringWithFormat:@"http://localhost:3000/admin/admin_page/destroy/%d", index];
NSLog(@"Destroy story at index: %@", preUrl);
NSURL *url = [NSURL URLWithString: preUrl];
NSURLCredential *userCredentials = [NSURLCredential credentialWithUser:@"macteo"
password:@"password"
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *space = [[NSURLProtectionSpace alloc] initWithHost:@"localhost"
port:3000
protocol:@"http"
realm:nil
authenticationMethod: NSURLAuthenticationMethodDefault];
[[NSURLCredentialStorage sharedCredentialStorage] setCredential:userCredentials
forProtectionSpace:space];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
/* [request setHTTPBody:[[self xml] dataUsingEncoding:NSUTF8StringEncoding]]; */
NSURLResponse * response;
NSError * error;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// We're ignoring all the outputs because we don't really care.
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
This is the Ruby on Rails serve log
Code:
Processing AdminPageController#destroy (for 127.0.0.1 at 2008-08-24 12:50:47) [post]
Session ID: BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo
SGFzaHsABjoKQHVzZWR7AA==--94d9d598af90877c999f52867ccabb58fd6b0c74
Parameters: {"action"=>"destroy", "id"=>"8", "controller"=>"admin_page"}
Filter chain halted as [:http_basic_authentication] rendered_or_redirected.
Completed in 0.00092 (1090 reqs/sec) | Rendering: 0.00063 (68%) | DB: 0.00000 (0%) | 401 Unauthorized [http://localhost/admin/admin_page/destroy/8]
Thanks,
MacTeo
|
|
|
03-11-2009, 02:31 AM
|
#2 (permalink)
|
|
New Member
Join Date: Mar 2009
Posts: 1
|
Iphone native app integration with Ruby on Rails backend
Hi,
I'm trying to build an iphone app that works with my ruby on rails web server. I've https based authentication working from web browser.
Using NSDownloadURL , I'm trying to get some data from the server, however the call fails because the credentials have not been provided. Can you please me some pointers/examples of changes on the Rails app as well as on iphone to authenticate with server and get the data
Thanks a lot
Navneet
Quote:
Originally Posted by macteo
Hi,
i'm trying to build an application that interact with a Ruby on Rails backend.
Actually i have a problem with authentication.
I've create a controller that populate a table from an xml downloaded file. If you click on edit button you can delete rows and there's a call to the sever to delete the record on the database. Everything works, but if i reactivate the authentication filter on the server i cannot authenticate with the code written below.
Actually the server do a redirect to compile a form with login and password.
The authentication requested is HTTP Basic and i've confurated the server to accept every combination on username and password.
Code:
authenticate_or_request_with_http_basic do |username, password|
true
end
This is my controller code:
Code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString *objectId = [[stories objectAtIndex: storyIndex] objectForKey: @"id"];
NSUInteger index = [objectId intValue];
NSString *preUrl = [NSString stringWithFormat:@"http://localhost:3000/admin/admin_page/destroy/%d", index];
NSLog(@"Destroy story at index: %@", preUrl);
NSURL *url = [NSURL URLWithString: preUrl];
NSURLCredential *userCredentials = [NSURLCredential credentialWithUser:@"macteo"
password:@"password"
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *space = [[NSURLProtectionSpace alloc] initWithHost:@"localhost"
port:3000
protocol:@"http"
realm:nil
authenticationMethod: NSURLAuthenticationMethodDefault];
[[NSURLCredentialStorage sharedCredentialStorage] setCredential:userCredentials
forProtectionSpace:space];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
/* [request setHTTPBody:[[self xml] dataUsingEncoding:NSUTF8StringEncoding]]; */
NSURLResponse * response;
NSError * error;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// We're ignoring all the outputs because we don't really care.
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
This is the Ruby on Rails serve log
Code:
Processing AdminPageController#destroy (for 127.0.0.1 at 2008-08-24 12:50:47) [post]
Session ID: BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo
SGFzaHsABjoKQHVzZWR7AA==--94d9d598af90877c999f52867ccabb58fd6b0c74
Parameters: {"action"=>"destroy", "id"=>"8", "controller"=>"admin_page"}
Filter chain halted as [:http_basic_authentication] rendered_or_redirected.
Completed in 0.00092 (1090 reqs/sec) | Rendering: 0.00063 (68%) | DB: 0.00000 (0%) | 401 Unauthorized [http://localhost/admin/admin_page/destroy/8]
Thanks,
MacTeo
|
|
|
|
10-26-2009, 10:37 PM
|
#3 (permalink)
|
|
Registered Member
Join Date: Dec 2008
Location: India
Posts: 136
|
Hi navneetaron,
I am also trying to make an iphone app for ruby on rails backend website. Could you please help me out how you managed to save the user data in session. That is, I want user to remain logged-in throughout the session. How could it be done?
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 534 |
| 32 members and 502 guests |
| a123b, ashishjraval, blum, DonomaGames, drhansen, Duncan C, Error404, foxykirian, hfmn, Hololont, houssem, ideafactory, iGeorG, LemonMeringue, Lokendra, Maechi, marklusted, momolgtm, myPhone, rawjam, richyclements, sheonlee, shilpa85, SirGeraint, sohel62, telo3, tiderip, umarmara, xiii3, YoseMite, _nivek, _sean |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 24,350
Threads: 39,144
Posts: 171,614
Top Poster: smasher (2,577)
|
| Welcome to our newest member, 17make |
|