Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added bearer auth
  • Loading branch information
gasugesu committed Apr 1, 2020
commit 270e39fc772060c414ecbd1e11b33671bb6ea2df
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,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
11 changes: 11 additions & 0 deletions modules/openapi-generator/src/main/resources/dart2/README.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ import 'package:{{pubName}}/api.dart';
//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 @@ -116,6 +124,9 @@ Class | Method | HTTP request | Description
{{#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 @@ -26,6 +26,9 @@ class ApiClient {
{{#isBasicBasic}}
_authentications['{{name}}'] = HttpBasicAuth();
{{/isBasicBasic}}
{{#isBasicBearer}}
_authentications['{{name}}'] = HttpBearerAuth();
{{/isBasicBearer}}
{{/isBasic}}
{{#isApiKey}}
_authentications['{{name}}'] = ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ import 'package:{{pubName}}/api.dart';
//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;
}
}