-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add fulfill route and tests #280
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import json | ||
import os | ||
|
||
import jwt | ||
|
||
from flask import Blueprint, request | ||
from ..utils import APIUtils | ||
from logger import createLog | ||
|
||
JWT_ALGORITHM = '' | ||
logger = createLog(__name__) | ||
|
||
fulfill = Blueprint('fulfill', __name__, url_prefix='/fulfill') | ||
|
||
@fulfill.route('/<uuid>', methods=['GET']) | ||
def workFulfill(uuid): | ||
logger.info('Checking if authorization is needed for work {}'.format(uuid)) | ||
|
||
requires_authorization = True | ||
|
||
if requires_authorization: | ||
try: | ||
bearer = request.headers.get('Authorization') | ||
token = bearer.split()[1] | ||
|
||
jwt_secret = os.environ['NYPL_API_CLIENT_PUBLIC_KEY'] | ||
decoded_token =(jwt.decode(token, jwt_secret, 'RS256', | ||
audience="app_myaccount")) | ||
if json.loads(json.dumps(decoded_token))['iss'] == "https://www.nypl.org": | ||
statusCode = 200 | ||
responseBody = uuid | ||
else: | ||
statusCode = 401 | ||
responseBody = 'Invalid access token' | ||
|
||
except jwt.exceptions.ExpiredSignatureError: | ||
statusCode = 401 | ||
responseBody = 'Expired access token' | ||
except (jwt.exceptions.DecodeError, UnicodeDecodeError, IndexError, AttributeError): | ||
statusCode = 401 | ||
responseBody = 'Invalid access token' | ||
except ValueError: | ||
logger.warning("Could not deserialize NYPL-issued public key") | ||
statusCode = 500 | ||
responseBody = 'Server error' | ||
|
||
else: | ||
# TODO: In the future, this could record an analytics timestamp | ||
# and redirect to URL of a work if authentication is not required. | ||
# For now, only use /fulfill endpoint in response if authentication is required. | ||
statusCode = 400 | ||
responseBody = "Bad Request" | ||
|
||
response = APIUtils.formatResponseObject( | ||
statusCode, 'fulfill', responseBody | ||
) | ||
|
||
if statusCode == 401: | ||
response[0].headers['WWW-Authenticate'] = 'Bearer' | ||
|
||
return response | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,12 @@ HATHI_API_ROOT: https://babel.hathitrust.org/cgi/htd | |
OCLC_QUERY_LIMIT: '390000' | ||
#OCLC_API_KEY: | ||
|
||
# NYPL AUTH CONFIGURATION | ||
NYPL_API_CLIENT_PUBLIC_KEY: > | ||
-----BEGIN PUBLIC KEY----- | ||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA44ilHg/PxcJYsISHMRyoxsmez178qZpkJVXg7rOMVTLZuf05an7Pl+lX4nw/rqcvGQDXyrimciLgLkWu00xhm6h6klTeJSNq2DgseF8OMw2olfuBKq1NBQ/vC8U0l5NJu34oSN4/iipgpovqAHHBGV4zDt0EWSXE5xpnBWi+w1NMAX/muB2QRfRxkkhueDkAmwKvz5MXJPay7FB/WRjf+7r2EN78x5iQKyCw0tpEZ5hpBX831SEnVULCnpFOcJWMPLdg0Ff6tBmgDxKQBVFIQ9RrzMLTqxKnVVn2+hVpk4F/8tMsGCdd4s/AJqEQBy5lsq7ji1B63XYqi5fc1SnJEQIDAQAB | ||
-----END PUBLIC KEY----- | ||
Comment on lines
+51
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good addition to the sample compose file for the local environment. |
||
|
||
# Bardo CCE API URL | ||
BARDO_CCE_API: http://sfr-bardo-copyright-development.us-east-1.elasticbeanstalk.com/search | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from flask import Flask | ||
import pytest | ||
|
||
import jwt | ||
|
||
from api.blueprints.drbFulfill import workFulfill | ||
from api.utils import APIUtils | ||
|
||
|
||
class TestSearchBlueprint: | ||
@pytest.fixture | ||
def mockUtils(self, mocker): | ||
return mocker.patch.multiple( | ||
APIUtils, | ||
formatResponseObject=mocker.DEFAULT | ||
) | ||
|
||
@pytest.fixture | ||
def testApp(self): | ||
flaskApp = Flask('test') | ||
flaskApp.config['DB_CLIENT'] = 'testDBClient' | ||
flaskApp.config['READER_VERSION'] = 'test' | ||
|
||
return flaskApp | ||
|
||
def test_workFulfill_invalid_token(self, testApp, mockUtils, monkeypatch): | ||
with testApp.test_request_context('/fulfill/12345', | ||
headers={'Authorization': 'Bearer Whatever'}): | ||
monkeypatch.setenv('NYPL_API_CLIENT_PUBLIC_KEY', "SomeKeyValue") | ||
workFulfill('12345') | ||
mockUtils['formatResponseObject'].assert_called_once_with( | ||
401, 'fulfill', 'Invalid access token') | ||
|
||
def test_workFulfill_no_bearer_auth(self, testApp, mockUtils): | ||
with testApp.test_request_context('/fulfill/12345', | ||
headers={'Authorization': 'Whatever'}): | ||
workFulfill('12345') | ||
mockUtils['formatResponseObject'].assert_called_once_with( | ||
401, 'fulfill', 'Invalid access token') | ||
|
||
def test_workFulfill_empty_token(self, testApp, mockUtils): | ||
with testApp.test_request_context('/fulfill/12345', | ||
headers={'Authorization': ''}): | ||
workFulfill('12345') | ||
mockUtils['formatResponseObject'].assert_called_once_with( | ||
401, 'fulfill', 'Invalid access token') | ||
|
||
def test_workFulfill_no_header(self, testApp, mockUtils): | ||
with testApp.test_request_context('/fulfill/12345'): | ||
workFulfill('12345') | ||
mockUtils['formatResponseObject'].assert_called_once_with( | ||
401, 'fulfill', 'Invalid access token') | ||
|
||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to catch all these exception cases