Skip to content

Commit 9a19db7

Browse files
committed
add auth generation for dart jaguar
1 parent f34f15c commit 9a19db7

39 files changed

+922
-125
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/DartJaguarClientCodegen.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,20 @@ public void processOpts() {
9191
additionalProperties.put("modelDocPath", modelDocPath);
9292

9393
final String libFolder = sourceFolder + File.separator + "lib";
94+
9495
supportingFiles.add(new SupportingFile("pubspec.mustache", "", "pubspec.yaml"));
95-
supportingFiles.add(new SupportingFile("analysis_options.mustache", "", ".analysis_options"));
96+
supportingFiles.add(new SupportingFile("analysis_options.mustache", "", "analysis_options.yaml"));
9697
supportingFiles.add(new SupportingFile("apilib.mustache", libFolder, "api.dart"));
9798

9899
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
99100
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
100101
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
102+
103+
final String authFolder = sourceFolder + File.separator + "lib" + File.separator + "auth";
104+
supportingFiles.add(new SupportingFile("auth/api_key_auth.mustache", authFolder, "api_key_auth.dart"));
105+
supportingFiles.add(new SupportingFile("auth/basic_auth.mustache", authFolder, "basic_auth.dart"));
106+
supportingFiles.add(new SupportingFile("auth/oauth.mustache", authFolder, "oauth.dart"));
107+
supportingFiles.add(new SupportingFile("auth/auth.mustache", authFolder, "auth.dart"));
101108
}
102109

103110

