Update security samples for ObjectiveC client. (#5286)

This commit is contained in:
Paŭlo Ebermann
2017-04-04 08:44:09 +02:00
committed by wing328
parent 35ce3f48b9
commit ca976c524a
5 changed files with 351 additions and 3 deletions

View File

@@ -59,9 +59,6 @@ NSInteger kSWGFakeApiMissingParamErrorCode = 234513;
completionHandler: (void (^)(NSError* error)) handler {
NSMutableString* resourcePath = [NSMutableString stringWithFormat:@"/fake"];
// remove format in URL if needed
[resourcePath replaceOccurrencesOfString:@".{format}" withString:@".json" options:0 range:NSMakeRange(0,resourcePath.length)];
NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init];
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];

View File

@@ -0,0 +1,14 @@
/** The `SWGBasicAuthTokenProvider` class creates a basic auth token from username and password.
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen
* Do not edit the class manually.
*/
#import <Foundation/Foundation.h>
@interface SWGBasicAuthTokenProvider : NSObject
+ (NSString *)createBasicAuthTokenWithUsername:(NSString *)username password:(NSString *)password;
@end

View File

@@ -0,0 +1,19 @@
#import "SWGBasicAuthTokenProvider.h"
@implementation SWGBasicAuthTokenProvider
+ (NSString *)createBasicAuthTokenWithUsername:(NSString *)username password:(NSString *)password {
// return empty string if username and password are empty
if (username.length == 0 && password.length == 0){
return @"";
}
NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", username, password];
NSData *data = [basicAuthCredentials dataUsingEncoding:NSUTF8StringEncoding];
basicAuthCredentials = [NSString stringWithFormat:@"Basic %@", [data base64EncodedStringWithOptions:0]];
return basicAuthCredentials;
}
@end

View File

