Skip to content

Commit

Permalink
[Dart] Fix "basic" auth method and Add Bearer token support (OpenAPIT…
Browse files Browse the repository at this point in the history
…ools#5743)

* added auth check and lint

* fixed basic auth condition

* Added bearer auth

* updated samples

* update dart petstore samples

Co-authored-by: William Cheng <wing328hk@gmail.com>
  • Loading branch information
2 people authored and MikailBag committed May 31, 2020
1 parent cd016a6 commit 01712af
Show file tree
Hide file tree
Showing 16 changed files with 249 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ public void processOpts() {
final String authFolder = sourceFolder + File.separator + "lib" + File.separator + "auth";
supportingFiles.add(new SupportingFile("auth/authentication.mustache", authFolder, "authentication.dart"));
supportingFiles.add(new SupportingFile("auth/http_basic_auth.mustache", authFolder, "http_basic_auth.dart"));
supportingFiles.add(new SupportingFile("auth/http_bearer_auth.mustache", authFolder, "http_bearer_auth.dart"));
supportingFiles.add(new SupportingFile("auth/api_key_auth.mustache", authFolder, "api_key_auth.dart"));
supportingFiles.add(new SupportingFile("auth/oauth.mustache", authFolder, "oauth.dart"));
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,19 @@ import 'package:{{pubName}}/api.dart';
{{#hasAuthMethods}}
{{#authMethods}}
{{#isBasic}}
{{#isBasicBasic}}
// TODO Configure HTTP basic authorization: {{{name}}}
//defaultApiClient.getAuthentication<HttpBasicAuth>('{{{name}}}').username = 'YOUR_USERNAME'
//defaultApiClient.getAuthentication<HttpBasicAuth>('{{{name}}}').password = 'YOUR_PASSWORD';
{{/isBasicBasic}}
{{#isBasicBearer}}
// TODO Configure HTTP Bearer authorization: {{{name}}}
// Case 1. Use String Token
//defaultApiClient.getAuthentication<HttpBearerAuth>('{{{name}}}').setAccessToken('YOUR_ACCESS_TOKEN');
// Case 2. Use Function which generate token.
// String yourTokenGeneratorFunction() { ... }
//defaultApiClient.getAuthentication<HttpBearerAuth>('{{{name}}}').setAccessToken(yourTokenGeneratorFunction);
{{/isBasicBearer}}
{{/isBasic}}
{{#isApiKey}}
// TODO Configure API key authorization: {{{name}}}
Expand Down Expand Up @@ -110,7 +120,13 @@ Class | Method | HTTP request | Description
- **API key parameter name**: {{{keyParamName}}}
- **Location**: {{#isKeyInQuery}}URL query string{{/isKeyInQuery}}{{#isKeyInHeader}}HTTP header{{/isKeyInHeader}}
{{/isApiKey}}
{{#isBasic}}- **Type**: HTTP basic authentication
{{#isBasic}}
{{#isBasicBasic}}
- **Type**: HTTP basicc authentication
{{/isBasicBasic}}
{{#isBasicBearer}}
- **Type**: HTTP Bearer authentication
{{/isBasicBearer}}
{{/isBasic}}
{{#isOAuth}}- **Type**: OAuth
- **Flow**: {{{flow}}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,25 @@ class ApiClient {
final _regMap = RegExp(r'^Map<String,(.*)>$');

ApiClient({this.basePath = "{{{basePath}}}"}) {
// Setup authentications (key: authentication name, value: authentication).{{#authMethods}}{{#isBasic}}
_authentications['{{name}}'] = HttpBasicAuth();{{/isBasic}}{{#isApiKey}}
_authentications['{{name}}'] = ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}");{{/isApiKey}}{{#isOAuth}}
_authentications['{{name}}'] = OAuth();{{/isOAuth}}{{/authMethods}}
{{#hasAuthMethods}}
// Setup authentications (key: authentication name, value: authentication).
{{#authMethods}}
{{#isBasic}}
{{#isBasicBasic}}
_authentications['{{name}}'] = HttpBasicAuth();
{{/isBasicBasic}}
{{#isBasicBearer}}
_authentications['{{name}}'] = HttpBearerAuth();
{{/isBasicBearer}}
{{/isBasic}}
{{#isApiKey}}
_authentications['{{name}}'] = ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}");
{{/isApiKey}}
{{#isOAuth}}
_authentications['{{name}}'] = OAuth();
{{/isOAuth}}
{{/authMethods}}
{{/hasAuthMethods}}
}

void addDefaultHeader(String key, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,19 @@ import 'package:{{pubName}}/api.dart';
{{#hasAuthMethods}}
{{#authMethods}}
{{#isBasic}}
{{#isBasicBasic}}
// TODO Configure HTTP basic authorization: {{{name}}}
//defaultApiClient.getAuthentication<HttpBasicAuth>('{{{name}}}').username = 'YOUR_USERNAME'
//defaultApiClient.getAuthentication<HttpBasicAuth>('{{{name}}}').password = 'YOUR_PASSWORD';
{{/isBasicBasic}}
{{#isBasicBearer}}
// TODO Configure HTTP Bearer authorization: {{{name}}}
// Case 1. Use String Token
//defaultApiClient.getAuthentication<HttpBearerAuth>('{{{name}}}').setAccessToken('YOUR_ACCESS_TOKEN');
// Case 2. Use Function which generate token.
// String yourTokenGeneratorFunction() { ... }
//defaultApiClient.getAuthentication<HttpBearerAuth>('{{{name}}}').setAccessToken(yourTokenGeneratorFunction);
{{/isBasicBearer}}
{{/isBasic}}
{{#isApiKey}}
// TODO Configure API key authorization: {{{name}}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ part 'auth/authentication.dart';
part 'auth/api_key_auth.dart';
part 'auth/oauth.dart';
part 'auth/http_basic_auth.dart';
part 'auth/http_bearer_auth.dart';

{{#apiInfo}}{{#apis}}part 'api/{{classFilename}}.dart';
{{/apis}}{{/apiInfo}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
part of {{pubName}}.api;

class HttpBearerAuth implements Authentication {
dynamic _accessToken;
HttpBearerAuth() { }

@override
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams) {
if (_accessToken is String) {
headerParams["Authorization"] = "Bearer " + _accessToken;
} else if (_accessToken is String Function()){
headerParams["Authorization"] = "Bearer " + _accessToken();
} else {
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
}
}

void setAccessToken(dynamic accessToken) {
if (!((accessToken is String) | (accessToken is String Function()))){
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
}
this._accessToken = accessToken;
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.3.0-SNAPSHOT
4.3.1-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# openapi.model.InlineObject

## Load the model package
```dart
import 'package:openapi/api.dart';
```

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**name** | **String** | Updated name of the pet | [optional] [default to null]
**status** | **String** | Updated status of the pet | [optional] [default to null]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# openapi.model.InlineObject1

## Load the model package
```dart
import 'package:openapi/api.dart';
```

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**additionalMetadata** | **String** | Additional data to pass to server | [optional] [default to null]
**file** | [**MultipartFile**](File.md) | file to upload | [optional] [default to null]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
part of openapi.api;

class HttpBearerAuth implements Authentication {
dynamic _accessToken;

HttpBearerAuth() { }

@override
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams) {
if (_accessToken is String) {
headerParams["Authorization"] = "Bearer " + _accessToken;
} else if (_accessToken is String Function()){
headerParams["Authorization"] = "Bearer " + _accessToken();
} else {
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
}
}

void setAccessToken(dynamic accessToken) {
if (!((accessToken is String) | (accessToken is String Function()))){
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
}
this._accessToken = accessToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ part of openapi.api;

class InlineObject {
/* Updated name of the pet */
String name = null;
String name = null;
/* Updated status of the pet */
String status = null;
String status = null;
InlineObject();

@override
Expand All @@ -14,27 +14,33 @@ class InlineObject {

InlineObject.fromJson(Map<String, dynamic> json) {
if (json == null) return;
name = json['name'];
status = json['status'];
if (json['name'] == null) {
name = null;
} else {
name = json['name'];
}
if (json['status'] == null) {
status = null;
} else {
status = json['status'];
}
}

Map<String, dynamic> toJson() {
Map <String, dynamic> json = {};
if (name != null)
json['name'] = name;
if (status != null)
json['status'] = status;
return json;
return {
'name': name,
'status': status
};
}

static List<InlineObject> listFromJson(List<dynamic> json) {
return json == null ? List<InlineObject>() : json.map((value) => InlineObject.fromJson(value)).toList();
return json == null ? new List<InlineObject>() : json.map((value) => new InlineObject.fromJson(value)).toList();
}

static Map<String, InlineObject> mapFromJson(Map<String, dynamic> json) {
var map = Map<String, InlineObject>();
if (json != null && json.isNotEmpty) {
json.forEach((String key, dynamic value) => map[key] = InlineObject.fromJson(value));
static Map<String, InlineObject> mapFromJson(Map<String, Map<String, dynamic>> json) {
var map = new Map<String, InlineObject>();
if (json != null && json.length > 0) {
json.forEach((String key, Map<String, dynamic> value) => map[key] = new InlineObject.fromJson(value));
}
return map;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ part of openapi.api;

class InlineObject1 {
/* Additional data to pass to server */
String additionalMetadata = null;
String additionalMetadata = null;
/* file to upload */
MultipartFile file = null;
MultipartFile file = null;
InlineObject1();

@override
Expand All @@ -14,29 +14,33 @@ class InlineObject1 {

InlineObject1.fromJson(Map<String, dynamic> json) {
if (json == null) return;
additionalMetadata = json['additionalMetadata'];
file = (json['file'] == null) ?
null :
File.fromJson(json['file']);
if (json['additionalMetadata'] == null) {
additionalMetadata = null;
} else {
additionalMetadata = json['additionalMetadata'];
}
if (json['file'] == null) {
file = null;
} else {
file = new File.fromJson(json['file']);
}
}

Map<String, dynamic> toJson() {
Map <String, dynamic> json = {};
if (additionalMetadata != null)
json['additionalMetadata'] = additionalMetadata;
if (file != null)
json['file'] = file;
return json;
return {
'additionalMetadata': additionalMetadata,
'file': file
};
}

static List<InlineObject1> listFromJson(List<dynamic> json) {
return json == null ? List<InlineObject1>() : json.map((value) => InlineObject1.fromJson(value)).toList();
return json == null ? new List<InlineObject1>() : json.map((value) => new InlineObject1.fromJson(value)).toList();
}

static Map<String, InlineObject1> mapFromJson(Map<String, dynamic> json) {
var map = Map<String, InlineObject1>();
if (json != null && json.isNotEmpty) {
json.forEach((String key, dynamic value) => map[key] = InlineObject1.fromJson(value));
static Map<String, InlineObject1> mapFromJson(Map<String, Map<String, dynamic>> json) {
var map = new Map<String, InlineObject1>();
if (json != null && json.length > 0) {
json.forEach((String key, Map<String, dynamic> value) => map[key] = new InlineObject1.fromJson(value));
}
return map;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
part of openapi.api;

class HttpBearerAuth implements Authentication {
dynamic _accessToken;

HttpBearerAuth() { }

@override
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams) {
if (_accessToken is String) {
headerParams["Authorization"] = "Bearer " + _accessToken;
} else if (_accessToken is String Function()){
headerParams["Authorization"] = "Bearer " + _accessToken();
} else {
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
}
}

void setAccessToken(dynamic accessToken) {
if (!((accessToken is String) | (accessToken is String Function()))){
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
}
this._accessToken = accessToken;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
part of openapi.api;

class HttpBearerAuth implements Authentication {
dynamic _accessToken;

HttpBearerAuth() { }

@override
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams) {
if (_accessToken is String) {
headerParams["Authorization"] = "Bearer " + _accessToken;
} else if (_accessToken is String Function()){
headerParams["Authorization"] = "Bearer " + _accessToken();
} else {
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
}
}

void setAccessToken(dynamic accessToken) {
if (!((accessToken is String) | (accessToken is String Function()))){
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
}
this._accessToken = accessToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ part 'auth/authentication.dart';
part 'auth/api_key_auth.dart';
part 'auth/oauth.dart';
part 'auth/http_basic_auth.dart';
part 'auth/http_bearer_auth.dart';

part 'api/pet_api.dart';
part 'api/store_api.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
part of openapi.api;

class HttpBearerAuth implements Authentication {
dynamic _accessToken;

HttpBearerAuth() { }

@override
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams) {
if (_accessToken is String) {
headerParams["Authorization"] = "Bearer " + _accessToken;
} else if (_accessToken is String Function()){
headerParams["Authorization"] = "Bearer " + _accessToken();
} else {
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
}
}

void setAccessToken(dynamic accessToken) {
if (!((accessToken is String) | (accessToken is String Function()))){
throw ArgumentError('Type of Bearer accessToken should be String or String Function().');
}
this._accessToken = accessToken;
}
}

0 comments on commit 01712af

Please sign in to comment.