modules/swagger-codegen/src/main/resources/dart-jaguar/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ class {{classname}} extends _${{classname}}Client implements ApiClient {
2222
/// {{summary}}
2323
///
2424
/// {{notes}}
25-
@{{httpMethod}}Req(path: '{{path}}')
25+
@{{httpMethod}}Req(path: "{{path}}"{{#hasAuthMethods}}, metadata: {"auth": [{{#authMethods}} {"type": "{{type}}", "name": "{{name}}"{{#isApiKey}}, "keyName": "{{keyParamName}}", "where": "{{#isKeyInQuery}}query{{/isKeyInQuery}}{{#isKeyInHeader}}header{{/isKeyInHeader}}"{{/isApiKey}} }{{#hasMore}}, {{/hasMore}}{{/authMethods}}]}{{/hasAuthMethods}})
2626
Future<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {{nickname}}(
2727
{{#pathParams}}
28-
{{dataType}} {{paramName}}{{#hasMore}}, {{/hasMore}}
28+
@PathParam("{{baseName}}") {{dataType}} {{paramName}}{{#hasMore}}, {{/hasMore}}
2929
{{/pathParams}}
3030
{{#headerParams}}
3131
{{#-first}}{{#hasPathParams}},{{/hasPathParams}}{{/-first}}

modules/swagger-codegen/src/main/resources/dart-jaguar/apilib.mustache

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
library {{pubName}}.api;
22

3+
import 'package:http/http.dart';
34
import 'package:jaguar_serializer/jaguar_serializer.dart';
45
import 'package:jaguar_retrofit/jaguar_retrofit.dart';
6+
import 'package:{{pubName}}/auth/api_key_auth.dart';
7+
import 'package:{{pubName}}/auth/basic_auth.dart';
8+
import 'package:{{pubName}}/auth/oauth.dart';
59

610
{{#apiInfo}}{{#apis}}import 'package:{{pubName}}/api/{{classFilename}}.dart';
711
{{/apis}}{{/apiInfo}}
@@ -12,19 +16,52 @@ final jsonJaguarRepo = JsonRepo()
1216
{{#models}}{{#model}}..add({{classname}}Serializer())
1317
{{/model}}{{/models}};
1418

15-
final baseSwaggerPath = "{{basePath}}";
16-
final _baseRoute = Route(baseSwaggerPath);
19+
final _defaultInterceptors = [OAuthInterceptor(), BasicAuthInterceptor(), ApiKeyAuthInterceptor()];
1720

18-
class SwaggerGen {
19-
final List<Interceptor> interceptors;
20-
SwaggerGen({this.interceptors = const []}) {
21-
interceptors.forEach((interceptor) {
21+
class JaguarApiGen {
22+
List<Interceptor> interceptors;
23+
String basePath = "{{basePath}}";
24+
Route _baseRoute;
25+
26+
/**
27+
* Add custom global interceptors, put overrideInterceptors to true to set your interceptors only (auth interceptors will not be added)
28+
*/
29+
JaguarApiGen({List<Interceptor> interceptors, bool overrideInterceptors = false, String baseUrl}) {
30+
_baseRoute = Route(baseUrl ?? basePath).withClient(globalClient ?? IOClient());
31+
if(interceptors == null) {
32+
this.interceptors = _defaultInterceptors;
33+
}
34+
else if(overrideInterceptors){
35+
this.interceptors = interceptors;
36+
}
37+
else {
38+
this.interceptors = List.from(_defaultInterceptors)..addAll(interceptors);
39+
}
40+
41+
this.interceptors.forEach((interceptor) {
2242
_baseRoute.before(interceptor.before);
2343
_baseRoute.after(interceptor.after);
2444
});
2545
}
2646

27-
{{#apiInfo}}{{#apis}}{{classname}} get{{classname}}({Route base, SerializerRepo serializers}) {
47+
void setOAuthToken(String name, String token) {
48+
(_defaultInterceptors[0] as OAuthInterceptor).tokens[name] = token;
49+
}
50+
51+
void setBasicAuth(String name, String username, String password) {
52+
(_defaultInterceptors[1] as BasicAuthInterceptor).authInfo[name] = BasicAuthInfo(username, password);
53+
}
54+
55+
void setApiKey(String name, String apiKey) {
56+
(_defaultInterceptors[2] as ApiKeyAuthInterceptor).apiKeys[name] = apiKey;
57+
}
58+
59+
{{#apiInfo}}{{#apis}}
60+
/**
61+
* Get {{classname}} instance, base route and serializer can be overridden by a given but be careful,
62+
* by doing that all interceptors will not be executed
63+
*/
64+
{{classname}} get{{classname}}({Route base, SerializerRepo serializers}) {
2865
if(base == null) {
2966
base = _baseRoute;
3067
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import 'dart:async';
2+
import 'package:{{pubName}}/auth/auth.dart';
3+
import 'package:jaguar_retrofit/jaguar_retrofit.dart';
4+
5+
class ApiKeyAuthInterceptor extends AuthInterceptor {
6+
Map<String, String> apiKeys = {};
7+
8+
@override
9+
FutureOr<void> before(RouteBase route) {
10+
final authInfo = getAuthInfo(route, "apiKey");
11+
for (var info in authInfo) {
12+
final authName = info["name"];
13+
final authKeyName = info["keyName"];
14+
final authWhere = info["where"];
15+
final apiKey = apiKeys[authName];
16+
if(apiKey != null) {
17+
if(authWhere == 'query'){
18+
route.query(authKeyName, apiKey);
19+
}
20+
else {
21+
route.header(authKeyName, apiKey);
22+
}
23+
break;
24+
}
25+
}
26+
return super.before(route);
27+
}
28+
29+
@override
30+
FutureOr after(StringResponse response) {
31+
return Future.value(response);
32+
}
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import 'dart:async';
2+
3+
import 'package:jaguar_retrofit/jaguar_retrofit.dart';
4+
5+
abstract class AuthInterceptor extends Interceptor {
6+
/*
7+
* Get auth information on given route for the given type
8+
* Can return null if type is not present on auth data or if route doesn't need authentication
9+
*/
10+
List<Map<String, dynamic>> getAuthInfo(RouteBase route, String type) {
11+
if (route.metadataMap.containsKey("auth")) {
12+
final auth = route.metadataMap["auth"];
13+
List<Map<String, dynamic>> results = [];
14+
for (var info in auth) {
15+
if(info["type"] == type) {
16+
results.add(info);
17+
}
18+
}
19+
return results;
20+
}
21+
return [];
22+
}
23+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import 'dart:async';
2+
import 'package:{{pubName}}/auth/auth.dart';
3+
import 'package:jaguar_retrofit/jaguar_retrofit.dart';
4+
5+
class BasicAuthInfo {
6+
final String username;
7+
final String password;
8+
9+
const BasicAuthInfo(this.username, this.password);
10+
11+
}
12+
13+
class BasicAuthInterceptor extends AuthInterceptor {
14+
Map<String, BasicAuthInfo> authInfo = {};
15+
16+
@override
17+
FutureOr<void> before(RouteBase route) {
18+
final metadataAuthInfo = getAuthInfo(route, "basic");
19+
for (var info in metadataAuthInfo) {
20+
final authName = info["name"];
21+
final basicAuthInfo = authInfo[authName];
22+
if(basicAuthInfo != null) {
23+
route.basicAuth(basicAuthInfo.username, basicAuthInfo.password);
24+
break;
25+
}
26+
}
27+
return super.before(route);
28+
}
29+
30+
@override
31+
FutureOr after(StringResponse response) {
32+
return Future.value(response);
33+
}
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import 'dart:async';
2+
import 'package:{{pubName}}/auth/auth.dart';
3+
import 'package:jaguar_retrofit/jaguar_retrofit.dart';
4+
5+
class OAuthInterceptor extends AuthInterceptor {
6+
Map<String, String> tokens = {};
7+
8+
@override
9+
FutureOr<void> before(RouteBase route) {
10+
final authInfo = getAuthInfo(route, "oauth");
11+
for (var info in authInfo) {
12+
final token = tokens[info["name"]];
13+
if(token != null) {
14+
route.header("Authorization", "Bearer ${token}");
15+
break;
16+
}
17+
}
18+
return super.before(route);
19+
}
20+
21+
@override
22+
FutureOr after(StringResponse response) {
23+
return Future.value(response);
24+
}
25+
}

modules/swagger-codegen/src/main/resources/dart-jaguar/pubspec.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ description: {{pubDescription}}
44
environment:
55
sdk: ">=2.0.0 <3.0.0"
66
dependencies:
7-
jaguar_retrofit: '^2.4.1'
8-
jaguar_serializer: '^2.2.0'
7+
jaguar_retrofit: '^2.5.4'
8+
jaguar_serializer: '^2.2.2'
99
dev_dependencies:
10-
jaguar_retrofit_gen: '^2.4.2'
10+
jaguar_retrofit_gen: '^2.5.1'
1111
jaguar_serializer_cli: '^2.2.1'
1212
build_runner: '^0.10.0'

samples/client/petstore/dart-jaguar/flutter_petstore/ios/Flutter/flutter_assets/LICENSE

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,34 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
7373
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
7474
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7575

76+
--------------------------------------------------------------------------------
77+
auth_header
78+
79+
Copyright (c) 2016, Ravi Teja Gudapati.
80+
All rights reserved.
81+
82+
Redistribution and use in source and binary forms, with or without
83+
modification, are permitted provided that the following conditions are met:
84+
* Redistributions of source code must retain the above copyright
85+
notice, this list of conditions and the following disclaimer.
86+
* Redistributions in binary form must reproduce the above copyright
87+
notice, this list of conditions and the following disclaimer in the
88+
documentation and/or other materials provided with the distribution.
89+
* Neither the name of the <organization> nor the
90+
names of its contributors may be used to endorse or promote products
91+
derived from this software without specific prior written permission.
92+
93+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
94+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
95+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
96+
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
97+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
98+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
100+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
101+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
102+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
103+
76104
--------------------------------------------------------------------------------
77105
boolean_selector
78106
front_end
@@ -3164,6 +3192,34 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31643192
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31653193
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31663194

3195+
--------------------------------------------------------------------------------
3196+
client_cookie
3197+
3198+
Copyright (c) 2017, teja.
3199+
All rights reserved.
3200+
3201+
Redistribution and use in source and binary forms, with or without
3202+
modification, are permitted provided that the following conditions are met:
3203+
* Redistributions of source code must retain the above copyright
3204+
notice, this list of conditions and the following disclaimer.
3205+
* Redistributions in binary form must reproduce the above copyright
3206+
notice, this list of conditions and the following disclaimer in the
3207+
documentation and/or other materials provided with the distribution.
3208+
* Neither the name of the <organization> nor the
3209+
names of its contributors may be used to endorse or promote products
3210+
derived from this software without specific prior written permission.
3211+
3212+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
3213+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
3214+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3215+
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
3216+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3217+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
3218+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
3219+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3220+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3221+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3222+
31673223
--------------------------------------------------------------------------------
31683224
colorama
31693225

@@ -7653,6 +7709,66 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
76537709
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
76547710
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
76557711

7712+
--------------------------------------------------------------------------------
7713+
jaguar_resty
7714+
7715+
BSD 3-Clause License
7716+
7717+
Copyright (c) 2017, Ravi Teja Gudapati <[email protected]>
7718+
All rights reserved.
7719+
7720+
Redistribution and use in source and binary forms, with or without
7721+
modification, are permitted provided that the following conditions are met:
7722+
7723+
* Redistributions of source code must retain the above copyright notice, this
7724+
list of conditions and the following disclaimer.
7725+
7726+
* Redistributions in binary form must reproduce the above copyright notice,
7727+
this list of conditions and the following disclaimer in the documentation
7728+
and/or other materials provided with the distribution.
7729+
7730+
* Neither the name of the copyright holder nor the names of its
7731+
contributors may be used to endorse or promote products derived from
7732+
this software without specific prior written permission.
7733+
7734+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
7735+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
7736+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
7737+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
7738+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
7739+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
7740+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
7741+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
7742+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
7743+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7744+
--------------------------------------------------------------------------------
7745+
jaguar_retrofit
7746+
7747+
Copyright (c) 2016, Jaguar Authors.
7748+
All rights reserved.
7749+
7750+
Redistribution and use in source and binary forms, with or without
7751+
modification, are permitted provided that the following conditions are met:
7752+
* Redistributions of source code must retain the above copyright
7753+
notice, this list of conditions and the following disclaimer.
7754+
* Redistributions in binary form must reproduce the above copyright
7755+
notice, this list of conditions and the following disclaimer in the
7756+
documentation and/or other materials provided with the distribution.
7757+
* Neither the name of Jaguar Authors nor the
7758+
names of its contributors may be used to endorse or promote products
7759+
derived from this software without specific prior written permission.
7760+
7761+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
7762+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
7763+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
7764+
DISCLAIMED. IN NO EVENT SHALL Jaguar Authors BE LIABLE FOR ANY
7765+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
7766+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
7767+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
7768+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
7769+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
7770+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7771+
76567772
--------------------------------------------------------------------------------
76577773
js
76587774

0 commit comments

Comments
 (0)