forked from loafle/openapi-generator-original
add oauth support to objc
This commit is contained in:
parent
93657d5ec5
commit
c936f4b436
@ -716,11 +716,11 @@ static void (^reachabilityChangeBlock)(int);
|
|||||||
for (NSString *auth in authSettings) {
|
for (NSString *auth in authSettings) {
|
||||||
NSDictionary *authSetting = [[config authSettings] objectForKey:auth];
|
NSDictionary *authSetting = [[config authSettings] objectForKey:auth];
|
||||||
|
|
||||||
if (authSetting) {
|
if (authSetting) { // auth setting is set only if the key is non-empty
|
||||||
if ([authSetting[@"in"] isEqualToString:@"header"]) {
|
if ([authSetting[@"in"] isEqualToString:@"header"] && [authSetting[@"key"] length] != 0) {
|
||||||
[headersWithAuth setObject:authSetting[@"value"] forKey:authSetting[@"key"]];
|
[headersWithAuth setObject:authSetting[@"value"] forKey:authSetting[@"key"]];
|
||||||
}
|
}
|
||||||
else if ([authSetting[@"in"] isEqualToString:@"query"]) {
|
else if ([authSetting[@"in"] isEqualToString:@"query"] && [authSetting[@"key"] length] != 0) {
|
||||||
[querysWithAuth setObject:authSetting[@"value"] forKey:authSetting[@"key"]];
|
[querysWithAuth setObject:authSetting[@"value"] forKey:authSetting[@"key"]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
self.host = @"{{basePath}}";
|
self.host = @"{{basePath}}";
|
||||||
self.username = @"";
|
self.username = @"";
|
||||||
self.password = @"";
|
self.password = @"";
|
||||||
|
self.accessToken= @"";
|
||||||
self.tempFolderPath = nil;
|
self.tempFolderPath = nil;
|
||||||
self.debug = NO;
|
self.debug = NO;
|
||||||
self.verifySSL = YES;
|
self.verifySSL = YES;
|
||||||
@ -42,18 +43,23 @@
|
|||||||
#pragma mark - Instance Methods
|
#pragma mark - Instance Methods
|
||||||
|
|
||||||
- (NSString *) getApiKeyWithPrefix:(NSString *)key {
|
- (NSString *) getApiKeyWithPrefix:(NSString *)key {
|
||||||
if ([self.apiKeyPrefix objectForKey:key] && [self.apiKey objectForKey:key]) {
|
if ([self.apiKeyPrefix objectForKey:key] && [self.apiKey objectForKey:key] != (id)[NSNull null] && [[self.apiKey objectForKey:key] length] != 0) { // both api key prefix and api key are set
|
||||||
return [NSString stringWithFormat:@"%@ %@", [self.apiKeyPrefix objectForKey:key], [self.apiKey objectForKey:key]];
|
return [NSString stringWithFormat:@"%@ %@", [self.apiKeyPrefix objectForKey:key], [self.apiKey objectForKey:key]];
|
||||||
}
|
}
|
||||||
else if ([self.apiKey objectForKey:key]) {
|
else if ([self.apiKey objectForKey:key] != (id)[NSNull null] && [[self.apiKey objectForKey:key] length] != 0) { // only api key, no api key prefix
|
||||||
return [NSString stringWithFormat:@"%@", [self.apiKey objectForKey:key]];
|
return [NSString stringWithFormat:@"%@", [self.apiKey objectForKey:key]];
|
||||||
}
|
}
|
||||||
else {
|
else { // return empty string if nothing is set
|
||||||
return @"";
|
return @"";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSString *) getBasicAuthToken {
|
- (NSString *) getBasicAuthToken {
|
||||||
|
// return empty string if username and password are empty
|
||||||
|
if (self.username.length == 0 && self.password.length == 0){
|
||||||
|
return @"";
|
||||||
|
}
|
||||||
|
|
||||||
NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", self.username, self.password];
|
NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", self.username, self.password];
|
||||||
NSData *data = [basicAuthCredentials dataUsingEncoding:NSUTF8StringEncoding];
|
NSData *data = [basicAuthCredentials dataUsingEncoding:NSUTF8StringEncoding];
|
||||||
basicAuthCredentials = [NSString stringWithFormat:@"Basic %@", [data base64EncodedStringWithOptions:0]];
|
basicAuthCredentials = [NSString stringWithFormat:@"Basic %@", [data base64EncodedStringWithOptions:0]];
|
||||||
@ -61,6 +67,15 @@
|
|||||||
return basicAuthCredentials;
|
return basicAuthCredentials;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (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
|
#pragma mark - Setter Methods
|
||||||
|
|
||||||
- (void) setApiKey:(NSString *)apiKey forApiKeyIdentifier:(NSString *)identifier {
|
- (void) setApiKey:(NSString *)apiKey forApiKeyIdentifier:(NSString *)identifier {
|
||||||
@ -126,6 +141,15 @@
|
|||||||
@"value": [self getBasicAuthToken]
|
@"value": [self getBasicAuthToken]
|
||||||
},
|
},
|
||||||
{{/isBasic}}
|
{{/isBasic}}
|
||||||
|
{{#isOAuth}}
|
||||||
|
@"{{name}}":
|
||||||
|
@{
|
||||||
|
@"type": @"oauth",
|
||||||
|
@"in": @"header",
|
||||||
|
@"key": @"Authorization",
|
||||||
|
@"value": [self getAccessToken]
|
||||||
|
},
|
||||||
|
{{/isOAuth}}
|
||||||
{{/authMethods}}
|
{{/authMethods}}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -46,6 +46,11 @@
|
|||||||
*/
|
*/
|
||||||
@property (nonatomic) NSString *password;
|
@property (nonatomic) NSString *password;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Access token for OAuth
|
||||||
|
*/
|
||||||
|
@property (nonatomic) NSString *accessToken;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Temp folder for file download
|
* Temp folder for file download
|
||||||
*/
|
*/
|
||||||
@ -132,6 +137,11 @@
|
|||||||
*/
|
*/
|
||||||
- (NSString *) getBasicAuthToken;
|
- (NSString *) getBasicAuthToken;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets OAuth access token
|
||||||
|
*/
|
||||||
|
- (NSString *) getAccessToken;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets Authentication Setings
|
* Gets Authentication Setings
|
||||||
*/
|
*/
|
||||||
|
@ -6,7 +6,7 @@ This ObjC package is automatically generated by the [Swagger Codegen](https://gi
|
|||||||
|
|
||||||
- API version: 1.0.0
|
- API version: 1.0.0
|
||||||
- Package version:
|
- Package version:
|
||||||
- Build date: 2016-04-01T18:59:36.262+08:00
|
- Build date: 2016-04-05T23:29:02.396+08:00
|
||||||
- Build package: class io.swagger.codegen.languages.ObjcClientCodegen
|
- Build package: class io.swagger.codegen.languages.ObjcClientCodegen
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
@ -77,7 +77,7 @@ SWGConfiguration *apiConfig = [SWGConfiguration sharedConfig];
|
|||||||
[apiConfig setAccessToken:@"YOUR_ACCESS_TOKEN"];
|
[apiConfig setAccessToken:@"YOUR_ACCESS_TOKEN"];
|
||||||
|
|
||||||
|
|
||||||
SWGPet* *body = [[SWGPet alloc] init]; // Pet object that needs to be added to the store
|
SWGPet* *body = [[SWGPet alloc] init]; // Pet object that needs to be added to the store (optional)
|
||||||
|
|
||||||
@try
|
@try
|
||||||
{
|
{
|
||||||
|
@ -716,11 +716,11 @@ static void (^reachabilityChangeBlock)(int);
|
|||||||
for (NSString *auth in authSettings) {
|
for (NSString *auth in authSettings) {
|
||||||
NSDictionary *authSetting = [[config authSettings] objectForKey:auth];
|
NSDictionary *authSetting = [[config authSettings] objectForKey:auth];
|
||||||
|
|
||||||
if (authSetting) {
|
if (authSetting) { // auth setting is set only if the key is non-empty
|
||||||
if ([authSetting[@"in"] isEqualToString:@"header"]) {
|
if ([authSetting[@"in"] isEqualToString:@"header"] && [authSetting[@"key"] length] != 0) {
|
||||||
[headersWithAuth setObject:authSetting[@"value"] forKey:authSetting[@"key"]];
|
[headersWithAuth setObject:authSetting[@"value"] forKey:authSetting[@"key"]];
|
||||||
}
|
}
|
||||||
else if ([authSetting[@"in"] isEqualToString:@"query"]) {
|
else if ([authSetting[@"in"] isEqualToString:@"query"] && [authSetting[@"key"] length] != 0) {
|
||||||
[querysWithAuth setObject:authSetting[@"value"] forKey:authSetting[@"key"]];
|
[querysWithAuth setObject:authSetting[@"value"] forKey:authSetting[@"key"]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,6 +46,11 @@
|
|||||||
*/
|
*/
|
||||||
@property (nonatomic) NSString *password;
|
@property (nonatomic) NSString *password;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Access token for OAuth
|
||||||
|
*/
|
||||||
|
@property (nonatomic) NSString *accessToken;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Temp folder for file download
|
* Temp folder for file download
|
||||||
*/
|
*/
|
||||||
@ -132,6 +137,11 @@
|
|||||||
*/
|
*/
|
||||||
- (NSString *) getBasicAuthToken;
|
- (NSString *) getBasicAuthToken;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets OAuth access token
|
||||||
|
*/
|
||||||
|
- (NSString *) getAccessToken;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets Authentication Setings
|
* Gets Authentication Setings
|
||||||
*/
|
*/
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
self.host = @"http://petstore.swagger.io/v2";
|
self.host = @"http://petstore.swagger.io/v2";
|
||||||
self.username = @"";
|
self.username = @"";
|
||||||
self.password = @"";
|
self.password = @"";
|
||||||
|
self.accessToken= @"";
|
||||||
self.tempFolderPath = nil;
|
self.tempFolderPath = nil;
|
||||||
self.debug = NO;
|
self.debug = NO;
|
||||||
self.verifySSL = YES;
|
self.verifySSL = YES;
|
||||||
@ -42,18 +43,23 @@
|
|||||||
#pragma mark - Instance Methods
|
#pragma mark - Instance Methods
|
||||||
|
|
||||||
- (NSString *) getApiKeyWithPrefix:(NSString *)key {
|
- (NSString *) getApiKeyWithPrefix:(NSString *)key {
|
||||||
if ([self.apiKeyPrefix objectForKey:key] && [self.apiKey objectForKey:key]) {
|
if ([self.apiKeyPrefix objectForKey:key] && [self.apiKey objectForKey:key] != (id)[NSNull null] && [[self.apiKey objectForKey:key] length] != 0) { // both api key prefix and api key are set
|
||||||
return [NSString stringWithFormat:@"%@ %@", [self.apiKeyPrefix objectForKey:key], [self.apiKey objectForKey:key]];
|
return [NSString stringWithFormat:@"%@ %@", [self.apiKeyPrefix objectForKey:key], [self.apiKey objectForKey:key]];
|
||||||
}
|
}
|
||||||
else if ([self.apiKey objectForKey:key]) {
|
else if ([self.apiKey objectForKey:key] != (id)[NSNull null] && [[self.apiKey objectForKey:key] length] != 0) { // only api key, no api key prefix
|
||||||
return [NSString stringWithFormat:@"%@", [self.apiKey objectForKey:key]];
|
return [NSString stringWithFormat:@"%@", [self.apiKey objectForKey:key]];
|
||||||
}
|
}
|
||||||
else {
|
else { // return empty string if nothing is set
|
||||||
return @"";
|
return @"";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSString *) getBasicAuthToken {
|
- (NSString *) getBasicAuthToken {
|
||||||
|
// return empty string if username and password are empty
|
||||||
|
if (self.username.length == 0 && self.password.length == 0){
|
||||||
|
return @"";
|
||||||
|
}
|
||||||
|
|
||||||
NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", self.username, self.password];
|
NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", self.username, self.password];
|
||||||
NSData *data = [basicAuthCredentials dataUsingEncoding:NSUTF8StringEncoding];
|
NSData *data = [basicAuthCredentials dataUsingEncoding:NSUTF8StringEncoding];
|
||||||
basicAuthCredentials = [NSString stringWithFormat:@"Basic %@", [data base64EncodedStringWithOptions:0]];
|
basicAuthCredentials = [NSString stringWithFormat:@"Basic %@", [data base64EncodedStringWithOptions:0]];
|
||||||
@ -61,6 +67,15 @@
|
|||||||
return basicAuthCredentials;
|
return basicAuthCredentials;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (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
|
#pragma mark - Setter Methods
|
||||||
|
|
||||||
- (void) setApiKey:(NSString *)apiKey forApiKeyIdentifier:(NSString *)identifier {
|
- (void) setApiKey:(NSString *)apiKey forApiKeyIdentifier:(NSString *)identifier {
|
||||||
@ -149,6 +164,13 @@
|
|||||||
@"key": @"test_api_key_query",
|
@"key": @"test_api_key_query",
|
||||||
@"value": [self getApiKeyWithPrefix:@"test_api_key_query"]
|
@"value": [self getApiKeyWithPrefix:@"test_api_key_query"]
|
||||||
},
|
},
|
||||||
|
@"petstore_auth":
|
||||||
|
@{
|
||||||
|
@"type": @"oauth",
|
||||||
|
@"in": @"header",
|
||||||
|
@"key": @"Authorization",
|
||||||
|
@"value": [self getAccessToken]
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user