def setUp(self): self.db_fd, app.config['DATABASE'] = tempfile.mkstemp() app.config['TESTING'] = True commands.init_db() commands.load_data(filepath('test-data.json')) self.client = app.test_client() with open(filepath('test-patch-replace-values-1.json')) as f: self.patch = f.read() with app.app_context(): self.unauthorized_identity = auth.add_user_or_update_credentials({ 'name': 'Dangerous Dan', 'access_token': 'f7e00c02-6f97-4636-8499-037446d95446', 'expires_in': 631138518, 'orcid': '0000-0000-0000-000X', }) db = database.get_db() curs = db.cursor() curs.execute('UPDATE user SET permissions = ? WHERE name = ?', ('[]', 'Dangerous Dan')) self.user_identity = auth.add_user_or_update_credentials({ 'name': 'Regular Gal', 'access_token': '5005eb18-be6b-4ac0-b084-0443289b3378', 'expires_in': 631138518, 'orcid': '1234-5678-9101-112X', }) self.admin_identity = auth.add_user_or_update_credentials({ 'name': 'Super Admin', 'access_token': 'f7c64584-0750-4cb6-8c81-2932f5daabb8', 'expires_in': 3600, 'orcid': '1211-1098-7654-321X', }, (ActionNeed('accept-patch'),)) db.commit()
def registered(): if not request.args['state'] == session.pop('state_token'): abort(403) data = { 'client_id': app.config['ORCID_CLIENT_ID'], 'client_secret': app.config['ORCID_CLIENT_SECRET'], 'code': request.args['code'], 'grant_type': 'authorization_code', 'redirect_uri': url_for('registered', _external=True), 'scope': '/authenticate', } response = requests.post( 'https://pub.orcid.org/oauth/token', headers={'Accept': 'application/json'}, allow_redirects=True, data=data) credentials = response.json() if not response.status_code == 200: app.logger.error('Response to request for ORCID credentials was not OK') app.logger.error('Request: %s', data) app.logger.error('Response: %s', response.text) identity = auth.add_user_or_update_credentials(credentials) database.get_db().commit() return make_response( """ <!doctype html> <head> <script type="text/javascript"> localStorage.auth = '{}'; window.close(); </script> </head> <body> """.format(json.dumps( {'name': credentials['name'], 'token': identity.b64token.decode()} )))
def setUp(self): self.db_fd, app.config['DATABASE'] = tempfile.mkstemp() app.config['TESTING'] = True self.client = app.test_client() commands.init_db() with app.app_context(): self.identity = auth.add_user_or_update_credentials( {'name': 'Testy Testerson', 'access_token': '5005eb18-be6b-4ac0-b084-0443289b3378', 'expires_in': 631138518, 'orcid': '1234-5678-9101-112X'}) self.expired_identity = auth.add_user_or_update_credentials({ 'name': 'Eric Expired', 'access_token': 'f7c64584-0750-4cb6-8c81-2932f5daabb8', 'expires_in': -3600, 'orcid': '1211-1098-7654-321X', }) database.commit()
def setUp(self): self.db_fd, app.config['DATABASE'] = tempfile.mkstemp() app.config['TESTING'] = True self.client = app.test_client() commands.init_db() commands.load_data(filepath('test-data.json')) with app.app_context(): self.user_identity = auth.add_user_or_update_credentials({ 'name': 'Regular Gal', 'access_token': '5005eb18-be6b-4ac0-b084-0443289b3378', 'expires_in': 631138518, 'orcid': '1234-5678-9101-112X', }) self.admin_identity = auth.add_user_or_update_credentials({ 'name': 'Super Admin', 'access_token': 'f7c64584-0750-4cb6-8c81-2932f5daabb8', 'expires_in': 3600, 'orcid': '1211-1098-7654-321X', }, (ActionNeed('accept-patch'),)) database.commit()
def registered(): if not request.args['state'] == session.pop('state_token', None): abort(403) data = { 'client_id': app.config['ORCID_CLIENT_ID'], 'client_secret': app.config['ORCID_CLIENT_SECRET'], 'code': request.args['code'], 'grant_type': 'authorization_code', 'redirect_uri': build_redirect_uri(cli=('cli' in request.args)), 'scope': '/authenticate', } response = requests.post( 'https://orcid.org/oauth/token', headers={'Accept': 'application/json'}, allow_redirects=True, data=data) if not response.status_code == 200: app.logger.error('Response to request for ORCID credential was not OK') app.logger.error('Request: %s', data) app.logger.error('Response: %s', response.text) credentials = response.json() if 'name' not in credentials or len(credentials['name']) == 0: # User has made their name private, so just use their ORCID as name credentials['name'] = credentials['orcid'] identity = auth.add_user_or_update_credentials(credentials) database.get_db().commit() if 'cli' in request.args: return make_response( ('Your token is: {}'.format(identity.b64token.decode()), {'Content-Type': 'text/plain'})) else: return make_response(""" <!doctype html> <head> <script type="text/javascript"> parent.postMessage( {{ name: {}, token: {} }}, "{}" ) window.close(); </script> </head> <body> """.format( json.dumps(credentials['name']), json.dumps(identity.b64token.decode()), request.host_url ))