1- from flask import request
1+ from flask import request , session
22from flask_restx import Resource
33from flask_restx import fields
44
55from mindsdb .__about__ import __version__ as mindsdb_version
66from mindsdb .api .http .namespaces .configs .default import ns_conf
77from mindsdb .api .http .utils import http_error
88from mindsdb .metrics .metrics import api_endpoint_metrics
9- from mindsdb .utilities .config import Config
9+ from mindsdb .utilities .config import config , HTTP_AUTH_TYPE
1010from mindsdb .utilities import log
1111from mindsdb .api .common .middleware import generate_pat , revoke_pat , verify_pat
1212
1313
1414logger = log .getLogger (__name__ )
1515
1616
17+ def check_session_auth () -> bool :
18+ """checking whether current user is authenticated
19+
20+ Returns:
21+ bool: True if user authentication is approved
22+ """
23+ try :
24+ if config ["auth" ]["http_auth_enabled" ] is False :
25+ return True
26+ return session .get ("username" ) == config ["auth" ]["username" ]
27+ except Exception :
28+ return False
29+
30+
1731@ns_conf .route ("/login" , methods = ["POST" ])
1832class LoginRoute (Resource ):
1933 @ns_conf .doc (
@@ -36,7 +50,6 @@ def post(self):
3650 ):
3751 return http_error (400 , "Error in username or password" , "Username and password should be string" )
3852
39- config = Config ()
4053 inline_username = config ["auth" ]["username" ]
4154 inline_password = config ["auth" ]["password" ]
4255
@@ -45,14 +58,24 @@ def post(self):
4558
4659 logger .info (f"User '{ username } ' logged in successfully" )
4760
48- return {"token" : generate_pat ()}, 200
61+ response = {}
62+ if config ["auth" ]["http_auth_type" ] in (HTTP_AUTH_TYPE .SESSION , HTTP_AUTH_TYPE .SESSION_OR_TOKEN ):
63+ session .clear ()
64+ session ["username" ] = username
65+ session .permanent = True
66+
67+ if config ["auth" ]["http_auth_type" ] in (HTTP_AUTH_TYPE .TOKEN , HTTP_AUTH_TYPE .SESSION_OR_TOKEN ):
68+ response ["token" ] = generate_pat ()
69+
70+ return response , 200
4971
5072
5173@ns_conf .route ("/logout" , methods = ["POST" ])
5274class LogoutRoute (Resource ):
5375 @ns_conf .doc (responses = {200 : "Success" })
5476 @api_endpoint_metrics ("POST" , "/default/logout" )
5577 def post (self ):
78+ session .clear ()
5679 # We can't forcibly log out a user with the
5780 h = request .headers .get ("Authorization" )
5881 if not h or not h .startswith ("Bearer " ):
@@ -89,7 +112,6 @@ class StatusRoute(Resource):
89112 def get (self ):
90113 """returns auth and environment data"""
91114 environment = "local"
92- config = Config ()
93115
94116 environment = config .get ("environment" )
95117 if environment is None :
@@ -107,11 +129,20 @@ def get(self):
107129 else :
108130 auth_provider = "local"
109131
132+ auth_confirmed = False
133+ auth_type = config ["auth" ]["http_auth_type" ]
134+ if auth_type in (HTTP_AUTH_TYPE .SESSION , HTTP_AUTH_TYPE .SESSION_OR_TOKEN ):
135+ auth_confirmed = auth_confirmed or check_session_auth ()
136+ if auth_type in (HTTP_AUTH_TYPE .TOKEN , HTTP_AUTH_TYPE .SESSION_OR_TOKEN ):
137+ auth_confirmed = auth_confirmed or verify_pat (
138+ request .headers .get ("Authorization" , "" ).replace ("Bearer " , "" )
139+ )
140+
110141 resp = {
111142 "mindsdb_version" : mindsdb_version ,
112143 "environment" : environment ,
113144 "auth" : {
114- "confirmed" : verify_pat ( request . headers . get ( "Authorization" , "" ). replace ( "Bearer " , "" )) ,
145+ "confirmed" : auth_confirmed ,
115146 "http_auth_enabled" : config ["auth" ]["http_auth_enabled" ],
116147 "provider" : auth_provider ,
117148 },
0 commit comments