mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2026-03-16 13:39:11 +00:00
Add Bearer authentication support to Python client (#1999)
* add bearer auth support to python * add bearer auth support to python * update python oas2 petstore samples * update samples * add bearer format * update php symfony samle
This commit is contained in:
@@ -18,8 +18,23 @@ Method | HTTP request | Description
|
||||
{{{notes}}}{{/notes}}
|
||||
|
||||
### Example
|
||||
{{#hasAuthMethods}}{{#authMethods}}
|
||||
{{#isBasic}}* Basic Authentication ({{name}}): {{/isBasic }}{{#isApiKey}}* Api Key Authentication ({{name}}): {{/isApiKey }}{{#isOAuth}}* OAuth Authentication ({{name}}): {{/isOAuth }}
|
||||
|
||||
{{#hasAuthMethods}}
|
||||
{{#authMethods}}
|
||||
{{#isBasic}}
|
||||
{{^isBasicBearer}}
|
||||
* Basic Authentication ({{name}}):
|
||||
{{/isBasicBearer}}
|
||||
{{#isBasicBearer}}
|
||||
* Bearer{{#bearerFormat}} ({{{.}}}){{/bearerFormat}} Authentication ({{name}}):
|
||||
{{/isBasicBearer}}
|
||||
{{/isBasic}}
|
||||
{{#isApiKey}}
|
||||
* Api Key Authentication ({{name}}):
|
||||
{{/isApiKey }}
|
||||
{{#isOAuth}}
|
||||
* OAuth Authentication ({{name}}):
|
||||
{{/isOAuth }}
|
||||
{{> api_doc_example }}
|
||||
{{/authMethods}}
|
||||
{{/hasAuthMethods}}
|
||||
|
||||
@@ -4,25 +4,14 @@ import time
|
||||
import {{{packageName}}}
|
||||
from {{{packageName}}}.rest import ApiException
|
||||
from pprint import pprint
|
||||
{{> python_doc_auth_partial}}
|
||||
{{#hasAuthMethods}}
|
||||
configuration = {{{packageName}}}.Configuration(){{#isBasic}}
|
||||
# Configure HTTP basic authorization: {{{name}}}
|
||||
configuration.username = 'YOUR_USERNAME'
|
||||
configuration.password = 'YOUR_PASSWORD'{{/isBasic}}{{#isApiKey}}
|
||||
# Configure API key authorization: {{{name}}}
|
||||
configuration.api_key['{{{keyParamName}}}'] = 'YOUR_API_KEY'
|
||||
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
|
||||
# configuration.api_key_prefix['{{{keyParamName}}}'] = 'Bearer'{{/isApiKey}}{{#isOAuth}}
|
||||
# Configure OAuth2 access token for authorization: {{{name}}}
|
||||
configuration.access_token = 'YOUR_ACCESS_TOKEN'{{/isOAuth}}
|
||||
|
||||
# create an instance of the API class
|
||||
api_instance = {{{packageName}}}.{{{classname}}}({{{packageName}}}.ApiClient(configuration))
|
||||
{{#allParams}}{{paramName}} = {{{example}}} # {{{dataType}}} | {{{description}}}{{^required}} (optional){{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
|
||||
{{/allParams}}
|
||||
{{/hasAuthMethods}}
|
||||
{{^hasAuthMethods}}
|
||||
|
||||
# create an instance of the API class
|
||||
api_instance = {{{packageName}}}.{{{classname}}}()
|
||||
{{#allParams}}{{paramName}} = {{{example}}} # {{{dataType}}} | {{{description}}}{{^required}} (optional){{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
|
||||
|
||||
@@ -4,19 +4,8 @@ import time
|
||||
import {{{packageName}}}
|
||||
from {{{packageName}}}.rest import ApiException
|
||||
from pprint import pprint
|
||||
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}{{#hasAuthMethods}}{{#authMethods}}
|
||||
configuration = {{{packageName}}}.Configuration(){{#isBasic}}
|
||||
# Configure HTTP basic authorization: {{{name}}}
|
||||
configuration.username = 'YOUR_USERNAME'
|
||||
configuration.password = 'YOUR_PASSWORD'{{/isBasic}}{{#isApiKey}}
|
||||
# Configure API key authorization: {{{name}}}
|
||||
configuration.api_key['{{{keyParamName}}}'] = 'YOUR_API_KEY'
|
||||
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
|
||||
# configuration.api_key_prefix['{{{keyParamName}}}'] = 'Bearer'{{/isApiKey}}{{#isOAuth}}
|
||||
# Configure OAuth2 access token for authorization: {{{name}}}
|
||||
configuration.access_token = 'YOUR_ACCESS_TOKEN'{{/isOAuth}}{{/authMethods}}
|
||||
{{/hasAuthMethods}}
|
||||
|
||||
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}
|
||||
{{> python_doc_auth_partial}}
|
||||
# create an instance of the API class
|
||||
api_instance = {{{packageName}}}.{{{classname}}}({{{packageName}}}.ApiClient(configuration))
|
||||
{{#allParams}}{{paramName}} = {{{example}}} # {{{dataType}}} | {{{description}}}{{^required}} (optional){{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
|
||||
@@ -47,17 +36,28 @@ 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
|
||||
{{#isOAuth}}
|
||||
- **Type**: OAuth
|
||||
- **Flow**: {{{flow}}}
|
||||
- **Authorization URL**: {{{authorizationUrl}}}
|
||||
- **Scopes**: {{^scopes}}N/A{{/scopes}}
|
||||
|
||||
@@ -51,10 +51,16 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
|
||||
self.username = ""
|
||||
# Password for HTTP basic authentication
|
||||
self.password = ""
|
||||
{{#authMethods}}{{#isOAuth}}
|
||||
# access token for OAuth
|
||||
{{#hasOAuthMethods}}
|
||||
# access token for OAuth/Bearer
|
||||
self.access_token = ""
|
||||
{{/isOAuth}}{{/authMethods}}
|
||||
{{/hasOAuthMethods}}
|
||||
{{^hasOAuthMethods}}
|
||||
{{#hasBearerMethods}}
|
||||
# access token for OAuth/Bearer
|
||||
self.access_token = ""
|
||||
{{/hasBearerMethods}}
|
||||
{{/hasOAuthMethods}}
|
||||
# Logging Settings
|
||||
self.logger = {}
|
||||
self.logger["package_logger"] = logging.getLogger("{{packageName}}")
|
||||
@@ -218,6 +224,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
|
||||
},
|
||||
{{/isApiKey}}
|
||||
{{#isBasic}}
|
||||
{{^isBasicBearer}}
|
||||
'{{name}}':
|
||||
{
|
||||
'type': 'basic',
|
||||
@@ -225,7 +232,21 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
|
||||
'key': 'Authorization',
|
||||
'value': self.get_basic_auth_token()
|
||||
},
|
||||
{{/isBasic}}{{#isOAuth}}
|
||||
{{/isBasicBearer}}
|
||||
{{#isBasicBearer}}
|
||||
'{{name}}':
|
||||
{
|
||||
'type': 'bearer',
|
||||
'in': 'header',
|
||||
{{#bearerFormat}}
|
||||
'format': '{{{.}}}',
|
||||
{{/bearerFormat}}
|
||||
'key': 'Authorization',
|
||||
'value': 'Bearer ' + self.access_token
|
||||
},
|
||||
{{/isBasicBearer}}
|
||||
{{/isBasic}}
|
||||
{{#isOAuth}}
|
||||
'{{name}}':
|
||||
{
|
||||
'type': 'oauth2',
|
||||
@@ -233,7 +254,8 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
|
||||
'key': 'Authorization',
|
||||
'value': 'Bearer ' + self.access_token
|
||||
},
|
||||
{{/isOAuth}}{{/authMethods}}
|
||||
{{/isOAuth}}
|
||||
{{/authMethods}}
|
||||
}
|
||||
|
||||
def to_debug_report(self):
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
{{#hasAuthMethods}}
|
||||
{{#authMethods}}
|
||||
configuration = {{{packageName}}}.Configuration()
|
||||
{{#isBasic}}
|
||||
{{^isBasicBearer}}
|
||||
# Configure HTTP basic authorization: {{{name}}}
|
||||
configuration.username = 'YOUR_USERNAME'
|
||||
configuration.password = 'YOUR_PASSWORD'
|
||||
{{/isBasicBearer}}
|
||||
{{#isBasicBearer}}
|
||||
# Configure Bearer authorization{{#bearerFormat}} ({{{.}}}){{/bearerFormat}}: {{{name}}}
|
||||
configuration.access_token = 'YOUR_BEARER_TOKEN'
|
||||
{{/isBasicBearer}}
|
||||
{{/isBasic}}
|
||||
{{#isApiKey}}
|
||||
# Configure API key authorization: {{{name}}}
|
||||
configuration.api_key['{{{keyParamName}}}'] = 'YOUR_API_KEY'
|
||||
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
|
||||
# configuration.api_key_prefix['{{{keyParamName}}}'] = 'Bearer'
|
||||
{{/isApiKey}}
|
||||
{{#isOAuth}}
|
||||
# Configure OAuth2 access token for authorization: {{{name}}}
|
||||
configuration.access_token = 'YOUR_ACCESS_TOKEN'
|
||||
{{/isOAuth}}
|
||||
{{/authMethods}}
|
||||
{{/hasAuthMethods}}
|
||||
Reference in New Issue
Block a user