Skip to content
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

added initial apikey endpoint #26

Closed
wants to merge 9 commits into from
95 changes: 95 additions & 0 deletions webhook_to_fedora_messaging/endpoints/apikey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from flask import Blueprint, Flask, request, Response, Request
from ..database import db
from ..models.apikey import APIKey
from ..models.user import User
Comment on lines +2 to +4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from datetime import datetime
from sqlalchemy_helpers import get_or_create
from .util import not_found, success, bad_request, created, conflict, validate_request, unprocessable_entity


app = Flask(__name__)
apikey_endpoint = Blueprint("apikey_endpoint", __name__)

brngylni marked this conversation as resolved.
Show resolved Hide resolved

@apikey_endpoint.route("/apikey", methods=["POST"])
@validate_request(['username', 'valid_till', 'name'])
def create_apikey():
"""Used for creating a new service by sending a post request to /service/ path.

Request body:
username: Username of the user that api key belongs to
valid_till: Time the api key will be valid until
name: Name of the api key.
"""
session = db.Session()
body = request.json

user = session.query(User).filter(User.username == body['username']).first()
brngylni marked this conversation as resolved.
Show resolved Hide resolved
if user is None:
return not_found()
# Can be different parsing here.
valid_till = datetime.strptime(body['valid_till'], "%Y-%m-%d")
brngylni marked this conversation as resolved.
Show resolved Hide resolved
apikey, is_created = get_or_create(session, APIKey, user_id=user.id, name=body['name'], expiry_date=valid_till)
brngylni marked this conversation as resolved.
Show resolved Hide resolved

if not is_created:
return conflict({'message': 'Key Already Exists'})
else:
return created({'message': 'Created', 'uuid': apikey.id, 'code': apikey.token})


@apikey_endpoint.route("/apikey/search", methods=["GET"])
@validate_request
def list_apikey():
"""Used for listing all api keys by sending a get request to /apikey/search path.

Request body:
username: Username of the user
"""
session = db.Session()
user = session.query(User).filter(User.username.like(request.json['username'])).first()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add exception handling for KeyError (when the username key is not found in the JSON body). Abort the request with the HTTP 429 Unprocessable Entity error.

if user is None:
return not_found()

apikeys = session.query(APIKey).filter(APIKey.user_id == user.id).all()

return success({'apikey_list': apikeys})


@apikey_endpoint.route("/apikey", methods=["GET"])
@validate_request(['apikey_uuid'])
def lookup_apikey():
"""Used for searching api keys by sending a get request to /apikey path

Request body:
uuid: UUID of the apikey
"""

session = db.Session()
apikey = session.query(APIKey).filter(APIKey.id == request.json['apikey_uuid']).first()
brngylni marked this conversation as resolved.
Show resolved Hide resolved

if apikey is None:
return not_found()
else:
valid_till = datetime.strftime(apikey.expiry_date, "%Y-%m-%d")
return success({'uuid': apikey.id, 'name': apikey.name, 'valid_till': valid_till, 'valid': not apikey.disabled})


@apikey_endpoint.route("/apikey/revoke", methods=["PUT"])
@validate_request
def revoke_service():
brngylni marked this conversation as resolved.
Show resolved Hide resolved
"""Used for revoking an api key by sending a PUT request to /apikey/revoke path.

Request body:
username: Username of the user that the api key belongs to.
apikey_uuid: UUID of the api key.
"""
session = db.Session()
user = session.query(User).filter(User.username == request.json['username']).first()
brngylni marked this conversation as resolved.
Show resolved Hide resolved
apikey = session.query(APIKey).filter(APIKey.user_id == user.id and APIKey.id == request.json['apikey_uuid']).first()
brngylni marked this conversation as resolved.
Show resolved Hide resolved
if apikey is None:
return not_found()
else:
apikey.disabled = True
session.commit()
return success({'uuid': apikey.id, 'is_valid': not apikey.disabled})

Loading