Skip to content

Instantly share code, notes, and snippets.

@adi-li
Created July 11, 2016 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adi-li/7a7f82c98199ce303805dd82d59fcd49 to your computer and use it in GitHub Desktop.
Save adi-li/7a7f82c98199ce303805dd82d59fcd49 to your computer and use it in GitHub Desktop.
Parse allow invalid certificate in iOS
// ParseClientConfiguration.h
@protocol ParseMutableClientConfiguration <NSObject>
@property (nonatomic, assign) BOOL allowInvalidCertificate;
@end
@interface ParseClientConfiguration : NSObject <NSCopying>
@property (nonatomic, assign, readonly) BOOL allowInvalidCertificate;
@end
// ParseClientConfiguration.m
@implementation ParseClientConfiguration
- (void)setAllowInvalidCertificate:(BOOL)allowInvalidCertificate
{
_allowInvalidCertificate = allowInvalidCertificate;
}
- (BOOL)isEqual:(id)object {
if (![object isKindOfClass:[ParseClientConfiguration class]]) {
return NO;
}
ParseClientConfiguration *other = object;
return ([PFObjectUtilities isObject:self.applicationId equalToObject:other.applicationId] &&
[PFObjectUtilities isObject:self.clientKey equalToObject:other.clientKey] &&
[self.server isEqualToString:other.server] &&
self.localDatastoreEnabled == other.localDatastoreEnabled &&
[PFObjectUtilities isObject:self.applicationGroupIdentifier equalToObject:other.applicationGroupIdentifier] &&
[PFObjectUtilities isObject:self.containingApplicationBundleIdentifier equalToObject:other.containingApplicationBundleIdentifier] &&
self.networkRetryAttempts == other.networkRetryAttempts &&
self.allowInvalidCertificate == other.allowInvalidCertificate);
}
- (instancetype)copyWithZone:(NSZone *)zone {
return [ParseClientConfiguration configurationWithBlock:^(ParseClientConfiguration *configuration) {
// Use direct assignment to skip over all of the assertions that may fail if we're not fully initialized yet.
configuration->_applicationId = [self->_applicationId copy];
configuration->_clientKey = [self->_clientKey copy];
configuration->_server = [self.server copy];
configuration->_localDatastoreEnabled = self->_localDatastoreEnabled;
configuration->_applicationGroupIdentifier = [self->_applicationGroupIdentifier copy];
configuration->_containingApplicationBundleIdentifier = [self->_containingApplicationBundleIdentifier copy];
configuration->_networkRetryAttempts = self->_networkRetryAttempts;
configuration->_allowInvalidCertificate = self->_allowInvalidCertificate;
}];
}
@end
// PFURLSession.m
#import "Parse.h"
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
{
ParseClientConfiguration *config = [Parse currentConfiguration];
if (config.allowInvalidCertificate) {
NSURLCredential *cred = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential, cred);
} else {
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
}
}
@adi-li
Copy link
Author

adi-li commented Jul 11, 2016

TODO:

  • Should check the domain name before allowing invalid certificate.
  • Should not be used in production mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment