Add information of login to user log

This commit is contained in:
Firmlyzhu 2018-09-16 15:52:15 +08:00
parent 46ea09828d
commit 987077e7d7
3 changed files with 17 additions and 8 deletions

View File

@ -254,7 +254,7 @@ class userManager:
} }
return result return result
def auth_external(self, form): def auth_external(self, form, userip=""):
if (env.getenv('EXTERNAL_LOGIN') != 'True'): if (env.getenv('EXTERNAL_LOGIN') != 'True'):
failed_result = {'success': 'false', 'reason' : 'external auth disabled'} failed_result = {'success': 'false', 'reason' : 'external auth disabled'}
@ -267,6 +267,7 @@ class userManager:
return failed_result return failed_result
username = result['username'] username = result['username']
logger.info("External login success: username=%s, userip=%s" % (username, userip))
user = User.query.filter_by(username = username).first() user = User.query.filter_by(username = username).first()
if (user != None and user.auth_method == result['auth_method']): if (user != None and user.auth_method == result['auth_method']):
result = { result = {
@ -316,18 +317,24 @@ class userManager:
} }
return result return result
def auth(self, username, password): def auth(self, username, password, userip=""):
''' '''
authenticate a user by username & password authenticate a user by username & password
return a token as well as some user information return a token as well as some user information
''' '''
user = User.query.filter_by(username = username).first() user = User.query.filter_by(username = username).first()
result = {}
if (user == None or user.auth_method =='pam'): if (user == None or user.auth_method =='pam'):
return self.auth_pam(username, password) result = self.auth_pam(username, password)
elif (user.auth_method == 'local'): elif (user.auth_method == 'local'):
return self.auth_local(username, password) result = self.auth_local(username, password)
else: else:
result = {'success':'false', 'reason':'auth_method error'} result = {'success':'false', 'reason':'auth_method error'}
if result['success'] == 'true':
logger.info("Login success: username=%s, userip=%s" % (result['data']['username'], userip))
else:
logger.info("Login failed: userip=%s" % (userip))
return result return result
def auth_token(self, token): def auth_token(self, token):

View File

@ -87,9 +87,10 @@ def login():
logger.info("handle request : user login") logger.info("handle request : user login")
user = request.form.get("user", None) user = request.form.get("user", None)
key = request.form.get("key", None) key = request.form.get("key", None)
userip = request.form.get("ip", "")
if user == None or key == None: if user == None or key == None:
return json.dumps({'success': 'false', 'message':'user or key is null'}) return json.dumps({'success': 'false', 'message':'user or key is null'})
auth_result = G_usermgr.auth(user,key) auth_result = G_usermgr.auth(user,key,userip)
if auth_result['success'] == 'false': if auth_result['success'] == 'false':
logger.info("%s login failed" % user) logger.info("%s login failed" % user)
return json.dumps({'success':'false', 'message':'auth failed'}) return json.dumps({'success':'false', 'message':'auth failed'})
@ -100,8 +101,9 @@ def login():
def external_login(): def external_login():
global G_usermgr global G_usermgr
logger.info("handle request : external user login") logger.info("handle request : external user login")
userip = request.form.get("ip", "")
try: try:
result = G_usermgr.auth_external(request.form) result = G_usermgr.auth_external(request.form,userip)
return json.dumps(result) return json.dumps(result)
except: except:
result = {'success':'false', 'reason':'Something wrong happened when auth an external account'} result = {'success':'false', 'reason':'Something wrong happened when auth an external account'}

View File

@ -52,7 +52,7 @@ class loginView(normalView):
@classmethod @classmethod
def post(self): def post(self):
if (request.form['username']): if (request.form['username']):
data = {"user": request.form['username'], "key": request.form['password']} data = {"user": request.form['username'], "key": request.form['password'], 'ip': request.remote_addr}
result = dockletRequest.unauthorizedpost('/login/', data) result = dockletRequest.unauthorizedpost('/login/', data)
ok = result and result.get('success', None) ok = result and result.get('success', None)
if (ok and (ok == "true")): if (ok and (ok == "true")):