From b0063d9e9f65085f59bb3e5f182013b38a85da89 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Mon, 14 Sep 2015 18:29:49 +0800 Subject: [PATCH] Objc client ssl verification --- .../resources/objc/ApiClient-body.mustache | 28 ++++++++-- .../resources/objc/ApiClient-header.mustache | 7 +++ .../objc/Configuration-body.mustache | 1 + .../objc/Configuration-header.mustache | 18 +++++-- .../petstore/objc/SwaggerClient.podspec | 5 ++ .../objc/SwaggerClient/SWGApiClient.h | 7 +++ .../objc/SwaggerClient/SWGApiClient.m | 28 ++++++++-- .../objc/SwaggerClient/SWGConfiguration.h | 18 +++++-- .../objc/SwaggerClient/SWGConfiguration.m | 1 + .../petstore/objc/SwaggerClient/SWGPetApi.m | 51 +++++++++++++++---- .../petstore/objc/SwaggerClient/SWGStoreApi.m | 12 ++++- .../petstore/objc/SwaggerClient/SWGUserApi.m | 18 +++++-- .../SwaggerClient/SWGViewController.m | 10 ++++ .../Tests/DeserializationTest.m | 5 +- 14 files changed, 182 insertions(+), 27 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache b/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache index 04c628ee5a3..6690dafc7cc 100644 --- a/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache @@ -28,6 +28,7 @@ static void (^reachabilityChangeBlock)(int); if (self) { self.requestSerializer = [AFJSONRequestSerializer serializer]; self.responseSerializer = [AFJSONResponseSerializer serializer]; + self.securityPolicy = [self customSecurityPolicy]; // configure reachability [self configureCacheReachibility]; } @@ -278,7 +279,7 @@ static void (^reachabilityChangeBlock)(int); // pure object if ([class isEqualToString:@"NSObject"]) { - return [[NSObject alloc] init]; + return data; } // list of models @@ -407,7 +408,7 @@ static void (^reachabilityChangeBlock)(int); if([[{{classPrefix}}Configuration sharedConfig] debug]) [self logResponse:nil forRequest:request error:augmentedError]; - + NSDictionary *responseHeaders = [[operation response] allHeaderFields]; self.HTTPResponseHeaders = responseHeaders; @@ -740,5 +741,26 @@ static void (^reachabilityChangeBlock)(int); @throw e; } } - + +- (AFSecurityPolicy *) customSecurityPolicy { + AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone]; + + {{classPrefix}}Configuration *config = [{{classPrefix}}Configuration sharedConfig]; + + if (config.sslCaCert) { + NSData *certData = [NSData dataWithContentsOfFile:config.sslCaCert]; + [securityPolicy setPinnedCertificates:@[certData]]; + } + + if (config.verifySSL) { + [securityPolicy setAllowInvalidCertificates:NO]; + } + else { + [securityPolicy setAllowInvalidCertificates:YES]; + [securityPolicy setValidatesDomainName:NO]; + } + + return securityPolicy; +} + @end diff --git a/modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache b/modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache index fef1a2b79e7..226033973f5 100644 --- a/modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache @@ -203,4 +203,11 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey; */ - (id) sanitizeForSerialization:(id) object; +/** + * Custom security policy + * + * @return AFSecurityPolicy + */ +- (AFSecurityPolicy *) customSecurityPolicy; + @end diff --git a/modules/swagger-codegen/src/main/resources/objc/Configuration-body.mustache b/modules/swagger-codegen/src/main/resources/objc/Configuration-body.mustache index 9c941b107a0..167b05aef1f 100644 --- a/modules/swagger-codegen/src/main/resources/objc/Configuration-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/Configuration-body.mustache @@ -31,6 +31,7 @@ self.password = @""; self.tempFolderPath = nil; self.debug = NO; + self.verifySSL = YES; self.loggingFile = nil; self.mutableApiKey = [NSMutableDictionary dictionary]; self.mutableApiKeyPrefix = [NSMutableDictionary dictionary]; diff --git a/modules/swagger-codegen/src/main/resources/objc/Configuration-header.mustache b/modules/swagger-codegen/src/main/resources/objc/Configuration-header.mustache index c9bac508bbb..9abe135bc47 100644 --- a/modules/swagger-codegen/src/main/resources/objc/Configuration-header.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/Configuration-header.mustache @@ -1,6 +1,6 @@ #import #import "{{classPrefix}}ApiClient.h" - + /** The `{{classPrefix}}Configuration` class manages the configurations for the sdk. * * NOTE: This class is auto generated by the swagger code generator program. @@ -9,14 +9,14 @@ */ @class {{classPrefix}}ApiClient; - + @interface {{classPrefix}}Configuration : NSObject /** * Default api client */ @property (nonatomic) {{classPrefix}}ApiClient *apiClient; - + /** * Default base url */ @@ -75,6 +75,18 @@ */ + (instancetype) sharedConfig; +/** + * SSL/TLS verification + * Set this to NO to skip verifying SSL certificate when calling API from https server + */ +@property (nonatomic) BOOL verifySSL; + +/** + * SSL/TLS verification + * Set this to customize the certificate file to verify the peer + */ +@property (nonatomic) NSString *sslCaCert; + /** * Sets API key * diff --git a/samples/client/petstore/objc/SwaggerClient.podspec b/samples/client/petstore/objc/SwaggerClient.podspec index d9b02841cf9..0afb511073c 100644 --- a/samples/client/petstore/objc/SwaggerClient.podspec +++ b/samples/client/petstore/objc/SwaggerClient.podspec @@ -20,6 +20,11 @@ Pod::Spec.new do |s| s.requires_arc = true s.framework = 'SystemConfiguration' + + s.homepage = "https://github.com/swagger-api/swagger-codegen" + s.license = "MIT" + s.source = { :git => "https://github.com/swagger-api/swagger-codegen.git", :tag => "#{s.version}" } + s.author = { "Swagger" => "apiteam@swagger.io" } s.source_files = 'SwaggerClient/**/*' s.public_header_files = 'SwaggerClient/**/*.h' diff --git a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h index 580efa739a0..adf59bb25cf 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h +++ b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h @@ -207,4 +207,11 @@ extern NSString *const SWGResponseObjectErrorKey; */ - (id) sanitizeForSerialization:(id) object; +/** + * Custom security policy + * + * @return AFSecurityPolicy + */ +- (AFSecurityPolicy *) customSecurityPolicy; + @end diff --git a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m index 826443453b0..8d640957dec 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m @@ -28,6 +28,7 @@ static void (^reachabilityChangeBlock)(int); if (self) { self.requestSerializer = [AFJSONRequestSerializer serializer]; self.responseSerializer = [AFJSONResponseSerializer serializer]; + self.securityPolicy = [self customSecurityPolicy]; // configure reachability [self configureCacheReachibility]; } @@ -278,7 +279,7 @@ static void (^reachabilityChangeBlock)(int); // pure object if ([class isEqualToString:@"NSObject"]) { - return [[NSObject alloc] init]; + return data; } // list of models @@ -407,7 +408,7 @@ static void (^reachabilityChangeBlock)(int); if([[SWGConfiguration sharedConfig] debug]) [self logResponse:nil forRequest:request error:augmentedError]; - + NSDictionary *responseHeaders = [[operation response] allHeaderFields]; self.HTTPResponseHeaders = responseHeaders; @@ -740,5 +741,26 @@ static void (^reachabilityChangeBlock)(int); @throw e; } } - + +- (AFSecurityPolicy *) customSecurityPolicy { + AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone]; + + SWGConfiguration *config = [SWGConfiguration sharedConfig]; + + if (config.sslCaCert) { + NSData *certData = [NSData dataWithContentsOfFile:config.sslCaCert]; + [securityPolicy setPinnedCertificates:@[certData]]; + } + + if (config.verifySSL) { + [securityPolicy setAllowInvalidCertificates:NO]; + } + else { + [securityPolicy setAllowInvalidCertificates:YES]; + [securityPolicy setValidatesDomainName:NO]; + } + + return securityPolicy; +} + @end diff --git a/samples/client/petstore/objc/SwaggerClient/SWGConfiguration.h b/samples/client/petstore/objc/SwaggerClient/SWGConfiguration.h index 99f97cdec1b..cd4e7117e89 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGConfiguration.h +++ b/samples/client/petstore/objc/SwaggerClient/SWGConfiguration.h @@ -1,6 +1,6 @@ #import #import "SWGApiClient.h" - + /** The `SWGConfiguration` class manages the configurations for the sdk. * * NOTE: This class is auto generated by the swagger code generator program. @@ -9,14 +9,14 @@ */ @class SWGApiClient; - + @interface SWGConfiguration : NSObject /** * Default api client */ @property (nonatomic) SWGApiClient *apiClient; - + /** * Default base url */ @@ -75,6 +75,18 @@ */ + (instancetype) sharedConfig; +/** + * SSL/TLS verification + * Set this to NO to skip verifying SSL certificate when calling API from https server + */ +@property (nonatomic) BOOL verifySSL; + +/** + * SSL/TLS verification + * Set this to customize the certificate file to verify the peer + */ +@property (nonatomic) NSString *sslCaCert; + /** * Sets API key * diff --git a/samples/client/petstore/objc/SwaggerClient/SWGConfiguration.m b/samples/client/petstore/objc/SwaggerClient/SWGConfiguration.m index 9cc1c07d9f9..16ce311c59e 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGConfiguration.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGConfiguration.m @@ -31,6 +31,7 @@ self.password = @""; self.tempFolderPath = nil; self.debug = NO; + self.verifySSL = YES; self.loggingFile = nil; self.mutableApiKey = [NSMutableDictionary dictionary]; self.mutableApiKeyPrefix = [NSMutableDictionary dictionary]; diff --git a/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m b/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m index 4b8eba70562..50a1583b8a3 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGPetApi.m @@ -421,7 +421,11 @@ static SWGPetApi* singletonAPI = nil; NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; if (petId != nil) { - pathParams[@"petId"] = petId; + if([petId isKindOfClass:[NSNumber class]]){ + pathParams[@"petId"] = [((NSNumber *)petId) stringValue]; + }else{ + pathParams[@"petId"] = petId; + } } @@ -513,7 +517,11 @@ static SWGPetApi* singletonAPI = nil; NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; if (petId != nil) { - pathParams[@"petId"] = petId; + if([petId isKindOfClass:[NSNumber class]]){ + pathParams[@"petId"] = [((NSNumber *)petId) stringValue]; + }else{ + pathParams[@"petId"] = petId; + } } @@ -551,13 +559,21 @@ static SWGPetApi* singletonAPI = nil; if (name) { - formParams[@"name"] = name; + if([name isKindOfClass:[NSNumber class]]){ + formParams[@"name"] = [((NSNumber *)name) stringValue]; + }else{ + formParams[@"name"] = name; + } } if (status) { - formParams[@"status"] = status; + if([status isKindOfClass:[NSNumber class]]){ + formParams[@"status"] = [((NSNumber *)status) stringValue]; + }else{ + formParams[@"status"] = status; + } } @@ -614,7 +630,11 @@ static SWGPetApi* singletonAPI = nil; NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; if (petId != nil) { - pathParams[@"petId"] = petId; + if([petId isKindOfClass:[NSNumber class]]){ + pathParams[@"petId"] = [((NSNumber *)petId) stringValue]; + }else{ + pathParams[@"petId"] = petId; + } } @@ -622,8 +642,13 @@ static SWGPetApi* singletonAPI = nil; NSMutableDictionary* headerParams = [NSMutableDictionary dictionaryWithDictionary:self.defaultHeaders]; - if(apiKey != nil) - headerParams[@"api_key"] = apiKey; + if(apiKey != nil){ + if([apiKey isKindOfClass:[NSNumber class]]){ + headerParams[@"api_key"] = [((NSNumber *)apiKey) stringValue]; + }else{ + headerParams[@"api_key"] = apiKey; + } + } // HTTP header `Accept` @@ -708,7 +733,11 @@ static SWGPetApi* singletonAPI = nil; NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; if (petId != nil) { - pathParams[@"petId"] = petId; + if([petId isKindOfClass:[NSNumber class]]){ + pathParams[@"petId"] = [((NSNumber *)petId) stringValue]; + }else{ + pathParams[@"petId"] = petId; + } } @@ -746,7 +775,11 @@ static SWGPetApi* singletonAPI = nil; if (additionalMetadata) { - formParams[@"additionalMetadata"] = additionalMetadata; + if([additionalMetadata isKindOfClass:[NSNumber class]]){ + formParams[@"additionalMetadata"] = [((NSNumber *)additionalMetadata) stringValue]; + }else{ + formParams[@"additionalMetadata"] = additionalMetadata; + } } diff --git a/samples/client/petstore/objc/SwaggerClient/SWGStoreApi.m b/samples/client/petstore/objc/SwaggerClient/SWGStoreApi.m index 5fdf86b43af..e70a31561ef 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGStoreApi.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGStoreApi.m @@ -250,7 +250,11 @@ static SWGStoreApi* singletonAPI = nil; NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; if (orderId != nil) { - pathParams[@"orderId"] = orderId; + if([orderId isKindOfClass:[NSNumber class]]){ + pathParams[@"orderId"] = [((NSNumber *)orderId) stringValue]; + }else{ + pathParams[@"orderId"] = orderId; + } } @@ -336,7 +340,11 @@ static SWGStoreApi* singletonAPI = nil; NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; if (orderId != nil) { - pathParams[@"orderId"] = orderId; + if([orderId isKindOfClass:[NSNumber class]]){ + pathParams[@"orderId"] = [((NSNumber *)orderId) stringValue]; + }else{ + pathParams[@"orderId"] = orderId; + } } diff --git a/samples/client/petstore/objc/SwaggerClient/SWGUserApi.m b/samples/client/petstore/objc/SwaggerClient/SWGUserApi.m index 75cf8d51b0e..032c305d43a 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGUserApi.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGUserApi.m @@ -495,7 +495,11 @@ static SWGUserApi* singletonAPI = nil; NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; if (username != nil) { - pathParams[@"username"] = username; + if([username isKindOfClass:[NSNumber class]]){ + pathParams[@"username"] = [((NSNumber *)username) stringValue]; + }else{ + pathParams[@"username"] = username; + } } @@ -584,7 +588,11 @@ static SWGUserApi* singletonAPI = nil; NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; if (username != nil) { - pathParams[@"username"] = username; + if([username isKindOfClass:[NSNumber class]]){ + pathParams[@"username"] = [((NSNumber *)username) stringValue]; + }else{ + pathParams[@"username"] = username; + } } @@ -670,7 +678,11 @@ static SWGUserApi* singletonAPI = nil; NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init]; if (username != nil) { - pathParams[@"username"] = username; + if([username isKindOfClass:[NSNumber class]]){ + pathParams[@"username"] = [((NSNumber *)username) stringValue]; + }else{ + pathParams[@"username"] = username; + } } diff --git a/samples/client/petstore/objc/SwaggerClientTests/SwaggerClient/SWGViewController.m b/samples/client/petstore/objc/SwaggerClientTests/SwaggerClient/SWGViewController.m index ab7f6e8f72e..ea8adc5c0e5 100644 --- a/samples/client/petstore/objc/SwaggerClientTests/SwaggerClient/SWGViewController.m +++ b/samples/client/petstore/objc/SwaggerClientTests/SwaggerClient/SWGViewController.m @@ -21,6 +21,16 @@ - (void)viewDidLoad { [super viewDidLoad]; + + SWGConfiguration *config = [SWGConfiguration sharedConfig]; + config.debug = YES; + config.verifySSL = NO; + + SWGPetApi *api = [[SWGPetApi alloc] init]; + SWGPet *pet = [self createPet]; + [api addPetWithCompletionBlock:pet completionHandler:^(NSError *error) { + NSLog(@"*** error: %@", error); + }]; } - (void)didReceiveMemoryWarning diff --git a/samples/client/petstore/objc/SwaggerClientTests/Tests/DeserializationTest.m b/samples/client/petstore/objc/SwaggerClientTests/Tests/DeserializationTest.m index 0460a4be4af..7b4b348e4d5 100644 --- a/samples/client/petstore/objc/SwaggerClientTests/Tests/DeserializationTest.m +++ b/samples/client/petstore/objc/SwaggerClientTests/Tests/DeserializationTest.m @@ -45,7 +45,10 @@ } - (void)testDeserializeObject { - XCTAssertTrue([[apiClient deserialize:@"" class:@"NSObject*"] isKindOfClass:[NSObject class]]); + NSNumber *data = @1; + NSNumber *result = [apiClient deserialize:data class:@"NSObject*"]; + + XCTAssertEqualObjects(data, result); } - (void)testDeserializeString {