File tree Expand file tree Collapse file tree 4 files changed +68
-0
lines changed
tests/all/integration/api/helpers Expand file tree Collapse file tree 4 files changed +68
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff line change 1+ import json
2+
13from flask_login import login_user , logout_user
24
35from 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 ('authtest2@gmail.com' , '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' ]
You can’t perform that action at this time.
0 commit comments