davidstump / SwiftPhoenixClient

Connect your Phoenix and iOS applications through WebSockets!
MIT License
506 stars 146 forks source link

Adding a certificate authentication request #251

Open owenhartnett opened 7 months ago

owenhartnett commented 7 months ago

I'm trying to hit a server which sits in the dmz and asks for a certificate. It sends out a challenge in the initial http request. I supply the certificate for the NSURLSession. The code I use for AFNetworking on my REST calls to send the cert is here:

 [manager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession * _Nonnull session, NSURLAuthenticationChallenge * _Nonnull challenge, NSURLCredential *__autoreleasing  _Nullable * _Nullable credential) {
        id sender = [challenge sender];
        const void *certArray[] = { certificate };
        CFArrayRef myCerts = CFArrayCreate(NULL, (void *) certArray, 1, NULL);     
        *credential = [NSURLCredential credentialWithIdentity: ident certificates: (__bridge NSArray *) myCerts persistence: NSURLCredentialPersistencePermanent];
        [sender useCredential:*credential forAuthenticationChallenge:challenge];
        CFRelease(myCerts);
        return NSURLSessionAuthChallengeUseCredential;
    }];

Is it possible to send this through the SwiftPhoenix client and, if so, where is the point where I could insert it? I know websocket makes an initial http call to connect, and I'm looking for that particular point in the code (I'm assuming it's using a NSURLSession to connect!)

I don't think the above is certificate pinning, but I could be wrong.

Thanks for any help.

dsrees commented 7 months ago

The URLSession used to create the websocket task is found here. I'm not sure exactly what API you would need to accomplish what you're trying to do but you can extend the URLSessionTransport class, override the connect method, and provide your own implementation that should work for you. You can then provide your custom implementation of Transport when you create the `Socket

let socket = Socket("example.com", { url -> MyCustomTransport(url) }