15
15
from automatoes import get_version
16
16
from automatoes .crypto import generate_protected_header , sign_request_v2
17
17
from automatoes .errors import AccountAlreadyExistsError , AcmeError
18
- from automatoes .model import Account , RegistrationResult
18
+ from automatoes .model import ( Account , AccountResponse , RegistrationResponse )
19
19
20
20
import copy
21
21
22
22
from peasant .client .protocol import Peasant
23
23
from peasant .client .transport import METHOD_POST
24
24
from peasant .client .transport_requests import RequestsTransport
25
25
26
- import requests
26
+ import warnings
27
27
28
28
29
29
class AcmeV2Pesant (Peasant ):
@@ -47,11 +47,11 @@ def directory_path(self, path):
47
47
def verify (self ):
48
48
return self ._verify
49
49
50
- def get_registration (self , account : Account ):
50
+ def get_registration (self , account : Account ) -> RegistrationResponse :
51
51
return self .transport .get_registration (account )
52
52
53
53
def new_account (self , account : Account , contacts : list ,
54
- terms_agreed : bool = False ) -> RegistrationResult :
54
+ terms_agreed : bool = False ) -> AccountResponse :
55
55
return self .transport .new_account (account , contacts , terms_agreed )
56
56
57
57
@@ -84,8 +84,7 @@ def __kwargs_updater(self, method, **kwargs):
84
84
protected ['kid' ] = kid
85
85
protected .pop ("jwk" )
86
86
data = kwargs .get ("data" )
87
- if data :
88
- kwargs ['data' ] = sign_request_v2 (key , protected , data )
87
+ kwargs ['data' ] = sign_request_v2 (key , protected , data )
89
88
if self .peasant .verify :
90
89
kwargs ['verify' ] = self .peasant .verify
91
90
return kwargs
@@ -102,21 +101,34 @@ def get_protected_headers(self, key, uri=None):
102
101
return protected_header
103
102
104
103
def post_as_get (self , path , ** kwargs ):
105
- """Send a POST request.
104
+ """Send a POST as GET request.
105
+
106
+ This method enforces no data/body being passed into kwargs.
107
+
108
+ If either data or body is present into kwargs a warning will be
109
+ displayed and the value will be discarded.
110
+
111
+ This post will be sent without body into.
106
112
107
113
:param path: absolute or relative URL for the new
108
114
:class:`requests.Request` object.
109
115
:param **kwargs: Optional arguments that ``request`` takes.
110
116
:return: :class:`requests.Response <Response>` object
111
117
:rtype: requests.Response
112
118
"""
113
- url = self .get_url (path , ** kwargs )
114
- headers = self .get_headers (** kwargs )
115
- kwargs ['headers' ] = headers
116
- kwargs = self .update_kwargs (METHOD_POST , ** kwargs )
117
- with requests .post (url , ** kwargs ) as result :
118
- result .raise_for_status ()
119
- return result
119
+ if "data" in kwargs :
120
+ kwargs .pop ("data" )
121
+ warnings .warn ("Do not pass the key 'data' in kwargs. The "
122
+ "post_as_get method won't add body to the request "
123
+ "and the value will be ignored!" ,
124
+ category = RuntimeWarning )
125
+ if "body" in kwargs :
126
+ kwargs .pop ("body" )
127
+ warnings .warn ("Do not pass the key 'body' in kwargs. The "
128
+ "post_as_get method won't add body to the request "
129
+ "and the value will be ignored!" ,
130
+ category = RuntimeWarning )
131
+ return self .post (path , ** kwargs )
120
132
121
133
def set_directory (self ):
122
134
response = self .get ("/%s" % self .peasant .directory_path )
@@ -125,15 +137,21 @@ def set_directory(self):
125
137
else :
126
138
raise Exception
127
139
128
- def get_registration (self , account : Account ):
140
+ def get_registration (self , account : Account ) -> RegistrationResponse :
129
141
"""
130
142
Get available account information from the server.
131
143
"""
132
144
headers = {'Content-Type' : "application/jose+json" }
133
145
response = self .post_as_get (account .uri , headers = headers ,
134
- kid = self . account .uri ,
135
- key = account . key , uri = account .uri )
146
+ kid = account .uri , key = account . key ,
147
+ uri = account .uri )
136
148
if str (response .status_code ).startswith ("2" ):
149
+ response_json = _json (response )
150
+ return RegistrationResponse (
151
+ contact = response_json ['contact' ],
152
+ orders = response_json ['orders' ],
153
+ status = response_json ['status' ],
154
+ )
137
155
return _json (response )
138
156
raise AcmeError (response )
139
157
@@ -146,37 +164,39 @@ def new_nonce(self):
146
164
147
165
# TODO: Document that this method used to be called register
148
166
def new_account (self , account : Account , contacts : list ,
149
- terms_agreed : bool = False ) -> RegistrationResult :
167
+ terms_agreed : bool = False ) -> AccountResponse :
150
168
""" Create a new account in the Acme Server """
151
169
payload = {
152
170
"termsOfServiceAgreed" : terms_agreed ,
153
171
"contact" : [f"mailto:{ contact } " for contact in contacts ],
154
172
}
155
173
headers = {'Content-Type' : "application/jose+json" }
156
174
path = self .peasant .directory ()['newAccount' ]
157
- response = self .post_as_get (path , data = payload , headers = headers ,
158
- key = account .key , uri = path )
159
-
160
- uri = response .headers .get ("Location" )
161
-
175
+ response = self .post (path , data = payload , headers = headers ,
176
+ key = account .key , uri = path )
162
177
if response .status_code == 201 :
178
+ uri = response .headers .get ("Location" )
163
179
# Find terms of service from link headers
164
180
terms = self .terms_from_directory ()
165
- return RegistrationResult (
166
- contents = _json (response ),
167
- uri = uri ,
168
- terms = terms
181
+ response_json = _json (response )
182
+ account .contact = response_json ['contact' ]
183
+ account .orders = response_json ['orders' ]
184
+ account .uri = uri
185
+ return AccountResponse (
186
+ orders = response_json ['orders' ],
187
+ status = response_json ['status' ],
188
+ terms = terms ,
169
189
)
170
190
elif response .status_code == 409 :
171
191
raise AccountAlreadyExistsError (response , uri )
172
192
raise AcmeError (response )
173
193
174
194
def terms_from_directory (self ):
175
- response = self . get_directory ()
176
- if response . status_code == 200 :
177
- if "meta" in response . json () :
178
- if "termsOfService" in response . json () ['meta' ]:
179
- return response . json () ['meta' ]['termsOfService' ]
195
+ # TODO: Check how to trigger an error here
196
+ directory = self . peasant . directory ()
197
+ if "meta" in directory :
198
+ if "termsOfService" in directory ['meta' ]:
199
+ return directory ['meta' ]['termsOfService' ]
180
200
return None
181
201
182
202
0 commit comments