forked from klokantech/flask-fastspring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask_fastspring.py
282 lines (238 loc) · 9.67 KB
/
flask_fastspring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import json
import requests
from base64 import b64encode
from cryptography.hazmat.backends import _available_backends
from cryptography.hazmat.primitives.ciphers import Cipher
from cryptography.hazmat.primitives.ciphers.algorithms import AES
from cryptography.hazmat.primitives.ciphers.modes import ECB
from cryptography.hazmat.primitives.padding import PKCS7
from cryptography.hazmat.primitives.serialization import load_pem_private_key
from datetime import datetime
from flask import Markup, current_app, render_template_string
from os import urandom
from psycopg2.tz import FixedOffsetTimezone
from sqlalchemy import Boolean, Column, DateTime, Text
from sqlalchemy.dialects.postgresql import JSON
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import deferred
class FastSpring:
def __init__(self, app=None):
self.storefront = None
self.username = None
self.password = None
self.openssl = None
self.access_key = None
self.private_key = None
if app is not None:
self.init_app(app)
def init_app(self, app):
"""Initialize the extension.
To authenticate with FastSpring API, configure the
FASTSPRING_USERNAME and FASTSPRING_PASSWORD options.
The FASTSPRING_STOREFRONT option determines which
storefront will be used, ie. testing or production.
Because FastSpring actually has a testing mode, these
options are all mandatory.
For secure payloads, configure the path to the RSA
private key with the FASTSPRING_PRIVATE_KEY option,
and the API access key with the FASTSPRING_ACCESS_KEY
option.
"""
app.extensions['fastspring'] = self
self.storefront = app.config['FASTSPRING_STOREFRONT']
self.username = app.config['FASTSPRING_USERNAME']
self.password = app.config['FASTSPRING_PASSWORD']
private_key = app.config.get('FASTSPRING_PRIVATE_KEY')
if private_key is not None:
self.openssl = openssl_backend()
self.access_key = app.config['FASTSPRING_ACCESS_KEY']
with open(private_key, 'rb') as fp:
self.private_key = load_pem_private_key(
fp.read(), password=None, backend=self.openssl)
def secure(self, payload):
"""Return payload secured with random key.
The return value is in the format expected by the FastSpring
session variable. That means you can do the following.
fastspring.render_head(
webhook=url_for('...'),
session={
'reset': True,
'secure': fastspring.secure({
...
}),
})
"""
key = urandom(16)
return {
'payload': self.secure_payload(key, json.dumps(payload).encode()),
'key': self.secure_key(key),
}
def secure_payload(self, key, payload):
"""Return payload secured with key."""
result = []
padder = PKCS7(128).padder()
encryptor = Cipher(AES(key), ECB(), backend=self.openssl).encryptor()
result.append(encryptor.update(padder.update(payload)))
result.append(encryptor.update(padder.finalize()))
result.append(encryptor.finalize())
return b64encode(b''.join(result)).decode()
def secure_key(self, key):
"""Return key secured with RSA private key."""
result = openssl_private_encrypt(self.private_key, key, self.openssl)
return b64encode(result).decode()
def render_head(self, webhook=None, session=None, payload=None):
html = render_template_string(
HEAD_TEMPLATE,
storefront=self.storefront,
access_key=self.access_key,
webhook=webhook,
session=session,
payload=payload)
return Markup(html)
def render_button(self, product):
t = 'data-fsc-action="Add,Checkout" data-fsc-item-path-value="{}"'
return Markup(t.format(product))
def fetch_order(self, order_id):
return self.fetch('/orders/{}'.format(order_id))
def fetch_subscription(self, subscription_id):
return self.fetch('/subscriptions/{}'.format(subscription_id))
def fetch(self, uri):
response = requests.get(
'https://api.fastspring.com' + uri,
auth=(self.username, self.password))
if response.status_code != 200:
raise APIError(response)
data = response.json()
if data['result'] != 'success':
raise APIError(response)
return data
class OrderMixin:
order_id = Column(Text, primary_key=True)
reference = Column(Text, nullable=False, unique=True)
invoice = Column(Text, nullable=False)
changed = Column(DateTime(timezone=True), nullable=False)
is_complete = Column(Boolean, default=False, nullable=False)
@declared_attr
def data(cls):
return deferred(Column(JSON, nullable=False))
def synchronize(self):
data = current_app.extensions['fastspring'].fetch_order(self.order_id)
changed = milliseconds_to_datetime(data['changed'])
if self.changed is not None and self.changed >= changed:
return False
self.reference = data['reference']
self.invoice = data['invoiceUrl']
self.changed = changed
self.is_complete = data['completed']
self.data = data
return True
def subscription_item(self):
candidates = []
for item in self.data['items']:
if item.get('subscription'):
candidates.append(item)
if len(candidates) != 1:
return None
return candidates[0]
class SubscriptionMixin:
subscription_id = Column(Text, primary_key=True)
begin = Column(DateTime(timezone=True), nullable=False)
changed = Column(DateTime(timezone=True), nullable=False)
next_event = Column(DateTime(timezone=True))
next_charge = Column(DateTime(timezone=True))
end = Column(DateTime(timezone=True))
is_active = Column(Boolean, nullable=False)
state = Column(Text, nullable=False)
@declared_attr
def data(cls):
return deferred(Column(JSON, nullable=False))
def synchronize(self):
data = current_app.extensions['fastspring'].fetch_subscription(self.subscription_id) # noqa
changed = milliseconds_to_datetime(data['changed'])
if self.changed is not None and self.changed >= changed:
return False
self.begin = milliseconds_to_datetime(data['begin'])
self.changed = changed
self.next_event = milliseconds_to_datetime(data.get('next'))
self.next_charge = milliseconds_to_datetime(data.get('nextChargeDate'))
self.end = milliseconds_to_datetime(data.get('end'))
self.is_active = data['active']
self.state = data['state']
self.data = data
return True
class APIError(Exception):
def __init__(self, response):
self.response = response
def __str__(self):
template = 'FastSpring API {} at {} failed with status code {}:\n{}'
return template.format(
self.response.request.method,
self.response.request.url,
self.response.status_code,
self.response.text)
def openssl_backend():
"""Return OpenSSL cryptography backend or fail."""
for backend in _available_backends():
if backend.name == 'openssl':
return backend
raise Exception('Could not find OpenSSL cryptography backend')
def openssl_private_encrypt(key, data, backend):
"""Encrypt data with RSA private key.
This is a rewrite of the function from PHP, using cryptography
FFI bindings to the OpenSSL library. Private key encryption is
non-standard operation and Python packages either don't offer
it at all, or it's incompatible with the PHP version.
The backend argument MUST be the OpenSSL cryptography backend.
"""
length = backend._lib.EVP_PKEY_size(key._evp_pkey)
buffer = backend._ffi.new('unsigned char[]', length)
result = backend._lib.RSA_private_encrypt(
len(data), data, buffer,
backend._lib.EVP_PKEY_get1_RSA(key._evp_pkey),
backend._lib.RSA_PKCS1_PADDING)
backend.openssl_assert(result == length)
return backend._ffi.buffer(buffer)[:]
UTC = FixedOffsetTimezone(offset=0)
def milliseconds_to_datetime(m):
if m is None:
return None
return datetime.utcfromtimestamp(m / 1000).replace(tzinfo=UTC)
HEAD_TEMPLATE = """\
<script type="text/javascript">
var fscSession = {{ session|tojson }};
{% if webhook %}
function fastspringOnPopupClosed(data) {
if (!data) return;
var xhr = new XMLHttpRequest();
xhr.open("POST", "{{ webhook }}", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
window.location.replace("{{ request.url }}");
} else if (xhr.status === 201 || (301 <= xhr.status && xhr.status <= 303)) {
window.location.replace(xhr.getResponseHeader("Location"));
} else {
var message = "ERROR: Could not process order: " + data["reference"];
console.log(message);
alert(message);
}
}
};
xhr.send(JSON.stringify({
"order_id": data["id"],
"reference": data["reference"],
"payload": {{ payload|tojson }}
}));
}
{% endif %}
</script>
<script
id="fsc-api"
src="https://d1f8f9xcsvx3ha.cloudfront.net/sbl/0.7.2/fastspring-builder.min.js"
type="text/javascript"
{% if webhook %}data-popup-closed="fastspringOnPopupClosed"{% endif %}
{% if access_key %}data-access-key="{{ access_key }}"{% endif %}
data-storefront="{{ storefront }}">
</script>
"""