Skip to content

Commit b5429d9

Browse files
committed
Added authentication for objc client.
1 parent ca14529 commit b5429d9

28 files changed

+721
-15
lines changed

modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ public ObjcClientCodegen() {
122122
supportingFiles.add(new SupportingFile("SWGFile.m", sourceFolder, "SWGFile.m"));
123123
supportingFiles.add(new SupportingFile("JSONValueTransformer+ISO8601.m", sourceFolder, "JSONValueTransformer+ISO8601.m"));
124124
supportingFiles.add(new SupportingFile("JSONValueTransformer+ISO8601.h", sourceFolder, "JSONValueTransformer+ISO8601.h"));
125+
supportingFiles.add(new SupportingFile("SWGConfiguration-body.mustache", sourceFolder, "SWGConfiguration.m"));
126+
supportingFiles.add(new SupportingFile("SWGConfiguration-header.mustache", sourceFolder, "SWGConfiguration.h"));
125127
supportingFiles.add(new SupportingFile("Podfile.mustache", "", "Podfile"));
126128
}
127129

modules/swagger-codegen/src/main/resources/objc/SWGApiClient.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,16 @@ extern NSString *const SWGResponseObjectErrorKey;
5454
-(void)setHeaderValue:(NSString*) value
5555
forKey:(NSString*) forKey;
5656

57+
- (void) updateHeaderParams:(NSDictionary **)headers
58+
queryParams:(NSDictionary **)querys
59+
WithAuthSettings:(NSArray *)authSettings;
60+
5761
-(NSNumber*) dictionary:(NSString*) path
5862
method:(NSString*) method
5963
queryParams:(NSDictionary*) queryParams
6064
body:(id) body
6165
headerParams:(NSDictionary*) headerParams
66+
authSettings: (NSArray *) authSettings
6267
requestContentType:(NSString*) requestContentType
6368
responseContentType:(NSString*) responseContentType
6469
completionBlock:(void (^)(NSDictionary*, NSError *))completionBlock;
@@ -68,8 +73,18 @@ extern NSString *const SWGResponseObjectErrorKey;
6873
queryParams:(NSDictionary*) queryParams
6974
body:(id) body
7075
headerParams:(NSDictionary*) headerParams
76+
authSettings: (NSArray *) authSettings
7177
requestContentType:(NSString*) requestContentType
7278
responseContentType:(NSString*) responseContentType
7379
completionBlock:(void (^)(NSString*, NSError *))completionBlock;
7480
@end
7581

82+
83+
84+
85+
86+
87+
88+
89+
90+

modules/swagger-codegen/src/main/resources/objc/SWGApiClient.m

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#import "SWGApiClient.h"
22
#import "SWGFile.h"
33
#import "SWGQueryParamCollection.h"
4+
#import "SWGConfiguration.h"
45

56
@implementation SWGApiClient
67

@@ -15,10 +16,22 @@ @implementation SWGApiClient
1516
static void (^reachabilityChangeBlock)(int);
1617
static bool loggingEnabled = true;
1718

19+
#pragma mark - Log Methods
20+
1821
+(void)setLoggingEnabled:(bool) state {
1922
loggingEnabled = state;
2023
}
2124

25+
- (void)logRequest:(NSURLRequest*)request {
26+
NSLog(@"request: %@", [self descriptionForRequest:request]);
27+
}
28+
29+
- (void)logResponse:(id)data forRequest:(NSURLRequest*)request error:(NSError*)error {
30+
NSLog(@"request: %@ response: %@ ", [self descriptionForRequest:request], data );
31+
}
32+
33+
#pragma mark -
34+
2235
+(void)clearCache {
2336
[[NSURLCache sharedURLCache] removeAllCachedResponses];
2437
}
@@ -305,19 +318,47 @@ - (NSString*)descriptionForRequest:(NSURLRequest*)request {
305318
return [[request URL] absoluteString];
306319
}
307320

308-
- (void)logRequest:(NSURLRequest*)request {
309-
NSLog(@"request: %@", [self descriptionForRequest:request]);
310-
}
311321