@@ -0,0 +1,166 @@
#import <Foundation/Foundation.h>
#import "SWGConfiguration.h"
/**
* Swagger Petstore *_/ ' \" =end -- \\r\\n \\n \\r
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end --
*
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
* Contact: apiteam@swagger.io *_/ ' \" =end -- \\r\\n \\n \\r
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
@class SWGApiClient;
@interface SWGDefaultConfiguration : NSObject <SWGConfiguration>
/**
* Default api logger
*/
@property (nonatomic, strong) SWGLogger * logger;
/**
* Default base url
*/
@property (nonatomic) NSString *host;
/**
* Api key values for Api Key type Authentication
*
* To add or remove api key, use `setApiKey:forApiKeyIdentifier:`.
*/
@property (readonly, nonatomic, strong) NSDictionary *apiKey;
/**
* Api key prefix values to be prepend to the respective api key
*
* To add or remove prefix, use `setApiKeyPrefix:forApiKeyPrefixIdentifier:`.
*/
@property (readonly, nonatomic, strong) NSDictionary *apiKeyPrefix;
/**
* Username for HTTP Basic Authentication
*/
@property (nonatomic) NSString *username;
/**
* Password for HTTP Basic Authentication
*/
@property (nonatomic) NSString *password;
/**
* Access token for OAuth
*/
@property (nonatomic) NSString *accessToken;
/**
* Temp folder for file download
*/
@property (nonatomic) NSString *tempFolderPath;
/**
* Debug switch, default false
*/
@property (nonatomic) BOOL debug;
/**
* Gets configuration singleton instance
*/
+ (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
*
* To remove a apiKey for an identifier, just set the apiKey to nil.
*
* @param apiKey API key or token.
* @param identifier API key identifier (authentication schema).
*
*/
- (void) setApiKey:(NSString *)apiKey forApiKeyIdentifier:(NSString*)identifier;
/**
* Removes api key
*
* @param identifier API key identifier.
*/
- (void) removeApiKey:(NSString *)identifier;
/**
* Sets the prefix for API key
*
* @param prefix API key prefix.
* @param identifier API key identifier.
*/
- (void) setApiKeyPrefix:(NSString *)prefix forApiKeyPrefixIdentifier:(NSString *)identifier;
/**
* Removes api key prefix
*
* @param identifier API key identifier.
*/
- (void) removeApiKeyPrefix:(NSString *)identifier;
/**
* Gets API key (with prefix if set)
*/
- (NSString *) getApiKeyWithPrefix:(NSString *) key;
/**
* Gets Basic Auth token
*/
- (NSString *) getBasicAuthToken;
/**
* Gets OAuth access token
*/
- (NSString *) getAccessToken;
/**
* Gets Authentication Settings
*/
- (NSDictionary *) authSettings;
/**
* Default headers for all services
*/
@property (readonly, nonatomic, strong) NSDictionary *defaultHeaders;
/**
* Removes header from defaultHeaders
*
* @param key Header name.
*/
-(void) removeDefaultHeaderForKey:(NSString*)key;
/**
* Sets the header for key
*
* @param value Value for header name
* @param key Header name
*/
-(void) setDefaultHeaderValue:(NSString*) value forKey:(NSString*)key;
/**
* @param key Header key name.
*/
-(NSString*) defaultHeaderForKey:(NSString*)key;
@end

View File

@@ -0,0 +1,152 @@
#import "SWGDefaultConfiguration.h"
#import "SWGBasicAuthTokenProvider.h"
#import "SWGLogger.h"
@interface SWGDefaultConfiguration ()
@property (nonatomic, strong) NSMutableDictionary *mutableDefaultHeaders;
@property (nonatomic, strong) NSMutableDictionary *mutableApiKey;
@property (nonatomic, strong) NSMutableDictionary *mutableApiKeyPrefix;
@end
@implementation SWGDefaultConfiguration
#pragma mark - Singleton Methods
+ (instancetype) sharedConfig {
static SWGDefaultConfiguration *shardConfig = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shardConfig = [[self alloc] init];
});
return shardConfig;
}
#pragma mark - Initialize Methods
- (instancetype) init {
self = [super init];
if (self) {
_host = @"https://petstore.swagger.io *_/ ' \" =end -- \\r\\n \\n \\r/v2 *_/ ' \" =end -- \\r\\n \\n \\r";
_username = @"";
_password = @"";
_accessToken= @"";
_verifySSL = YES;
_mutableApiKey = [NSMutableDictionary dictionary];
_mutableApiKeyPrefix = [NSMutableDictionary dictionary];
_mutableDefaultHeaders = [NSMutableDictionary dictionary];
_logger = [SWGLogger sharedLogger];
}
return self;
}
#pragma mark - Instance Methods
- (NSString *) getApiKeyWithPrefix:(NSString *)key {
NSString *prefix = self.apiKeyPrefix[key];
NSString *apiKey = self.apiKey[key];
if (prefix && apiKey != (id)[NSNull null] && apiKey.length > 0) { // both api key prefix and api key are set
return [NSString stringWithFormat:@"%@ %@", prefix, apiKey];
}
else if (apiKey != (id)[NSNull null] && apiKey.length > 0) { // only api key, no api key prefix
return [NSString stringWithFormat:@"%@", self.apiKey[key]];
}
else { // return empty string if nothing is set
return @"";
}
}
- (NSString *) getBasicAuthToken {
NSString *basicAuthToken = [SWGBasicAuthTokenProvider createBasicAuthTokenWithUsername:self.username password:self.password];
return basicAuthToken;
}
- (NSString *) getAccessToken {
if (self.accessToken.length == 0) { // token not set, return empty string
return @"";
} else {
return [NSString stringWithFormat:@"Bearer %@", self.accessToken];
}
}
#pragma mark - Setter Methods
- (void) setApiKey:(NSString *)apiKey forApiKeyIdentifier:(NSString *)identifier {
[self.mutableApiKey setValue:apiKey forKey:identifier];
}
- (void) removeApiKey:(NSString *)identifier {
[self.mutableApiKey removeObjectForKey:identifier];
}
- (void) setApiKeyPrefix:(NSString *)prefix forApiKeyPrefixIdentifier:(NSString *)identifier {
[self.mutableApiKeyPrefix setValue:prefix forKey:identifier];
}
- (void) removeApiKeyPrefix:(NSString *)identifier {
[self.mutableApiKeyPrefix removeObjectForKey:identifier];
}
#pragma mark - Getter Methods
- (NSDictionary *) apiKey {
return [NSDictionary dictionaryWithDictionary:self.mutableApiKey];
}
- (NSDictionary *) apiKeyPrefix {
return [NSDictionary dictionaryWithDictionary:self.mutableApiKeyPrefix];
}
#pragma mark -
- (NSDictionary *) authSettings {
return @{
@"api_key":
@{
@"type": @"api_key",
@"in": @"header",
@"key": @"api_key */ &#39; &quot; &#x3D;end -- \r\n \n \r",
@"value": [self getApiKeyWithPrefix:@"api_key */ &#39; &quot; &#x3D;end -- \r\n \n \r"]
},
@"petstore_auth":
@{
@"type": @"oauth",
@"in": @"header",
@"key": @"Authorization",
@"value": [self getAccessToken]
},
};
}
-(BOOL)debug {
return self.logger.isEnabled;
}
-(void)setDebug:(BOOL)debug {
self.logger.enabled = debug;
}
- (void)setDefaultHeaderValue:(NSString *)value forKey:(NSString *)key {
if(!value) {
[self.mutableDefaultHeaders removeObjectForKey:key];
return;
}
self.mutableDefaultHeaders[key] = value;
}
-(void) removeDefaultHeaderForKey:(NSString*)key {
[self.mutableDefaultHeaders removeObjectForKey:key];
}
- (NSString *)defaultHeaderForKey:(NSString *)key {
return self.mutableDefaultHeaders[key];
}
- (NSDictionary *)defaultHeaders {
return [self.mutableDefaultHeaders copy];
}
@end