c402f1ff87
Adds in per user api keys to the backend of lemur. the basics are: - API Keys are really just JWTs with custom second length TTLs. - API Keys are provided in the exact same ways JWTs are now. - API Keys can be revoked/unrevoked at any time by their creator as well as have their TTL Change at anytime. - Users can create/view/list their own API Keys at will, and an admin role has permission to modify all api keys in the instance. Adds in support for lemur api keys to the frontend of lemur. doing this required a few changes to the backend as well, but it is now all working (maybe not the best way though, review will determine that). - fixes inconsistency in moduleauthor name I inputted during the first commit. - Allows the revoke schema to optionally allow a full api_key object. - Adds `/users/:user_id/api_keys/:api_key` and `/users/:user_id/api_keys` endpoints. - normalizes use of `userId` vs `userId` - makes `put` call respond with a JWT so the frontend can show the token on updating. - adds in the API Key views for clicking "API Keys" on the main nav. - adds in the API Key views for clicking into a users edit page. - adds tests for the API Key backend views I added.
98 lines
2.0 KiB
Python
98 lines
2.0 KiB
Python
"""
|
|
.. module: lemur.api_keys.service
|
|
:platform: Unix
|
|
:copyright: (c) 2017 by Netflix Inc., see AUTHORS for more
|
|
:license: Apache, see LICENSE for more details.
|
|
.. moduleauthor:: Eric Coan <kungfury@instructure.com>
|
|
"""
|
|
from lemur import database
|
|
from lemur.api_keys.models import ApiKey
|
|
|
|
|
|
def get(aid):
|
|
"""
|
|
Retrieves an api key by its ID.
|
|
:param aid: The access key id to get.
|
|
:return:
|
|
"""
|
|
return database.get(ApiKey, aid)
|
|
|
|
|
|
def delete(access_key):
|
|
"""
|
|
Delete an access key. This is one way to remove a key, though you probably should just set revoked.
|
|
:param access_key:
|
|
:return:
|
|
"""
|
|
database.delete(access_key)
|
|
|
|
|
|
def revoke(aid):
|
|
"""
|
|
Revokes an api key.
|
|
:param aid:
|
|
:return:
|
|
"""
|
|
api_key = get(aid)
|
|
setattr(api_key, 'revoked', False)
|
|
|
|
return database.update(api_key)
|
|
|
|
|
|
def get_all_api_keys():
|
|
"""
|
|
Retrieves all Api Keys.
|
|
:return:
|
|
"""
|
|
return ApiKey.query.all()
|
|
|
|
|
|
def create(**kwargs):
|
|
"""
|
|
Creates a new API Key.
|
|
|
|
:param kwargs:
|
|
:return:
|
|
"""
|
|
api_key = ApiKey(**kwargs)
|
|
database.create(api_key)
|
|
return api_key
|
|
|
|
|
|
def update(api_key, **kwargs):
|
|
"""
|
|
Updates an api key.
|
|
:param api_key:
|
|
:param kwargs:
|
|
:return:
|
|
"""
|
|
for key, value in kwargs.items():
|
|
setattr(api_key, key, value)
|
|
|
|
return database.update(api_key)
|
|
|
|
|
|
def render(args):
|
|
"""
|
|
Helper to parse REST Api requests
|
|
|
|
:param args:
|
|
:return:
|
|
"""
|
|
query = database.session_query(ApiKey)
|
|
user_id = args.pop('user_id', None)
|
|
aid = args.pop('id', None)
|
|
has_permission = args.pop('has_permission', False)
|
|
requesting_user_id = args.pop('requesting_user_id')
|
|
|
|
if user_id:
|
|
query = query.filter(ApiKey.user_id == user_id)
|
|
|
|
if aid:
|
|
query = query.filter(ApiKey.id == aid)
|
|
|
|
if not has_permission:
|
|
query = query.filter(ApiKey.user_id == requesting_user_id)
|
|
|
|
return database.sort_and_page(query, ApiKey, args)
|