Skip to content

Commit d296a2d

Browse files
[Python] Python HTTP signature update (#5154)
* improve python documentation and add import to __init__package.mustache * improve python documentation and add import to __init__package.mustache * add signing_info parameter conditionally * add code comments and remove 'Content-Length' header from example * Remove debug log statement * set access_token to None by default * set access_token to None by default * fix unit tests for Python experimental * fix trailing space
1 parent 0df7216 commit d296a2d

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

configuration.mustache

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@ class Configuration(object):
3131
The dict value is an API key prefix when generating the auth data.
3232
:param username: Username for HTTP basic authentication
3333
:param password: Password for HTTP basic authentication
34-
:param signing_info: Configuration parameters for HTTP signature.
34+
{{#hasHttpSignatureMethods}}
35+
:param signing_info: Configuration parameters for the HTTP signature security scheme.
3536
Must be an instance of {{{packageName}}}.signing.HttpSigningConfiguration
37+
{{/hasHttpSignatureMethods}}
3638

39+
{{#hasAuthMethods}}
3740
:Example:
41+
{{#hasApiKeyMethods}}
3842

43+
API Key Authentication Example.
3944
Given the following security scheme in the OpenAPI specification:
4045
components:
4146
securitySchemes:
@@ -51,12 +56,32 @@ class Configuration(object):
5156
)
5257
The following cookie will be added to the HTTP request:
5358
Cookie: JSESSIONID abc123
59+
{{/hasApiKeyMethods}}
60+
{{#hasHttpBasicMethods}}
61+
62+
HTTP Basic Authentication Example.
63+
Given the following security scheme in the OpenAPI specification:
64+
components:
65+
securitySchemes:
66+
http_basic_auth:
67+
type: http
68+
scheme: basic
5469

5570
Configure API client with HTTP basic authentication:
5671
conf = {{{packageName}}}.Configuration(
5772
username='the-user',
5873
password='the-password',
5974
)
75+
{{/hasHttpBasicMethods}}
76+
{{#hasHttpSignatureMethods}}
77+
78+
HTTP Signature Authentication Example.
79+
Given the following security scheme in the OpenAPI specification:
80+
components:
81+
securitySchemes:
82+
http_basic_auth:
83+
type: http
84+
scheme: signature
6085

6186
Configure API client with HTTP signature authentication. Use the 'hs2019' signature scheme,
6287
sign the HTTP requests with the RSA-SSA-PSS signature algorithm, and set the expiration time
@@ -83,18 +108,22 @@ class Configuration(object):
83108
signing.HEADER_DATE,
84109
signing.HEADER_DIGEST,
85110
'Content-Type',
86-
'Content-Length',
87111
'User-Agent'
88112
],
89113
signature_max_validity = datetime.timedelta(minutes=5)
90114
)
91115
)
116+
{{/hasHttpSignatureMethods}}
117+
{{/hasAuthMethods}}
92118
"""
93119

94120
def __init__(self, host="{{{basePath}}}",
95121
api_key=None, api_key_prefix=None,
96122
username=None, password=None,
97-
signing_info=None):
123+
{{#hasHttpSignatureMethods}}
124+
signing_info=None,
125+
{{/hasHttpSignatureMethods}}
126+
):
98127
"""Constructor
99128
"""
100129
self.host = host
@@ -123,19 +152,21 @@ class Configuration(object):
123152
self.password = password
124153
"""Password for HTTP basic authentication
125154
"""
155+
{{#hasHttpSignatureMethods}}
126156
if signing_info is not None:
127157
signing_info.host = host
128158
self.signing_info = signing_info
129159
"""The HTTP signing configuration
130160
"""
161+
{{/hasHttpSignatureMethods}}
131162
{{#hasOAuthMethods}}
132-
self.access_token = ""
163+
self.access_token = None
133164
"""access token for OAuth/Bearer
134165
"""
135166
{{/hasOAuthMethods}}
136167
{{^hasOAuthMethods}}
137168
{{#hasBearerMethods}}
138-
self.access_token = ""
169+
self.access_token = None
139170
"""access token for OAuth/Bearer
140171
"""
141172
{{/hasBearerMethods}}

python-experimental/__init__package.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ from {{packageName}}.api_client import ApiClient
2020

2121
# import Configuration
2222
from {{packageName}}.configuration import Configuration
23+
{{#hasHttpSignatureMethods}}
24+
from {{packageName}}.signing import HttpSigningConfiguration
25+
{{/hasHttpSignatureMethods}}
2326

2427
# import exceptions
2528
from {{packageName}}.exceptions import OpenApiException

python-experimental/signing.mustache

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,35 @@ import re
1515
from six.moves.urllib.parse import urlencode, urlparse
1616
from time import mktime
1717

18+
# The constants below define a subset of HTTP headers that can be included in the
19+
# HTTP signature scheme. Additional headers may be included in the signature.
20+
21+
# The '(request-target)' header is a calculated field that includes the HTTP verb,
22+
# the URL path and the URL query.
1823
HEADER_REQUEST_TARGET = '(request-target)'
24+
# The time when the HTTP signature was generated.
1925
HEADER_CREATED = '(created)'
26+
# The time when the HTTP signature expires. The API server should reject HTTP requests
27+
# that have expired.
2028
HEADER_EXPIRES = '(expires)'
29+
# The 'Host' header.
2130
HEADER_HOST = 'Host'
31+
# The 'Date' header.
2232
HEADER_DATE = 'Date'
23-
HEADER_DIGEST = 'Digest' # RFC 3230, include digest of the HTTP request body.
33+
# When the 'Digest' header is included in the HTTP signature, the client automatically
34+
# computes the digest of the HTTP request body, per RFC 3230.
35+
HEADER_DIGEST = 'Digest'
36+
# The 'Authorization' header is automatically generated by the client. It includes
37+
# the list of signed headers and a base64-encoded signature.
2438
HEADER_AUTHORIZATION = 'Authorization'
2539

40+
# The constants below define the cryptographic schemes for the HTTP signature scheme.
2641
SCHEME_HS2019 = 'hs2019'
2742
SCHEME_RSA_SHA256 = 'rsa-sha256'
2843
SCHEME_RSA_SHA512 = 'rsa-sha512'
2944

45+
# The constants below define the signature algorithms that can be used for the HTTP
46+
# signature scheme.
3047
ALGORITHM_RSASSA_PSS = 'RSASSA-PSS'
3148
ALGORITHM_RSASSA_PKCS1v15 = 'RSASSA-PKCS1-v1_5'
3249

@@ -364,5 +381,4 @@ class HttpSigningConfiguration(object):
364381
auth_str = auth_str + "headers=\"{0}\",signature=\"{1}\"".format(
365382
headers_value, signed_msg.decode('ascii'))
366383

367-
print("AUTH: {0}".format(auth_str))
368384
return auth_str

0 commit comments

Comments
 (0)