How https request is handling using NSURLConnection - ios

Can anybody help me to know how https request are processing using NSRULConnection? I had gone through lot of tutorials and Apple documentation. But I am not able to understand how it is working. I have implemented the following delegates to process an https request.
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
When I implemented the above delegate, I got response from the server successfully. Can anyone help me to know how this is working. Also what are each parameters in the delegate and what it is doing?
Thanks in advance.

Think of the protectionSpace as the server. The delegate method connection: canAuthenticateAgainstProtectionSpace is used to ask you if you can handle the authentication requirements of the server. In your case, you say "if we're talking about the SSL certificate (that's what NSURLAuthenticationMethodServerTrust usually means), yes, I can handle that".
The connection then asks you to do just that with connection:didReceiveAuthenticationChallenge and provide a NSURLCredential for this specific server. With credentialForTrust: you create the credential by using the information stored in your keychain for the certificate of this server. With useCredential:forAuthenticationChallenge: you finally tell the connection to answer the challenge with this credential, i.e. use the keychain data to validate the certificate.

This example will help you How To Use iOS NSURLConnection By Example

Related

How to get domain from a NSURLAuthenticationChallenge instance

The self-signed certificate of https is used in my project. The following methods will be triggered during the request.
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
How do I get valid information about the certificate fromNSURLAuthenticationChallenge, at least I need to get the domain name in the certificate.
First of all, the client does not store any local certificates; in the process I found that the challenge.protectionSpace.host gets the requested host instead of the domain name in the certificate.
Hope to get an answer. Thank you!

How to implement SSL in client side?

I am new to web development and currently I am developing an Rest API which is to be consumed by an IOS app. So I developed the API and also implemented jwt token with oauth2 security in it.Now I want to provide the API to be consumed by the mobile app.So my backend server has SSL certificate. So the consumed Rest API will be something like
https://server:port/dataapiurl
So far I have read about SSL and JWT and i already they are for different reasons where
SSL is used for encrypted channel between client server communication and
JWT is used for Authorization.
So there will be no point if even I implement JWT and the communication is not in SSL.So to make sure the communication is done between client and server what have to be done on the client (mobile app) side?
1.Does the mobile app need to install a new certificate Or the SSL certificate of our backend server?
2.If it is our backend server's SSL certificate then how to install it in the mobile app ?
Any Help is appreciated.
You you can but haven't to set your ssl cert on the client.
You can just conform to the NSURLSessionDelegate protocol and implement this :
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler{
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
NSArray* netTrusts = #["your hostname here"];
if(netTrusts != nil && [netTrusts containsObject:challenge.protectionSpace.host]){
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
}
}
}

HTTPS + Form Logon NSURLConnection

I am using HTTPS to a form-logon page.
When intercepting via
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge`
and extracting the Authentication Method used via
NSString *authenticationMethod = [[challenge protectionSpace] authenticationMethod];
I get the following
NSURLAuthenticationMethodServerTrust
But expected result should be
NSURLAuthenticationMethodHTMLForm
Is this due to using HTTPS?
Short answer: Yes
The purpose of the NSURLAuthenticationMethodServerTrust authentication method is that the client can verify and trust that the server is actual the server it pretends to be.
The NSURLAuthenticationMethodHTMLFormis used to authenticate a user via a web form. The server sends a web form and requests user credentials. This authentication does not require to be send over SSL/TLS. But then the user's credentials will be send in the clear, which is a bad thing from a security point of view.
Client authentication is also part of the TLS protocol. In this case, you may receive a challenge whose method is NSURLAuthenticationMethodClientCertificate.
Notice, you may receive more than one authentication challenges.

iPhone - send a certificate in a request

Now, I'm planing to send a NSURLRequest to the server with a local certificate for server to validate. But I check the function in NSURLRequest, I cannot find a way to include the certificate. Does anyone know how to include a certificate in a NSUrlRequest? Or must I use a open source function, like ASIHttpRequest.
Try to give a look to this quite good tutorial
Authenticating an HTTP Request with Client Certificates in iOS
It explains how to use the NSURLConnectionDelegate method
- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
In order to provide the certificate.
Let me know if that helped.
UPDATE
In case support just for iOS >= 5.0 is provided, it's better to adopt the new non deprecated method
connection:willSendRequestForAuthenticationChallenge

Authenticate iOS App with Internal Application Credentials NOT User Credentials

Is it possible for me to authenticate an iOS App without user interaction to the level where it can make Facebook requests for Page data?
For example, in an app for a musician, I would like to be able to make facebook requests for the musician's artist page including wall posts. I could then get the raw data for their page and style it however I please. This wouldn't require a user to log in and session authentication would be done asynchronously by the app itself, using embedded credentials.
I'd like to use the SDK but am thinking this would require manual OAuth Access Token requests and posts.
Thanks for the help!
UPDATE:
To clarify, I am curious about the possibility of the following:
1) App loads and makes a request for an OAuth Access Token using credentials baked into the App
2) App can then make requests to facebook for feed data from a predetermined page feed
3) None of this requires any user interaction or bounces the application to mobile safari, etc.
I dont really understand what you want, but it is possible to authenticate without user interaction:
If the request requires authentication in order to make the
connection, valid credentials must already be available in the
NSURLCredentialStorage, or must be provided as part of the requested
URL. If the credentials are not available or fail to authenticate, the
URL loading system responds by sending the NSURLProtocol subclass
handling the connection a
continueWithoutCredentialForAuthenticationChallenge: message.
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/AuthenticationChallenges.html#//apple_ref/doc/uid/TP40009507-SW1
There is a way for authentication:
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
Ok - I figured this out. Feel pretty silly that I didn't know you could do this.
You can request an access token for an app id & secret. This will allow you to make public data requests that require an access token.
TO REQUEST ACCESS TOKEN:
https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=11111111&client_secret=9999999999
Then, simply use the returned Access Token in your Feed Request:
https://graph.facebook.com/musicpage/feed?access_token=ACCESS_TOKEN
If this is against TOS or deprecated - please let me know. For now this seems like the solution!

Resources