Skip to content

Commit

Permalink
Add Bearer authentication support to JS client (#2020)
Browse files Browse the repository at this point in the history
* add bearer authentication support to js client

* add bearer format to js client

* fix test case

* update php ze-ph samples
  • Loading branch information
wing328 authored Jan 31, 2019
1 parent cbda3fa commit 6801741
Show file tree
Hide file tree
Showing 53 changed files with 2,797 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1559,8 +1559,8 @@ public String toModelName(final String name) {
/**
* Convert OAS Model object to Codegen Model object
*
* @param name the name of the model
* @param schema OAS Model object
* @param name the name of the model
* @param schema OAS Model object
* @return Codegen Model object
*/
public CodegenModel fromModel(String name, Schema schema) {
Expand Down Expand Up @@ -1993,7 +1993,7 @@ public CodegenProperty fromProperty(String name, Schema p) {
if (property.minimum != null || property.maximum != null)
property.hasValidation = true;

} else if (ModelUtils.isFreeFormObject(p)){
} else if (ModelUtils.isFreeFormObject(p)) {
property.isFreeFormObject = true;
}

Expand All @@ -2005,7 +2005,7 @@ public CodegenProperty fromProperty(String name, Schema p) {
property._enum.add(String.valueOf(i));
}
property.isEnum = true;

Map<String, Object> allowableValues = new HashMap<String, Object>();
allowableValues.put("values", _enum);
if (allowableValues.size() > 0) {
Expand Down Expand Up @@ -2273,6 +2273,7 @@ protected ApiResponse findMethodResponse(ApiResponses responses) {

/**
* Convert OAS Operation object to Codegen Operation object
*
* @param httpMethod HTTP method
* @param operation OAS operation object
* @param path the path of the operation
Expand Down Expand Up @@ -2587,9 +2588,9 @@ public boolean isParameterNameUnique(CodegenParameter p, List<CodegenParameter>

/**
* Convert OAS Response object to Codegen Response object
*
* @param responseCode HTTP response code
* @param response OAS Response object
*
* @return Codegen Response object
*/
public CodegenResponse fromResponse(String responseCode, ApiResponse response) {
Expand Down Expand Up @@ -3121,8 +3122,7 @@ public List<CodegenSecurity> fromSecurity(Map<String, SecurityScheme> securitySc
cs.isBasic = true;
if ("basic".equals(securityScheme.getScheme())) {
cs.isBasicBasic = true;
}
else if ("bearer".equals(securityScheme.getScheme())) {
} else if ("bearer".equals(securityScheme.getScheme())) {
cs.isBasicBearer = true;
cs.bearerFormat = securityScheme.getBearerFormat();
}
Expand Down Expand Up @@ -3250,6 +3250,7 @@ protected List<Map<String, Object>> toExamples(Map<String, Object> examples) {

/**
* Add headers to codegen property
*
* @param response API response
* @param properties list of codegen property
*/
Expand Down Expand Up @@ -3366,8 +3367,8 @@ protected void addImport(CodegenModel m, String type) {

/**
* Loop through propertiies and unalias the reference if $ref (reference) is defined
* @param properties model properties (schemas)
*
* @param properties model properties (schemas)
* @return model properties with direct reference to schemas
*/
private Map<String, Schema> unaliasPropertySchema(Map<String, Schema> properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@
{{/emitJSDoc}}{{=< >=}} this.authentications = {
<#authMethods>
<#isBasic>
<^isBasicBearer>
'<name>': {type: 'basic'}<^-last>,</-last>
</isBasicBearer>
<#isBasicBearer>
'<name>': {type: 'bearer'}<^-last>,</-last><#bearerFormat> // <&.></bearerFormat>
</isBasicBearer>
</isBasic>
<#isApiKey>
'<name>': {type: 'apiKey', 'in': <#isKeyInHeader>'header'</isKeyInHeader><^isKeyInHeader>'query'</isKeyInHeader>, name: '<keyParamName>'}<^-last>,</-last>
Expand Down Expand Up @@ -303,6 +308,11 @@
request.auth(auth.username || '', auth.password || '');
}
break;
case 'bearer':
if (auth.accessToken) {
request.set({'Authorization': 'Bearer ' + auth.accessToken});
}
break;
case 'apiKey':
if (auth.apiKey) {
var data = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,35 @@ Please follow the [installation](#installation) instruction and execute the foll

```javascript
var {{{moduleName}}} = require('{{{projectName}}}');
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}{{#hasAuthMethods}}
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}
{{#hasAuthMethods}}
var defaultClient = {{{moduleName}}}.ApiClient.instance;
{{#authMethods}}{{#isBasic}}
{{#authMethods}}
{{#isBasic}}
{{^isBasicBearer}}
// Configure HTTP basic authorization: {{{name}}}
var {{{name}}} = defaultClient.authentications['{{{name}}}'];
{{{name}}}.username = 'YOUR USERNAME'
{{{name}}}.password = 'YOUR PASSWORD'{{/isBasic}}{{#isApiKey}}
{{{name}}}.password = 'YOUR PASSWORD'
{{/isBasicBearer}}
{{#isBasicBearer}}
// Configure Bearer{{#bearerFormat}} ({{{.}}}){{/bearerFormat}} access token for authorization: {{{name}}}
var {{{name}}} = defaultClient.authentications['{{{name}}}'];
{{{name}}}.accessToken = "YOUR ACCESS TOKEN"
{{/isBasicBearer}}
{{/isBasic}}
{{#isApiKey}}
// Configure API key authorization: {{{name}}}
var {{{name}}} = defaultClient.authentications['{{{name}}}'];
{{{name}}}.apiKey = "YOUR API KEY"
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//{{{name}}}.apiKeyPrefix['{{{keyParamName}}}'] = "Token"{{/isApiKey}}{{#isOAuth}}
//{{{name}}}.apiKeyPrefix['{{{keyParamName}}}'] = "Token"
{{/isApiKey}}
{{#isOAuth}}
// Configure OAuth2 access token for authorization: {{{name}}}
var {{{name}}} = defaultClient.authentications['{{{name}}}'];
{{{name}}}.accessToken = "YOUR ACCESS TOKEN"{{/isOAuth}}
{{{name}}}.accessToken = "YOUR ACCESS TOKEN"
{{/isOAuth}}
{{/authMethods}}
{{/hasAuthMethods}}

Expand Down Expand Up @@ -170,15 +184,24 @@ Class | Method | HTTP request | Description

## Documentation for Authorization

{{^authMethods}} All endpoints do not require authorization.
{{/authMethods}}{{#authMethods}}{{#last}} Authentication schemes defined for the API:{{/last}}{{/authMethods}}
{{#authMethods}}### {{name}}
{{^authMethods}}
All endpoints do not require authorization.
{{/authMethods}}
{{#authMethods}}
{{#last}} Authentication schemes defined for the API:{{/last}}
### {{name}}

{{#isApiKey}}- **Type**: API key
- **API key parameter name**: {{keyParamName}}
- **Location**: {{#isKeyInQuery}}URL query string{{/isKeyInQuery}}{{#isKeyInHeader}}HTTP header{{/isKeyInHeader}}
{{/isApiKey}}
{{#isBasic}}- **Type**: HTTP basic authentication
{{#isBasic}}
{{^isBasicBearer}}
- **Type**: HTTP basic authentication
{{/isBasicBearer}}
{{#isBasicBearer}}
- **Type**: Bearer authentication{{#bearerFormat}} ({{{.}}}){{/bearerFormat}}
{{/isBasicBearer}}
{{/isBasic}}
{{#isOAuth}}- **Type**: OAuth
- **Flow**: {{flow}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ Method | HTTP request | Description
var {{{moduleName}}} = require('{{{projectName}}}');
{{#hasAuthMethods}}
var defaultClient = {{{moduleName}}}.ApiClient.instance;
{{#authMethods}}{{#isBasic}}
{{#authMethods}}
{{#isBasic}}
{{^isBasicBearer}}
// Configure HTTP basic authorization: {{{name}}}
var {{{name}}} = defaultClient.authentications['{{{name}}}'];
{{{name}}}.username = 'YOUR USERNAME';
{{{name}}}.password = 'YOUR PASSWORD';
{{/isBasicBearer}}
{{#isBasicBearer}}
// Configure Bearer{{#bearerFormat}} ({{{.}}}){{/bearerFormat}} access token for authorization: {{{name}}}
var {{{name}}} = defaultClient.authentications['{{{name}}}'];
{{{name}}}.accessToken = 'YOUR ACCESS TOKEN';
{{/isBasicBearer}}
{{/isBasic}}
{{#isApiKey}}
// Configure API key authorization: {{{name}}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ class ApiClient {
this.authentications = {
<#authMethods>
<#isBasic>
<^isBasicBearer>
'<name>': {type: 'basic'}<^-last>,</-last>
</isBasicBearer>
<#isBasicBearer>
'<name>': {type: 'bearer'}<^-last>,</-last><#bearerFormat> // <&.></bearerFormat>
</isBasicBearer>
</isBasic>
<#isApiKey>
'<name>': {type: 'apiKey', 'in': <#isKeyInHeader>'header'</isKeyInHeader><^isKeyInHeader>'query'</isKeyInHeader>, name: '<keyParamName>'}<^-last>,</-last>
Expand Down Expand Up @@ -268,6 +273,12 @@ class ApiClient {
request.auth(auth.username || '', auth.password || '');
}

break;
case 'bearer':
if (auth.accessToken) {
request.set({'Authorization': 'Bearer ' + auth.accessToken});
}

break;
case 'apiKey':
if (auth.apiKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,17 @@ var {{{moduleName}}} = require('{{{projectName}}}');
var defaultClient = {{{moduleName}}}.ApiClient.instance;
{{#authMethods}}
{{#isBasic}}
{{^isBasicBearer}}
// Configure HTTP basic authorization: {{{name}}}
var {{{name}}} = defaultClient.authentications['{{{name}}}'];
{{{name}}}.username = 'YOUR USERNAME'
{{{name}}}.password = 'YOUR PASSWORD'
{{/isBasicBearer}}
{{#isBasicBearer}}
// Configure Bearer{{#bearerFormat}} ({{{.}}}){{/bearerFormat}} access token for authorization: {{{name}}}
var {{{name}}} = defaultClient.authentications['{{{name}}}'];
{{{name}}}.accessToken = "YOUR ACCESS TOKEN"
{{/isBasicBearer}}
{{/isBasic}}
{{#isApiKey}}
// Configure API key authorization: {{{name}}}
Expand Down Expand Up @@ -154,15 +161,25 @@ Class | Method | HTTP request | Description

## Documentation for Authorization

{{^authMethods}} All endpoints do not require authorization.
{{/authMethods}}{{#authMethods}}{{#last}} Authentication schemes defined for the API:{{/last}}{{/authMethods}}
{{#authMethods}}### {{name}}
{{^authMethods}}
All endpoints do not require authorization.
{{/authMethods}}
{{#authMethods}}
{{#last}} Authentication schemes defined for the API:{{/last}}
### {{name}}

{{#isApiKey}}- **Type**: API key
{{#isApiKey}}
- **Type**: API key
- **API key parameter name**: {{keyParamName}}
- **Location**: {{#isKeyInQuery}}URL query string{{/isKeyInQuery}}{{#isKeyInHeader}}HTTP header{{/isKeyInHeader}}
{{/isApiKey}}
{{#isBasic}}- **Type**: HTTP basic authentication
{{#isBasic}}
{{^isBasicBearer}}
- **Type**: HTTP basic authentication
{{/isBasicBearer}}
{{#isBasicBearer}}
- **Type**: Bearer authentication{{#bearerFormat}} ({{{.}}}){{/bearerFormat}}
{{/isBasicBearer}}
{{/isBasic}}
{{#isOAuth}}- **Type**: OAuth
- **Flow**: {{flow}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,32 @@ Method | HTTP request | Description
import {{{moduleName}}} from '{{{projectName}}}';
{{#hasAuthMethods}}
let defaultClient = {{{moduleName}}}.ApiClient.instance;
{{#authMethods}}{{#isBasic}}
{{#authMethods}}
{{#isBasic}}
{{^isBasicBearer}}
// Configure HTTP basic authorization: {{{name}}}
let {{{name}}} = defaultClient.authentications['{{{name}}}'];
{{{name}}}.username = 'YOUR USERNAME';
{{{name}}}.password = 'YOUR PASSWORD';{{/isBasic}}{{#isApiKey}}
{{{name}}}.password = 'YOUR PASSWORD';
{{/isBasicBearer}}
{{#isBasicBearer}}
// Configure Bearer{{#bearerFormat}} ({{{.}}}){{/bearerFormat}} access token for authorization: {{{name}}}
let {{{name}}} = defaultClient.authentications['{{{name}}}'];
{{{name}}}.accessToken = "YOUR ACCESS TOKEN"
{{/isBasicBearer}}
{{/isBasic}}
{{#isApiKey}}
// Configure API key authorization: {{{name}}}
let {{{name}}} = defaultClient.authentications['{{{name}}}'];
{{{name}}}.apiKey = 'YOUR API KEY';
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//{{{name}}}.apiKeyPrefix = 'Token';{{/isApiKey}}{{#isOAuth}}
//{{{name}}}.apiKeyPrefix = 'Token';
{{/isApiKey}}
{{#isOAuth}}
// Configure OAuth2 access token for authorization: {{{name}}}
let {{{name}}} = defaultClient.authentications['{{{name}}}'];
{{{name}}}.accessToken = 'YOUR ACCESS TOKEN';{{/isOAuth}}
{{{name}}}.accessToken = 'YOUR ACCESS TOKEN';
{{/isOAuth}}
{{/authMethods}}
{{/hasAuthMethods}}

Expand Down
5 changes: 5 additions & 0 deletions samples/client/petstore/javascript-es6/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ All URIs are relative to *http://petstore.swagger.io:80/v2*
Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
*OpenApiPetstore.AnotherFakeApi* | [**call123testSpecialTags**](docs/AnotherFakeApi.md#call123testSpecialTags) | **PATCH** /another-fake/dummy | To test special tags
*OpenApiPetstore.FakeApi* | [**createXmlItem**](docs/FakeApi.md#createXmlItem) | **POST** /fake/create_xml_item | creates an XmlItem
*OpenApiPetstore.FakeApi* | [**fakeOuterBooleanSerialize**](docs/FakeApi.md#fakeOuterBooleanSerialize) | **POST** /fake/outer/boolean |
*OpenApiPetstore.FakeApi* | [**fakeOuterCompositeSerialize**](docs/FakeApi.md#fakeOuterCompositeSerialize) | **POST** /fake/outer/composite |
*OpenApiPetstore.FakeApi* | [**fakeOuterNumberSerialize**](docs/FakeApi.md#fakeOuterNumberSerialize) | **POST** /fake/outer/number |
Expand Down Expand Up @@ -165,6 +166,7 @@ Class | Method | HTTP request | Description
- [OpenApiPetstore.TypeHolderDefault](docs/TypeHolderDefault.md)
- [OpenApiPetstore.TypeHolderExample](docs/TypeHolderExample.md)
- [OpenApiPetstore.User](docs/User.md)
- [OpenApiPetstore.XmlItem](docs/XmlItem.md)


## Documentation for Authorization
Expand All @@ -176,16 +178,19 @@ Class | Method | HTTP request | Description
- **API key parameter name**: api_key
- **Location**: HTTP header


### api_key_query

- **Type**: API key
- **API key parameter name**: api_key_query
- **Location**: URL query string


### http_basic_test

- **Type**: HTTP basic authentication


### petstore_auth

- **Type**: OAuth
Expand Down
Loading

0 comments on commit 6801741

Please sign in to comment.