diff --git a/pagure/api/__init__.py b/pagure/api/__init__.py index 0540acd..0e822ee 100644 --- a/pagure/api/__init__.py +++ b/pagure/api/__init__.py @@ -338,6 +338,43 @@ def api_users(): ) +@API.route('/-/whoami', methods=['POST']) +@api_login_optional() +def api_whoami(): + ''' + Who am I? + --------- + This API endpoint will return the username associated with the provided + API token. + + :: + + POST /api/0/-/whoami + + + Sample response + ^^^^^^^^^^^^^^^ + + :: + + { + "username": "user1" + } + + ''' + + if authenticated(): + return flask.jsonify({'username': flask.g.fas_user.username}) + else: + output = { + 'error_code': APIERROR.EINVALIDTOK.name, + 'error': APIERROR.EINVALIDTOK.value, + } + jsonout = flask.jsonify(output) + jsonout.status_code = 401 + return jsonout + + @API.route('/task//status') @API.route('/task//status/') def api_task_status(taskid): @@ -497,6 +534,7 @@ def api(): api_pull_request_add_flag_doc = load_doc(fork.api_pull_request_add_flag) api_version_doc = load_doc(api_version) + api_whoami_doc = load_doc(api_whoami) api_users_doc = load_doc(api_users) api_view_user_doc = load_doc(user.api_view_user) api_view_user_activity_stats_doc = load_doc( @@ -516,6 +554,7 @@ def api(): api_error_codes_doc = load_doc(api_error_codes) extras = [ + api_whoami_doc, api_version_doc, api_error_codes_doc, ] diff --git a/tests/test_pagure_flask_api.py b/tests/test_pagure_flask_api.py index 0111c7d..4a9e9fc 100644 --- a/tests/test_pagure_flask_api.py +++ b/tests/test_pagure_flask_api.py @@ -151,6 +151,54 @@ class PagureFlaskApitests(tests.SimplePagureTest): self.assertEqual(sorted(data.keys()), ['groups', 'total_groups']) self.assertEqual(data['total_groups'], 1) + def test_api_whoami_unauth(self): + """ Test the api_whoami function. """ + + output = self.app.post('/api/0/-/whoami') + self.assertEqual(output.status_code, 401) + data = json.loads(output.get_data(as_text=True)) + self.assertEqual( + data, + { + u'error': u'Invalid or expired token. Please visit ' + 'http://localhost.localdomain/settings#api-keys to get or ' + 'renew your API token.', + u'error_code': u'EINVALIDTOK' + } + ) + + def test_api_whoami_invalid_auth(self): + """ Test the api_whoami function with an invalid token. """ + tests.create_projects(self.session) + tests.create_tokens(self.session) + + headers = {'Authorization': 'token invalid'} + + output = self.app.post('/api/0/-/whoami', headers=headers) + self.assertEqual(output.status_code, 401) + data = json.loads(output.get_data(as_text=True)) + self.assertEqual( + data, + { + u'error': u'Invalid or expired token. Please visit ' + 'http://localhost.localdomain/settings#api-keys to get or ' + 'renew your API token.', + u'error_code': u'EINVALIDTOK' + } + ) + + def test_api_whoami_auth(self): + """ Test the api_whoami function with a valid token. """ + tests.create_projects(self.session) + tests.create_tokens(self.session) + + headers = {'Authorization': 'token aaabbbcccddd'} + + output = self.app.post('/api/0/-/whoami', headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.get_data(as_text=True)) + self.assertEqual(data, {u'username': u'pingou'}) + if __name__ == '__main__': unittest.main(verbosity=2)