-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CE-115] Refine user auth api code structure
Refine user auth api Add user login history Move models into modules directory Change-Id: Ibd6058d72a4a8f97126073874b4a420f2eaf8b13 Signed-off-by: Haitao Yue <hightall@me.com>
- Loading branch information
Showing
20 changed files
with
270 additions
and
150 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,7 @@ | ||
|
||
# Copyright IBM Corp, All Rights Reserved. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
from .user import ADMIN, OPERATOR, COMMON_USER, \ | ||
User, LoginHistory |
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,7 @@ | ||
|
||
# Copyright IBM Corp, All Rights Reserved. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
from .register import Register | ||
from .login import Login |
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 @@ | ||
|
||
# Copyright IBM Corp, All Rights Reserved. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
from flask_restful import Resource, reqparse, fields, marshal_with | ||
from flask import url_for | ||
from flask_login import login_user, logout_user | ||
import logging | ||
import sys | ||
import os | ||
import bcrypt | ||
|
||
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..')) | ||
from common import log_handler, LOG_LEVEL | ||
from modules.user.user import User | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.setLevel(LOG_LEVEL) | ||
logger.addHandler(log_handler) | ||
|
||
login_fields = { | ||
"success": fields.Boolean, | ||
"id": fields.String, | ||
"next": fields.String, | ||
"error": fields.String | ||
} | ||
|
||
login_parser = reqparse.RequestParser() | ||
login_parser.add_argument('username', required=True, | ||
location='form', | ||
help='Username for create') | ||
login_parser.add_argument('password', required=True, | ||
location='form', | ||
help='Password for create') | ||
|
||
|
||
class Login(Resource): | ||
@marshal_with(login_fields) | ||
def post(self, **kwargs): | ||
args = login_parser.parse_args() | ||
username, password = args["username"], args["password"] | ||
|
||
user_obj = User() | ||
try: | ||
user = user_obj.get_by_username_w_password(username) | ||
if bcrypt.checkpw(password.encode('utf8'), | ||
bytes(user.password.encode())): | ||
login_user(user) | ||
user_id = str(user.id) | ||
data = { | ||
"success": True, | ||
"id": user_id, | ||
"next": url_for('bp_index.show') | ||
} | ||
return data, 200 | ||
else: | ||
data = { | ||
"success": False, | ||
"error": "Wrong username or password" | ||
} | ||
return data, 401 | ||
except Exception as exc: | ||
logger.info("error {}".format(exc)) | ||
data = { | ||
"success": False, | ||
"error": "login failed" | ||
} | ||
return data, 401 |
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,68 @@ | ||
|
||
# Copyright IBM Corp, All Rights Reserved. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
from flask_restful import Resource, reqparse, fields, marshal_with | ||
from flask_login import login_required | ||
import logging | ||
import sys | ||
import os | ||
from flask import current_app as app | ||
import bcrypt | ||
|
||
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..')) | ||
from common import log_handler, LOG_LEVEL | ||
from modules.models import ADMIN | ||
from modules.user.user import User | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.setLevel(LOG_LEVEL) | ||
logger.addHandler(log_handler) | ||
|
||
register_fields = { | ||
"username": fields.String, | ||
"apikey": fields.String, | ||
"isActivated": fields.Boolean, | ||
"balance": fields.Integer, | ||
"success": fields.Boolean, | ||
"error": fields.String | ||
} | ||
|
||
register_parser = reqparse.RequestParser() | ||
register_parser.add_argument('username', required=True, | ||
location='form', | ||
help='Username for create') | ||
register_parser.add_argument('password', required=True, | ||
location='form', | ||
help='Password for create') | ||
|
||
|
||
class Register(Resource): | ||
@login_required | ||
@marshal_with(register_fields) | ||
def post(self, **kwargs): | ||
args = register_parser.parse_args() | ||
username, password = args["username"], args["password"] | ||
salt = app.config.get("SALT", b"") | ||
password = bcrypt.hashpw(password.encode('utf8'), bytes(salt.encode())) | ||
|
||
try: | ||
user = User(username, password) | ||
user_id = user.save() | ||
user = user.get_by_id(user_id) | ||
data = { | ||
"username": user.username, | ||
"apikey": str(user.id), | ||
"isActivated": user.active, | ||
"balance": user.balance, | ||
"success": True | ||
} | ||
return data, 200 | ||
except Exception as exc: | ||
logger.error("exc %s", exc) | ||
data = { | ||
"success": False, | ||
"error": "register failed" | ||
} | ||
return data, 400 |
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
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,46 @@ | ||
|
||
# Copyright IBM Corp, All Rights Reserved. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
from flask_restful import Resource, fields, marshal_with | ||
from flask_login import login_required | ||
import logging | ||
import sys | ||
import os | ||
|
||
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..')) | ||
from common import log_handler, LOG_LEVEL | ||
from modules.user.user import User | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.setLevel(LOG_LEVEL) | ||
logger.addHandler(log_handler) | ||
|
||
user_info_fields = { | ||
"username": fields.String, | ||
"apikey": fields.String, | ||
"isActivated": fields.Boolean, | ||
"balance": fields.Integer, | ||
"success": fields.Boolean, | ||
"error": fields.String | ||
} | ||
|
||
|
||
class UserInfo(Resource): | ||
@marshal_with(user_info_fields) | ||
def get(self, user_id): | ||
user_obj = User() | ||
user = user_obj.get_by_id(user_id) | ||
if not user: | ||
return {"error": "No such User", "success": False}, 400 | ||
|
||
data = { | ||
"username": user.username, | ||
"apikey": str(user.id), | ||
"isActivated": user.active, | ||
"balance": user.balance, | ||
"success": True | ||
} | ||
|
||
return data, 200 |
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
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.