Black lint all the things
This commit is contained in:
@ -14,23 +14,32 @@ from datetime import datetime
|
||||
manager = Manager(usage="Handles all api key related tasks.")
|
||||
|
||||
|
||||
@manager.option('-u', '--user-id', dest='uid', help='The User ID this access key belongs too.')
|
||||
@manager.option('-n', '--name', dest='name', help='The name of this API Key.')
|
||||
@manager.option('-t', '--ttl', dest='ttl', help='The TTL of this API Key. -1 for forever.')
|
||||
@manager.option(
|
||||
"-u", "--user-id", dest="uid", help="The User ID this access key belongs too."
|
||||
)
|
||||
@manager.option("-n", "--name", dest="name", help="The name of this API Key.")
|
||||
@manager.option(
|
||||
"-t", "--ttl", dest="ttl", help="The TTL of this API Key. -1 for forever."
|
||||
)
|
||||
def create(uid, name, ttl):
|
||||
"""
|
||||
Create a new api key for a user.
|
||||
:return:
|
||||
"""
|
||||
print("[+] Creating a new api key.")
|
||||
key = api_key_service.create(user_id=uid, name=name,
|
||||
ttl=ttl, issued_at=int(datetime.utcnow().timestamp()), revoked=False)
|
||||
key = api_key_service.create(
|
||||
user_id=uid,
|
||||
name=name,
|
||||
ttl=ttl,
|
||||
issued_at=int(datetime.utcnow().timestamp()),
|
||||
revoked=False,
|
||||
)
|
||||
print("[+] Successfully created a new api key. Generating a JWT...")
|
||||
jwt = create_token(uid, key.id, key.ttl)
|
||||
print("[+] Your JWT is: {jwt}".format(jwt=jwt))
|
||||
|
||||
|
||||
@manager.option('-a', '--api-key-id', dest='aid', help='The API Key ID to revoke.')
|
||||
@manager.option("-a", "--api-key-id", dest="aid", help="The API Key ID to revoke.")
|
||||
def revoke(aid):
|
||||
"""
|
||||
Revokes an api key for a user.
|
||||
|
@ -12,14 +12,19 @@ from lemur.database import db
|
||||
|
||||
|
||||
class ApiKey(db.Model):
|
||||
__tablename__ = 'api_keys'
|
||||
__tablename__ = "api_keys"
|
||||
id = Column(Integer, primary_key=True)
|
||||
name = Column(String)
|
||||
user_id = Column(Integer, ForeignKey('users.id'))
|
||||
user_id = Column(Integer, ForeignKey("users.id"))
|
||||
ttl = Column(BigInteger)
|
||||
issued_at = Column(BigInteger)
|
||||
revoked = Column(Boolean)
|
||||
|
||||
def __repr__(self):
|
||||
return "ApiKey(name={name}, user_id={user_id}, ttl={ttl}, issued_at={iat}, revoked={revoked})".format(
|
||||
user_id=self.user_id, name=self.name, ttl=self.ttl, iat=self.issued_at, revoked=self.revoked)
|
||||
user_id=self.user_id,
|
||||
name=self.name,
|
||||
ttl=self.ttl,
|
||||
iat=self.issued_at,
|
||||
revoked=self.revoked,
|
||||
)
|
||||
|
@ -13,12 +13,18 @@ from lemur.users.schemas import UserNestedOutputSchema, UserInputSchema
|
||||
|
||||
|
||||
def current_user_id():
|
||||
return {'id': g.current_user.id, 'email': g.current_user.email, 'username': g.current_user.username}
|
||||
return {
|
||||
"id": g.current_user.id,
|
||||
"email": g.current_user.email,
|
||||
"username": g.current_user.username,
|
||||
}
|
||||
|
||||
|
||||
class ApiKeyInputSchema(LemurInputSchema):
|
||||
name = fields.String(required=False)
|
||||
user = fields.Nested(UserInputSchema, missing=current_user_id, default=current_user_id)
|
||||
user = fields.Nested(
|
||||
UserInputSchema, missing=current_user_id, default=current_user_id
|
||||
)
|
||||
ttl = fields.Integer()
|
||||
|
||||
|
||||
|
@ -34,7 +34,7 @@ def revoke(aid):
|
||||
:return:
|
||||
"""
|
||||
api_key = get(aid)
|
||||
setattr(api_key, 'revoked', False)
|
||||
setattr(api_key, "revoked", False)
|
||||
|
||||
return database.update(api_key)
|
||||
|
||||
@ -80,10 +80,10 @@ def render(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')
|
||||
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)
|
||||
|
@ -19,10 +19,16 @@ from lemur.auth.permissions import ApiKeyCreatorPermission
|
||||
from lemur.common.schema import validate_schema
|
||||
from lemur.common.utils import paginated_parser
|
||||
|
||||
from lemur.api_keys.schemas import api_key_input_schema, api_key_revoke_schema, api_key_output_schema, \
|
||||
api_keys_output_schema, api_key_described_output_schema, user_api_key_input_schema
|
||||
from lemur.api_keys.schemas import (
|
||||
api_key_input_schema,
|
||||
api_key_revoke_schema,
|
||||
api_key_output_schema,
|
||||
api_keys_output_schema,
|
||||
api_key_described_output_schema,
|
||||
user_api_key_input_schema,
|
||||
)
|
||||
|
||||
mod = Blueprint('api_keys', __name__)
|
||||
mod = Blueprint("api_keys", __name__)
|
||||
api = Api(mod)
|
||||
|
||||
|
||||
@ -81,8 +87,8 @@ class ApiKeyList(AuthenticatedResource):
|
||||
"""
|
||||
parser = paginated_parser.copy()
|
||||
args = parser.parse_args()
|
||||
args['has_permission'] = ApiKeyCreatorPermission().can()
|
||||
args['requesting_user_id'] = g.current_user.id
|
||||
args["has_permission"] = ApiKeyCreatorPermission().can()
|
||||
args["requesting_user_id"] = g.current_user.id
|
||||
return service.render(args)
|
||||
|
||||
@validate_schema(api_key_input_schema, api_key_output_schema)
|
||||
@ -124,12 +130,26 @@ class ApiKeyList(AuthenticatedResource):
|
||||
:statuscode 403: unauthenticated
|
||||
"""
|
||||
if not ApiKeyCreatorPermission().can():
|
||||
if data['user']['id'] != g.current_user.id:
|
||||
return dict(message="You are not authorized to create tokens for: {0}".format(data['user']['username'])), 403
|
||||
if data["user"]["id"] != g.current_user.id:
|
||||
return (
|
||||
dict(
|
||||
message="You are not authorized to create tokens for: {0}".format(
|
||||
data["user"]["username"]
|
||||
)
|
||||
),
|
||||
403,
|
||||
)
|
||||
|
||||
access_token = service.create(name=data['name'], user_id=data['user']['id'], ttl=data['ttl'],
|
||||
revoked=False, issued_at=int(datetime.utcnow().timestamp()))
|
||||
return dict(jwt=create_token(access_token.user_id, access_token.id, access_token.ttl))
|
||||
access_token = service.create(
|
||||
name=data["name"],
|
||||
user_id=data["user"]["id"],
|
||||
ttl=data["ttl"],
|
||||
revoked=False,
|
||||
issued_at=int(datetime.utcnow().timestamp()),
|
||||
)
|
||||
return dict(
|
||||
jwt=create_token(access_token.user_id, access_token.id, access_token.ttl)
|
||||
)
|
||||
|
||||
|
||||
class ApiKeyUserList(AuthenticatedResource):
|
||||
@ -186,9 +206,9 @@ class ApiKeyUserList(AuthenticatedResource):
|
||||
"""
|
||||
parser = paginated_parser.copy()
|
||||
args = parser.parse_args()
|
||||
args['has_permission'] = ApiKeyCreatorPermission().can()
|
||||
args['requesting_user_id'] = g.current_user.id
|
||||
args['user_id'] = user_id
|
||||
args["has_permission"] = ApiKeyCreatorPermission().can()
|
||||
args["requesting_user_id"] = g.current_user.id
|
||||
args["user_id"] = user_id
|
||||
return service.render(args)
|
||||
|
||||
@validate_schema(user_api_key_input_schema, api_key_output_schema)
|
||||
@ -230,11 +250,25 @@ class ApiKeyUserList(AuthenticatedResource):
|
||||
"""
|
||||
if not ApiKeyCreatorPermission().can():
|
||||
if user_id != g.current_user.id:
|
||||
return dict(message="You are not authorized to create tokens for: {0}".format(user_id)), 403
|
||||
return (
|
||||
dict(
|
||||
message="You are not authorized to create tokens for: {0}".format(
|
||||
user_id
|
||||
)
|
||||
),
|
||||
403,
|
||||
)
|
||||
|
||||
access_token = service.create(name=data['name'], user_id=user_id, ttl=data['ttl'],
|
||||
revoked=False, issued_at=int(datetime.utcnow().timestamp()))
|
||||
return dict(jwt=create_token(access_token.user_id, access_token.id, access_token.ttl))
|
||||
access_token = service.create(
|
||||
name=data["name"],
|
||||
user_id=user_id,
|
||||
ttl=data["ttl"],
|
||||
revoked=False,
|
||||
issued_at=int(datetime.utcnow().timestamp()),
|
||||
)
|
||||
return dict(
|
||||
jwt=create_token(access_token.user_id, access_token.id, access_token.ttl)
|
||||
)
|
||||
|
||||
|
||||
class ApiKeys(AuthenticatedResource):
|
||||
@ -329,7 +363,9 @@ class ApiKeys(AuthenticatedResource):
|
||||
if not ApiKeyCreatorPermission().can():
|
||||
return dict(message="You are not authorized to update this token!"), 403
|
||||
|
||||
service.update(access_key, name=data['name'], revoked=data['revoked'], ttl=data['ttl'])
|
||||
service.update(
|
||||
access_key, name=data["name"], revoked=data["revoked"], ttl=data["ttl"]
|
||||
)
|
||||
return dict(jwt=create_token(access_key.user_id, access_key.id, access_key.ttl))
|
||||
|
||||
def delete(self, aid):
|
||||
@ -371,7 +407,7 @@ class ApiKeys(AuthenticatedResource):
|
||||
return dict(message="You are not authorized to delete this token!"), 403
|
||||
|
||||
service.delete(access_key)
|
||||
return {'result': True}
|
||||
return {"result": True}
|
||||
|
||||
|
||||
class UserApiKeys(AuthenticatedResource):
|
||||
@ -472,7 +508,9 @@ class UserApiKeys(AuthenticatedResource):
|
||||
if access_key.user_id != uid:
|
||||
return dict(message="You are not authorized to update this token!"), 403
|
||||
|
||||
service.update(access_key, name=data['name'], revoked=data['revoked'], ttl=data['ttl'])
|
||||
service.update(
|
||||
access_key, name=data["name"], revoked=data["revoked"], ttl=data["ttl"]
|
||||
)
|
||||
return dict(jwt=create_token(access_key.user_id, access_key.id, access_key.ttl))
|
||||
|
||||
def delete(self, uid, aid):
|
||||
@ -517,7 +555,7 @@ class UserApiKeys(AuthenticatedResource):
|
||||
return dict(message="You are not authorized to delete this token!"), 403
|
||||
|
||||
service.delete(access_key)
|
||||
return {'result': True}
|
||||
return {"result": True}
|
||||
|
||||
|
||||
class ApiKeysDescribed(AuthenticatedResource):
|
||||
@ -572,8 +610,12 @@ class ApiKeysDescribed(AuthenticatedResource):
|
||||
return access_key
|
||||
|
||||
|
||||
api.add_resource(ApiKeyList, '/keys', endpoint='api_keys')
|
||||
api.add_resource(ApiKeys, '/keys/<int:aid>', endpoint='api_key')
|
||||
api.add_resource(ApiKeysDescribed, '/keys/<int:aid>/described', endpoint='api_key_described')
|
||||
api.add_resource(ApiKeyUserList, '/users/<int:user_id>/keys', endpoint='user_api_keys')
|
||||
api.add_resource(UserApiKeys, '/users/<int:uid>/keys/<int:aid>', endpoint='user_api_key')
|
||||
api.add_resource(ApiKeyList, "/keys", endpoint="api_keys")
|
||||
api.add_resource(ApiKeys, "/keys/<int:aid>", endpoint="api_key")
|
||||
api.add_resource(
|
||||
ApiKeysDescribed, "/keys/<int:aid>/described", endpoint="api_key_described"
|
||||
)
|
||||
api.add_resource(ApiKeyUserList, "/users/<int:user_id>/keys", endpoint="user_api_keys")
|
||||
api.add_resource(
|
||||
UserApiKeys, "/users/<int:uid>/keys/<int:aid>", endpoint="user_api_key"
|
||||
)
|
||||
|
Reference in New Issue
Block a user