312-
- (void)logResponse:(id)data forRequest:(NSURLRequest*)request error:(NSError*)error {
313-
NSLog(@"request: %@ response: %@ ", [self descriptionForRequest:request], data );
322+
/**
323+
* Update header and query params based on authentication settings
324+
*/
325+
- (void) updateHeaderParams:(NSDictionary *__autoreleasing *)headers
326+
queryParams:(NSDictionary *__autoreleasing *)querys
327+
WithAuthSettings:(NSArray *)authSettings {
328+
329+
if (!authSettings || [authSettings count] == 0) {
330+
return;
331+
}
332+
333+
NSMutableDictionary *headersWithAuth = [NSMutableDictionary dictionaryWithDictionary:*headers];
334+
NSMutableDictionary *querysWithAuth = [NSMutableDictionary dictionaryWithDictionary:*querys];
335+
336+
SWGConfiguration *config = [SWGConfiguration sharedConfig];
337+
for (NSString *auth in authSettings) {
338+
NSDictionary *authSetting = [[config authSettings] objectForKey:auth];
339+
340+
if (authSetting) {
341+
if ([authSetting[@"in"] isEqualToString:@"header"]) {
342+
[headersWithAuth setObject:authSetting[@"value"] forKey:authSetting[@"key"]];
343+
}
344+
else if ([authSetting[@"in"] isEqualToString:@"query"]) {
345+
[querysWithAuth setObject:authSetting[@"value"] forKey:authSetting[@"key"]];
346+
}
347+
}
348+
}
349+
350+
*headers = [NSDictionary dictionaryWithDictionary:headersWithAuth];
351+
*querys = [NSDictionary dictionaryWithDictionary:querysWithAuth];
314352
}
315353

354+
#pragma mark - Perform Request Methods
355+
316356
-(NSNumber*) dictionary: (NSString*) path
317357
method: (NSString*) method
318358
queryParams: (NSDictionary*) queryParams
319359
body: (id) body
320360
headerParams: (NSDictionary*) headerParams
361+
authSettings: (NSArray *) authSettings
321362
requestContentType: (NSString*) requestContentType
322363
responseContentType: (NSString*) responseContentType
323364
completionBlock: (void (^)(NSDictionary*, NSError *))completionBlock {
@@ -342,6 +383,9 @@ -(NSNumber*) dictionary: (NSString*) path
342383
else {
343384
self.responseSerializer = [AFHTTPResponseSerializer serializer];
344385
}
386+
387+
// auth setting
388+
[self updateHeaderParams:&headerParams queryParams:&queryParams WithAuthSettings:authSettings];
345389

346390
NSMutableURLRequest * request = nil;
347391
if (body != nil && [body isKindOfClass:[NSArray class]]){
@@ -476,6 +520,7 @@ -(NSNumber*) stringWithCompletionBlock: (NSString*) path
476520
queryParams: (NSDictionary*) queryParams
477521
body: (id) body
478522
headerParams: (NSDictionary*) headerParams
523+
authSettings: (NSArray *) authSettings
479524
requestContentType: (NSString*) requestContentType
480525
responseContentType: (NSString*) responseContentType
481526
completionBlock: (void (^)(NSString*, NSError *))completionBlock {
@@ -500,6 +545,9 @@ -(NSNumber*) stringWithCompletionBlock: (NSString*) path
500545
else {
501546
self.responseSerializer = [AFHTTPResponseSerializer serializer];
502547
}
548+
549+
// auth setting
550+
[self updateHeaderParams:&headerParams queryParams:&queryParams WithAuthSettings:authSettings];
503551

504552
NSMutableURLRequest * request = nil;
505553
if (body != nil && [body isKindOfClass:[NSArray class]]){
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#import "SWGConfiguration.h"
2+
3+
@interface SWGConfiguration ()
4+
5+
@property (readwrite, nonatomic, strong) NSMutableDictionary *mutableApiKey;
6+
@property (readwrite, nonatomic, strong) NSMutableDictionary *mutableApiKeyPrefix;
7+
8+
@end
9+
10+
@implementation SWGConfiguration
11+
12+
#pragma mark - Singletion Methods
13+
14+
+ (instancetype) sharedConfig {
15+
static SWGConfiguration *shardConfig = nil;
16+
static dispatch_once_t onceToken;
17+
dispatch_once(&onceToken, ^{
18+
shardConfig = [[self alloc] init];
19+
});
20+
return shardConfig;
21+
}
22+
23+
#pragma mark - Initialize Methods
24+
25+
- (instancetype) init {
26+
self = [super init];
27+
if (self) {
28+
self.username = @"";
29+
self.password = @"";
30+
self.mutableApiKey = [NSMutableDictionary dictionary];
31+
self.mutableApiKeyPrefix = [NSMutableDictionary dictionary];
32+
}
33+
return self;
34+
}
35+
36+
#pragma mark - Instance Methods
37+
38+
- (NSString *) getApiKeyWithPrefix:(NSString *)key {
39+
if ([self.apiKeyPrefix objectForKey:key] && [self.apiKey objectForKey:key]) {
40+
return [NSString stringWithFormat:@"%@ %@", [self.apiKeyPrefix objectForKey:key], [self.apiKey objectForKey:key]];
41+
}
42+
else if ([self.apiKey objectForKey:key]) {
43+
return [NSString stringWithFormat:@"%@", [self.apiKey objectForKey:key]];
44+
}
45+
else {
46+
return @"";
47+
}
48+
}
49+
50+
#pragma mark - Setter Methods
51+
52+
- (void) setValue:(NSString *)value forApiKeyField:(NSString *)field {
53+
[self.mutableApiKey setValue:value forKey:field];
54+
}
55+
56+
- (void) setValue:(NSString *)value forApiKeyPrefixField:(NSString *)field {
57+
[self.mutableApiKeyPrefix setValue:value forKey:field];
58+
}
59+
60+
#pragma mark - Getter Methods
61+
62+
- (NSDictionary *) apiKey {
63+
return [NSDictionary dictionaryWithDictionary:self.mutableApiKey];
64+
}
65+
66+
- (NSDictionary *) apiKeyPrefix {
67+
return [NSDictionary dictionaryWithDictionary:self.mutableApiKeyPrefix];
68+
}
69+
70+
#pragma mark -
71+
72+
- (NSDictionary *) authSettings {
73+
return @{ {{#authMethods}}{{#isApiKey}}
74+
@"{{name}}": @{
75+
@"type": @"api_key",
76+
@"in": {{#isKeyInHeader}}@"header"{{/isKeyInHeader}}{{#isKeyInQuery}}@"query"{{/isKeyInQuery}},
77+
@"key": @"{{keyParamName}}",
78+
@"value": [self getApiKeyWithPrefix:@"{{keyParamName}}"]
79+
},
80+
{{/isApiKey}}{{#isBasic}}
81+
@"{{name}}": @{
82+
@"type": @"basic",
83+
@"in": @"header",
84+
@"key": @"Authorization",
85+
@"value": [self getBasicAuthToken]
86+
},
87+
{{/isBasic}}{{/authMethods}}
88+
};
89+
}
90+
91+
@end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#import <Foundation/Foundation.h>
2+
3+
@interface SWGConfiguration : NSObject
4+
5+
6+
/**
7+
* Api key values for Api Key type Authentication
8+
*
9+
* To add or remove api key, use `setValue:forApiKeyField:`.
10+
*/
11+
@property (readonly, nonatomic, strong) NSDictionary *apiKey;
12+
13+
/**
14+
* Api key prefix values to be prepend to the respective api key
15+
*
16+
* To add or remove prefix, use `setValue:forApiKeyPrefixField:`.
17+
*/
18+
@property (readonly, nonatomic, strong) NSDictionary *apiKeyPrefix;
19+
20+
/**
21+
* Usename and Password for Basic type Authentication
22+
*/
23+
@property (nonatomic) NSString *username;
24+
@property (nonatomic) NSString *password;
25+
26+
/**
27+
* Get configuration singleton instance
28+
*/
29+
+ (instancetype) sharedConfig;
30+
31+
/**
32+
* Sets field in `apiKey`
33+
*/
34+
- (void) setValue:(NSString *)value forApiKeyField:(NSString*)field;
35+
36+
/**
37+
* Sets field in `apiKeyPrefix`
38+
*/
39+
- (void) setValue:(NSString *)value forApiKeyPrefixField:(NSString *)field;
40+
41+
/**
42+
* Get API key (with prefix if set)
43+
*/
44+
- (NSString *) getApiKeyWithPrefix:(NSString *) key;
45+
46+
/**
47+
* Get Authentication Setings
48+
*/
49+
- (NSDictionary *) authSettings;
50+
51+
@end

modules/swagger-codegen/src/main/resources/objc/SWGQueryParamCollection.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ - (id) initWithValuesAndFormat: (NSArray*) values
1313
return self;
1414
}
1515

16-
@end
16+
@end

modules/swagger-codegen/src/main/resources/objc/api-body.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ static NSString * basePath = @"{{basePath}}";
3939
return self;
4040
}
4141

42+
#pragma mark -
43+
4244
+({{classname}}*) apiWithHeader:(NSString*)headerValue key:(NSString*)key {
4345
static {{classname}}* singletonAPI = nil;
4446

@@ -129,6 +131,9 @@ static NSString * basePath = @"{{basePath}}";
129131
// request content type
130132
NSString *requestContentType = [SWGApiClient selectHeaderContentType:@[{{#consumes}}@"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}}]];
131133

134+
// Authentication setting
135+
NSArray *authSettings = @[{{#authMethods}}@"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}}];
136+
132137
id bodyDictionary = nil;
133138
{{#bodyParam}}
134139
id __body = {{paramName}};

modules/swagger-codegen/src/main/resources/objc/api-header.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
@property(nonatomic, assign)SWGApiClient *apiClient;
1212

13+
-(instancetype) initWithApiClient:(SWGApiClient *)apiClient;
1314
-(void) addHeader:(NSString*)value forKey:(NSString*)key;
1415
-(unsigned long) requestQueueSize;
1516
+({{classname}}*) apiWithHeader:(NSString*)headerValue key:(NSString*)key;

modules/swagger-codegen/src/main/resources/objc/apiBodyResponseWithContainer.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
queryParams: queryParams
55
body: bodyDictionary
66
headerParams: headerParams
7+
authSettings: authSettings
78
requestContentType: requestContentType
89
responseContentType: responseContentType
910
completionBlock: ^(NSDictionary *data, NSError *error) {

modules/swagger-codegen/src/main/resources/objc/apiNonPrimitiveResponse.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
queryParams: queryParams
66
body: bodyDictionary
77
headerParams: headerParams
8+
authSettings: authSettings
89
requestContentType: requestContentType
910
responseContentType: responseContentType
1011
completionBlock: ^(NSDictionary *data, NSError *error) {

0 commit comments

Comments
 (0)