Skip to content

Commit

Permalink
add auth generation for dart jaguar
Browse files Browse the repository at this point in the history
  • Loading branch information
jaumard committed Sep 10, 2018
1 parent f34f15c commit 9a19db7
Show file tree
Hide file tree
Showing 39 changed files with 922 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,20 @@ public void processOpts() {
additionalProperties.put("modelDocPath", modelDocPath);

final String libFolder = sourceFolder + File.separator + "lib";

supportingFiles.add(new SupportingFile("pubspec.mustache", "", "pubspec.yaml"));
supportingFiles.add(new SupportingFile("analysis_options.mustache", "", ".analysis_options"));
supportingFiles.add(new SupportingFile("analysis_options.mustache", "", "analysis_options.yaml"));
supportingFiles.add(new SupportingFile("apilib.mustache", libFolder, "api.dart"));

supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));

final String authFolder = sourceFolder + File.separator + "lib" + File.separator + "auth";
supportingFiles.add(new SupportingFile("auth/api_key_auth.mustache", authFolder, "api_key_auth.dart"));
supportingFiles.add(new SupportingFile("auth/basic_auth.mustache", authFolder, "basic_auth.dart"));
supportingFiles.add(new SupportingFile("auth/oauth.mustache", authFolder, "oauth.dart"));
supportingFiles.add(new SupportingFile("auth/auth.mustache", authFolder, "auth.dart"));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class {{classname}} extends _${{classname}}Client implements ApiClient {
/// {{summary}}
///
/// {{notes}}
@{{httpMethod}}Req(path: '{{path}}')
@{{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}})
Future<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {{nickname}}(
{{#pathParams}}
{{dataType}} {{paramName}}{{#hasMore}}, {{/hasMore}}
@PathParam("{{baseName}}") {{dataType}} {{paramName}}{{#hasMore}}, {{/hasMore}}
{{/pathParams}}
{{#headerParams}}
{{#-first}}{{#hasPathParams}},{{/hasPathParams}}{{/-first}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
library {{pubName}}.api;

import 'package:http/http.dart';
import 'package:jaguar_serializer/jaguar_serializer.dart';
import 'package:jaguar_retrofit/jaguar_retrofit.dart';
import 'package:{{pubName}}/auth/api_key_auth.dart';
import 'package:{{pubName}}/auth/basic_auth.dart';
import 'package:{{pubName}}/auth/oauth.dart';

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

final baseSwaggerPath = "{{basePath}}";
final _baseRoute = Route(baseSwaggerPath);
final _defaultInterceptors = [OAuthInterceptor(), BasicAuthInterceptor(), ApiKeyAuthInterceptor()];

class SwaggerGen {
final List<Interceptor> interceptors;
SwaggerGen({this.interceptors = const []}) {
interceptors.forEach((interceptor) {
class JaguarApiGen {
List<Interceptor> interceptors;
String basePath = "{{basePath}}";
Route _baseRoute;
/**
* Add custom global interceptors, put overrideInterceptors to true to set your interceptors only (auth interceptors will not be added)
*/
JaguarApiGen({List<Interceptor> interceptors, bool overrideInterceptors = false, String baseUrl}) {
_baseRoute = Route(baseUrl ?? basePath).withClient(globalClient ?? IOClient());
if(interceptors == null) {
this.interceptors = _defaultInterceptors;
}
else if(overrideInterceptors){
this.interceptors = interceptors;
}
else {
this.interceptors = List.from(_defaultInterceptors)..addAll(interceptors);
}

this.interceptors.forEach((interceptor) {
_baseRoute.before(interceptor.before);
_baseRoute.after(interceptor.after);
});
}

{{#apiInfo}}{{#apis}}{{classname}} get{{classname}}({Route base, SerializerRepo serializers}) {
void setOAuthToken(String name, String token) {
(_defaultInterceptors[0] as OAuthInterceptor).tokens[name] = token;
}

void setBasicAuth(String name, String username, String password) {
(_defaultInterceptors[1] as BasicAuthInterceptor).authInfo[name] = BasicAuthInfo(username, password);
}

void setApiKey(String name, String apiKey) {
(_defaultInterceptors[2] as ApiKeyAuthInterceptor).apiKeys[name] = apiKey;
}

{{#apiInfo}}{{#apis}}
/**
* Get {{classname}} instance, base route and serializer can be overridden by a given but be careful,
* by doing that all interceptors will not be executed
*/
{{classname}} get{{classname}}({Route base, SerializerRepo serializers}) {
if(base == null) {
base = _baseRoute;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'dart:async';
import 'package:{{pubName}}/auth/auth.dart';
import 'package:jaguar_retrofit/jaguar_retrofit.dart';

class ApiKeyAuthInterceptor extends AuthInterceptor {
Map<String, String> apiKeys = {};

@override
FutureOr<void> before(RouteBase route) {
final authInfo = getAuthInfo(route, "apiKey");
for (var info in authInfo) {
final authName = info["name"];
final authKeyName = info["keyName"];
final authWhere = info["where"];
final apiKey = apiKeys[authName];
if(apiKey != null) {
if(authWhere == 'query'){
route.query(authKeyName, apiKey);
}
else {
route.header(authKeyName, apiKey);
}
break;
}
}
return super.before(route);
}

@override
FutureOr after(StringResponse response) {
return Future.value(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'dart:async';

import 'package:jaguar_retrofit/jaguar_retrofit.dart';

abstract class AuthInterceptor extends Interceptor {
/*
* Get auth information on given route for the given type
* Can return null if type is not present on auth data or if route doesn't need authentication
*/
List<Map<String, dynamic>> getAuthInfo(RouteBase route, String type) {
if (route.metadataMap.containsKey("auth")) {
final auth = route.metadataMap["auth"];
List<Map<String, dynamic>> results = [];
for (var info in auth) {
if(info["type"] == type) {
results.add(info);
}
}
return results;
}
return [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'dart:async';
import 'package:{{pubName}}/auth/auth.dart';
import 'package:jaguar_retrofit/jaguar_retrofit.dart';

class BasicAuthInfo {
final String username;
final String password;
const BasicAuthInfo(this.username, this.password);
}

class BasicAuthInterceptor extends AuthInterceptor {
Map<String, BasicAuthInfo> authInfo = {};

@override
FutureOr<void> before(RouteBase route) {
final metadataAuthInfo = getAuthInfo(route, "basic");
for (var info in metadataAuthInfo) {
final authName = info["name"];
final basicAuthInfo = authInfo[authName];
if(basicAuthInfo != null) {
route.basicAuth(basicAuthInfo.username, basicAuthInfo.password);
break;
}
}
return super.before(route);
}

@override
FutureOr after(StringResponse response) {
return Future.value(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'dart:async';
import 'package:{{pubName}}/auth/auth.dart';
import 'package:jaguar_retrofit/jaguar_retrofit.dart';

class OAuthInterceptor extends AuthInterceptor {
Map<String, String> tokens = {};

@override
FutureOr<void> before(RouteBase route) {
final authInfo = getAuthInfo(route, "oauth");
for (var info in authInfo) {
final token = tokens[info["name"]];
if(token != null) {
route.header("Authorization", "Bearer ${token}");
break;
}
}
return super.before(route);
}

@override
FutureOr after(StringResponse response) {
return Future.value(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ description: {{pubDescription}}
environment:
sdk: ">=2.0.0 <3.0.0"
dependencies:
jaguar_retrofit: '^2.4.1'
jaguar_serializer: '^2.2.0'
jaguar_retrofit: '^2.5.4'
jaguar_serializer: '^2.2.2'
dev_dependencies:
jaguar_retrofit_gen: '^2.4.2'
jaguar_retrofit_gen: '^2.5.1'
jaguar_serializer_cli: '^2.2.1'
build_runner: '^0.10.0'
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,34 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

--------------------------------------------------------------------------------
auth_header

Copyright (c) 2016, Ravi Teja Gudapati.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

--------------------------------------------------------------------------------
boolean_selector
front_end
Expand Down Expand Up @@ -3164,6 +3192,34 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

--------------------------------------------------------------------------------
client_cookie

Copyright (c) 2017, teja.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

--------------------------------------------------------------------------------
colorama

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

--------------------------------------------------------------------------------
jaguar_resty

BSD 3-Clause License

Copyright (c) 2017, Ravi Teja Gudapati <[email protected]>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------
jaguar_retrofit

Copyright (c) 2016, Jaguar Authors.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Jaguar Authors nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Jaguar Authors BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

--------------------------------------------------------------------------------
js

Expand Down
Binary file not shown.
Loading

0 comments on commit 9a19db7

Please sign in to comment.