Skip to content

Commit 0ab459e

Browse files
committed
Used merchant user IDs to check for access
1 parent 7a70392 commit 0ab459e

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
installation requirements as it's required for the added manual ident URLs
1313
feature.
1414

15+
* Added support to check for access based on `muid` instead of `lptoken`.
16+
1517
## 5.2.0
1618

1719
* Added constants outlining expiration and duration time bases for purchases

laterpay/__init__.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def get_access_url(self):
343343
"""
344344
return self._access_url
345345

346-
def get_access_params(self, article_ids, lptoken=None):
346+
def get_access_params(self, article_ids, lptoken=None, muid=None):
347347
"""
348348
Return a params ``dict`` for /access call.
349349
@@ -352,6 +352,7 @@ def get_access_params(self, article_ids, lptoken=None):
352352
:param article_ids: list of article ids or a single article id as a
353353
string
354354
:param lptoken: optional lptoken as `str`
355+
:param str muid: merchant defined user ID. Optional.
355356
"""
356357
if not isinstance(article_ids, (list, tuple)):
357358
article_ids = [article_ids]
@@ -363,6 +364,13 @@ def get_access_params(self, article_ids, lptoken=None):
363364
'article_id': article_ids,
364365
}
365366

367+
if muid:
368+
# TODO: The behavior when lptoken and muid are given is not yet
369+
# defined. Thus we'll allow both at the same time for now. It might
370+
# be that in the end only one is allowed or one is prefered over
371+
# the other.
372+
params['muid'] = muid
373+
366374
params['hmac'] = signing.sign(
367375
secret=self.shared_secret,
368376
params=params.copy(),
@@ -372,7 +380,7 @@ def get_access_params(self, article_ids, lptoken=None):
372380

373381
return params
374382

375-
def get_access_data(self, article_ids, lptoken=None):
383+
def get_access_data(self, article_ids, lptoken=None, muid=None):
376384
"""
377385
Perform a request to /access API and return obtained data.
378386
@@ -383,8 +391,9 @@ def get_access_data(self, article_ids, lptoken=None):
383391
:param article_ids: list of article ids or a single article id as a
384392
string
385393
:param lptoken: optional lptoken as `str`
394+
:param str muid: merchant defined user ID. Optional.
386395
"""
387-
params = self.get_access_params(article_ids=article_ids, lptoken=lptoken)
396+
params = self.get_access_params(article_ids=article_ids, lptoken=lptoken, muid=muid)
388397
url = self.get_access_url()
389398
headers = self.get_request_headers()
390399

tests/test_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ def test_get_access_data_success(self, time_time_mock, sign_mock):
326326
data = client.get_access_data(
327327
['article-1', 'article-2'],
328328
lptoken='fake-lptoken',
329+
muid='some-user',
329330
)
330331

331332
self.assertEqual(data, {
@@ -348,6 +349,7 @@ def test_get_access_data_success(self, time_time_mock, sign_mock):
348349
self.assertEqual(qd['cp'], ['fake-cp-key'])
349350
self.assertEqual(qd['article_id'], ['article-1', 'article-2'])
350351
self.assertEqual(qd['hmac'], ['fake-signature'])
352+
self.assertEqual(qd['muid'], ['some-user'])
351353

352354
sign_mock.assert_called_once_with(
353355
secret='fake-shared-secret',
@@ -356,6 +358,7 @@ def test_get_access_data_success(self, time_time_mock, sign_mock):
356358
'article_id': ['article-1', 'article-2'],
357359
'ts': '123',
358360
'lptoken': 'fake-lptoken',
361+
'muid': 'some-user',
359362
},
360363
url='http://example.net/access',
361364
method='GET',
@@ -368,13 +371,22 @@ def test_get_access_params(self, time_time_mock, sign_mock):
368371
sign_mock.return_value = 'fake-signature'
369372

370373
params = self.lp.get_access_params('article-1', lptoken='fake-lptoken')
374+
self.assertEqual(params, {
375+
'cp': 1,
376+
'ts': '123',
377+
'lptoken': 'fake-lptoken',
378+
'article_id': ['article-1'],
379+
'hmac': 'fake-signature',
380+
})
371381

382+
params = self.lp.get_access_params('article-1', lptoken='fake-lptoken', muid='some-user')
372383
self.assertEqual(params, {
373384
'cp': '1',
374385
'ts': '123',
375386
'lptoken': 'fake-lptoken',
376387
'article_id': ['article-1'],
377388
'hmac': 'fake-signature',
389+
'muid': 'some-user',
378390
})
379391

380392
@mock.patch('time.time')

0 commit comments

Comments
 (0)