-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR #3: Added userpass authentication method
Added `userpass` authentication method
- Loading branch information
Showing
15 changed files
with
261 additions
and
46 deletions.
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
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,28 @@ | ||
#!/usr/bin/env python3 | ||
from flask_httpauth import HTTPBasicAuth, HTTPTokenAuth | ||
from Api.api import conn, api | ||
|
||
# Auth Init | ||
userpass = HTTPBasicAuth() | ||
token = HTTPTokenAuth(scheme='Bearer') | ||
|
||
# TODO: error_handler | ||
|
||
|
||
@token.verify_token | ||
def abort_if_authorization_fail(token): | ||
""" Check if an API token is valid | ||
Args: | ||
token (str): API Token | ||
""" | ||
check, username = conn.tokens.is_authorized(token) | ||
if check: | ||
return username | ||
api.abort(401, "Not Authorized to access the requested resource") | ||
|
||
|
||
@userpass.verify_password | ||
def verify_userpass(username, password): | ||
if conn.userpass.is_authorized(username, password): | ||
return username | ||
api.abort(401, "Not Authorized to access the requested resource") |
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
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,69 @@ | ||
#!/usr/bin/env python3 | ||
""" User-Pass authentication for Secrets Manager | ||
""" | ||
from bson.timestamp import Timestamp | ||
import datetime as dt | ||
from werkzeug.security import generate_password_hash, check_password_hash | ||
|
||
|
||
class User_Pass: | ||
def __init__(self, userpass_auth_col): | ||
""" Userpass operations | ||
Args: | ||
userpass_auth_col (pymongo.collection.Collection) | ||
""" | ||
# * Create unique index on 'username' for secrets_manager_auth.userpass | ||
# * db.userpass.createIndex( { "username": 1 }, { unique: true } ) | ||
self._userpass = userpass_auth_col | ||
|
||
def register(self, username, password): | ||
""" Register a new user | ||
Args: | ||
username (str): Username | ||
password (str): Password | ||
Returns: | ||
dict : Dictionary with operation status | ||
""" | ||
finder = self._userpass.find_one({"username": username}) | ||
if not finder: | ||
password = generate_password_hash(password, method='sha256') | ||
data = { | ||
"username": username, | ||
"password": password, | ||
"added_on": Timestamp(int(dt.datetime.today().timestamp()), 1), | ||
} | ||
_ = self._userpass.insert_one(data) | ||
status = {"status": "OK"} | ||
else: | ||
status = {"status": "User already exist"} | ||
return status | ||
|
||
def remove(self, username): | ||
""" Deletes an existing user | ||
Args: | ||
username (str): Username | ||
Returns: | ||
dict : Dictionary with operation status | ||
""" | ||
finder = self._userpass.find_one({"username": username}) | ||
if not finder: | ||
result = {"status": "Username does not exist"} | ||
else: | ||
_ = self._userpass.delete_one({"username": username}) | ||
result = {"status": "OK"} | ||
return result | ||
|
||
def is_authorized(self, username, password): | ||
""" Check if a userpass is valid | ||
Args: | ||
username (str): Username | ||
password (str): Password | ||
Returns: | ||
bool: True for valid userpass and False otherwise. | ||
""" | ||
finder = self._userpass.find_one({"username": username}) | ||
# Return False, if username is not found | ||
if not finder: | ||
return False | ||
# Return True, if userpass is valid | ||
return check_password_hash(finder["password"], password) |
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
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
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,73 @@ | ||
#!/usr/bin/env python3 | ||
# Userpass Authentication API Resource | ||
from flask_restx import fields, Resource | ||
from Api.api import api, conn | ||
|
||
# Userpass Auth Namespace | ||
userpass_ns = api.namespace( | ||
name="auth/userpass", | ||
description="Allows authentication using a username and password.") | ||
userpass_model = api.model( | ||
"Auth Method - Userpass", { | ||
"username": fields.String( | ||
required=True, pattern="[a-fA-F0-9_]+", min_length=2, | ||
description="Username for userpass authentication"), | ||
"password": fields.String( | ||
required=True, min_length=6, | ||
description="Password for userpass authentication"), | ||
"status": fields.String( | ||
required=False, | ||
description="Operation Status"), | ||
}) | ||
|
||
# Userpass Arguments | ||
# For deleting user | ||
delete_userpass_parser = api.parser() | ||
delete_userpass_parser.add_argument( | ||
"username", type=str, required=True, location="form", | ||
help="Username must already exist.") | ||
# For adding new user | ||
post_userpass_parser = api.parser() | ||
post_userpass_parser.add_argument( | ||
"username", type=str, required=True, location="form", | ||
help="Username must atleast be 2 characters long") | ||
post_userpass_parser.add_argument( | ||
"password", type=str, required=True, location="form", | ||
help="Password should atleast be 6 characters long") | ||
|
||
|
||
@userpass_ns.route("/delete") | ||
@api.doc( | ||
responses={}, | ||
params={}) | ||
class Auth_Userpass_delete(Resource): | ||
"""Userpass operations""" | ||
|
||
@api.doc( | ||
description="Revoke a given user", | ||
responses={200: "User account removed"}, | ||
parser=delete_userpass_parser) | ||
@api.marshal_with(userpass_model) | ||
def delete(self): | ||
"""Revoke a given user""" | ||
args = delete_userpass_parser.parse_args() | ||
return conn.userpass.remove(username=args['username']) | ||
|
||
|
||
@userpass_ns.route("/register") | ||
@api.doc( | ||
responses={}, | ||
params={}) | ||
class Auth_Userpass_register(Resource): | ||
"""Userpass operations""" | ||
|
||
@api.doc( | ||
description="Register new user.", | ||
parser=post_userpass_parser) | ||
@api.marshal_with(userpass_model) | ||
def post(self): | ||
"""Register new user""" | ||
# TODO: Support for root key to create new users | ||
args = post_userpass_parser.parse_args() | ||
_usr, _pass = args['username'], args['password'] | ||
return conn.userpass.register(username=_usr, password=_pass) |
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
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
Oops, something went wrong.