Skip to content

Commit 5023982

Browse files
Hieu Lam - TMAntthitrinhcweitat
authored
feature-8986: API to get user ID with JWT token (#8989)
* feature-8986: API to get user ID with JWT token * feature-8986: API to get user ID with JWT token * feature-8986: API to get user ID with JWT token * feature-8986: API to get user ID with JWT token * feature-8986: Update documentation for API --------- Co-authored-by: ntthitrinh <[email protected]> Co-authored-by: cweitat <[email protected]>
1 parent 2b88d9c commit 5023982

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

app/api/custom/users.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import jwt
2+
from flask import Blueprint, current_app, request
3+
4+
from app.api.helpers.permissions import jwt_required
5+
6+
users_routes = Blueprint('users_routes', __name__, url_prefix='/v1/users')
7+
8+
9+
@users_routes.route('/user-details/get-user-id', methods=['GET'])
10+
@jwt_required
11+
def get_user_id():
12+
"""
13+
Get user id from token
14+
"""
15+
token = None
16+
if "Authorization" in request.headers:
17+
token = request.headers["Authorization"].split(" ")[1]
18+
if not token:
19+
return {
20+
"message": "Authentication Token is missing!",
21+
"data": None,
22+
"error": "Unauthorized",
23+
}, 401
24+
try:
25+
data = jwt.decode(token, current_app.config["SECRET_KEY"], algorithms=["HS256"])
26+
if not data.get('identity', False):
27+
return {"message": "Can't get user id!", "data": None}, 404
28+
return {"user_id": data["identity"]}, 200
29+
except UnicodeDecodeError:
30+
return {"message": "Can't get user id!", "data": None}, 500

app/instance.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ def create_app():
169169
from app.api.custom.group_role_invite import group_role_invites_routes
170170
from app.api.video_stream import streams_routes
171171
from app.api.events import events_blueprint
172+
from app.api.custom.users import users_routes
172173

173174
app.register_blueprint(api_v1)
174175
app.register_blueprint(event_copy)
@@ -202,6 +203,7 @@ def create_app():
202203
app.register_blueprint(events_blueprint)
203204
app.register_blueprint(tickets_routes)
204205
app.register_blueprint(group_role_invites_routes)
206+
app.register_blueprint(users_routes)
205207

206208
add_engine_pidguard(db.engine)
207209

docs/api/blueprint/user/users.apib

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,3 +1471,24 @@ Check if the email passed is available or not. True is returned if the email is
14711471
{
14721472
"exists": true
14731473
}
1474+
1475+
1476+
## Get User ID [/v1/users/user-details/get-user-id]
1477+
1478+
### Get User ID [GET]
1479+
1480+
Get the user id using JWT token
1481+
1482+
+ Request
1483+
1484+
+ Headers
1485+
1486+
Accept: application/json
1487+
1488+
Authorization: JWT <Auth Key>
1489+
1490+
+ Response 200 (application/json)
1491+
1492+
{
1493+
"user_id": "2"
1494+
}

tests/all/integration/api/helpers/test_auth.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
13
from flask_login import login_user, logout_user
24

35
from app.api.helpers.auth import AuthManager
@@ -41,3 +43,16 @@ def test_check_auth_admin(db):
4143
user.is_admin = False
4244
status = AuthManager.check_auth_admin('[email protected]', 'password')
4345
assert False == status
46+
47+
48+
def test_get_user_id(client, jwt):
49+
"""Method to test get user id"""
50+
51+
response = client.get(
52+
'/v1/users/user-details/get-user-id',
53+
content_type='application/vnd.api+json',
54+
headers=jwt,
55+
)
56+
57+
assert response.status_code == 200
58+
assert json.loads(response.data)['user_id']

0 commit comments

Comments
 (0)