-
Notifications
You must be signed in to change notification settings - Fork 4
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
Closed
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
61bd5bd
added initial apikey endpoint
brngylni e8186a8
fixed request validation
brngylni d9c43a7
resolve review
brngylni a7542b1
resolve review
brngylni e3f2a87
delete __init__ and util
brngylni 76d852a
added initial apikey endpoint
brngylni 2c0e3db
Add SQLAlchemy Helpers integration
gridhead 74a9a58
Merge branch 'dev_apikey_endpoint' of https://github.com/brngylni/web…
brngylni 33d70d5
Merge branch 'main' into dev_apikey_endpoint
brngylni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add exception handling for |
||
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}) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ref. #24 (comment)