forked from loafle/openapi-generator-original
[Dart] Fix "basic" auth method and Add Bearer token support (#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>
This commit is contained in:
parent
64c99504b4
commit
800293ccf9
@ -280,6 +280,7 @@ public class DartClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
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"));
|
||||
|
@ -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}}}
|
||||
@ -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}}}
|
||||
|
@ -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) {
|
||||
|
@ -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}}}
|
||||
|
@ -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}}
|
||||
|
25
modules/openapi-generator/src/main/resources/dart2/auth/http_bearer_auth.mustache
vendored
Normal file
25
modules/openapi-generator/src/main/resources/dart2/auth/http_bearer_auth.mustache
vendored
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -1 +1 @@
|
||||
4.3.0-SNAPSHOT
|
||||
4.3.1-SNAPSHOT
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -14,27 +14,33 @@ class InlineObject {
|
||||
|
||||
InlineObject.fromJson(Map<String, dynamic> json) {
|
||||
if (json == null) return;
|
||||
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;
|
||||
}
|
||||
|
@ -14,29 +14,33 @@ class InlineObject1 {
|
||||
|
||||
InlineObject1.fromJson(Map<String, dynamic> json) {
|
||||
if (json == null) return;
|
||||
if (json['additionalMetadata'] == null) {
|
||||
additionalMetadata = null;
|
||||
} else {
|
||||
additionalMetadata = json['additionalMetadata'];
|
||||
file = (json['file'] == null) ?
|
||||
null :
|
||||
File.fromJson(json['file']);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
@ -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,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;
|
||||
}
|
||||
}
|
@ -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';
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user