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():
169
169
from app .api .custom .group_role_invite import group_role_invites_routes
170
170
from app .api .video_stream import streams_routes
171
171
from app .api .events import events_blueprint
172
+ from app .api .custom .users import users_routes
172
173
173
174
app .register_blueprint (api_v1 )
174
175
app .register_blueprint (event_copy )
@@ -202,6 +203,7 @@ def create_app():
202
203
app .register_blueprint (events_blueprint )
203
204
app .register_blueprint (tickets_routes )
204
205
app .register_blueprint (group_role_invites_routes )
206
+ app .register_blueprint (users_routes )
205
207
206
208
add_engine_pidguard (db .engine )
207
209
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
1471
1471
{
1472
1472
" exists" : true
1473
1473
}
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
+
1
3
from flask_login import login_user , logout_user
2
4
3
5
from app .api .helpers .auth import AuthManager
@@ -41,3 +43,16 @@ def test_check_auth_admin(db):
41
43
user .is_admin = False
42
44
status = AuthManager .
check_auth_admin (
'[email protected] ' ,
'password' )
43
45
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