Sin descripción

__init__.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # IRIS Source Code
  2. # Copyright (C) 2024 - DFIR-IRIS
  3. # contact@dfir-iris.org
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 3 of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program; if not, write to the Free Software Foundation,
  17. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. from flask import Blueprint, session
  19. from flask import redirect, url_for
  20. from flask import request
  21. from flask_login import current_user, logout_user
  22. from oic.oauth2.exception import GrantError
  23. from app import app
  24. from app import db
  25. from app import oidc_client
  26. from app.blueprints.access_controls import is_authentication_ldap
  27. from app.blueprints.access_controls import is_authentication_oidc
  28. from app.blueprints.access_controls import not_authenticated_redirection_url
  29. from app.blueprints.rest.endpoints import response_api_error
  30. from app.blueprints.rest.endpoints import response_api_success
  31. from app.business.auth import validate_ldap_login, validate_local_login
  32. from app.iris_engine.utils.tracker import track_activity
  33. from app.schema.marshables import UserSchema
  34. auth_blueprint = Blueprint('auth', __name__, url_prefix='/auth')
  35. # TODO put this endpoint back after thinking about it (doesn't feel REST)
  36. #@auth_blueprint.post('/login')
  37. def login():
  38. """
  39. Login endpoint. Handles taking user/pass combo and authenticating a local session or returning an error.
  40. """
  41. if current_user.is_authenticated:
  42. return response_api_success('User already authenticated')
  43. if is_authentication_oidc() and app.config.get('AUTHENTICATION_LOCAL_FALLBACK') is False:
  44. return redirect(url_for('login.oidc_login'))
  45. username = request.json.get('username')
  46. password = request.json.get('password')
  47. if is_authentication_ldap() is True:
  48. authed_user = validate_ldap_login(
  49. username, password, app.config.get('AUTHENTICATION_LOCAL_FALLBACK'))
  50. else:
  51. authed_user = validate_local_login(username, password)
  52. if authed_user is None:
  53. return response_api_error('Invalid credentials')
  54. return response_api_success(data=authed_user)
  55. # TODO put this endpoint back after thinking about it (doesn't feel REST)
  56. #@auth_blueprint.get('/logout')
  57. def logout():
  58. """
  59. Logout function. Erase its session and redirect to index i.e login
  60. :return: Page
  61. """
  62. if session['current_case']:
  63. current_user.ctx_case = session['current_case']['case_id']
  64. current_user.ctx_human_case = session['current_case']['case_name']
  65. db.session.commit()
  66. if is_authentication_oidc():
  67. if oidc_client.provider_info.get("end_session_endpoint"):
  68. try:
  69. logout_request = oidc_client.construct_EndSessionRequest(
  70. state=session["oidc_state"])
  71. logout_url = logout_request.request(
  72. oidc_client.provider_info["end_session_endpoint"])
  73. track_activity("user '{}' has been logged-out".format(
  74. current_user.user), ctx_less=True, display_in_ui=False)
  75. logout_user()
  76. session.clear()
  77. return redirect(logout_url)
  78. except GrantError:
  79. track_activity(
  80. f"no oidc session found for user '{current_user.user}', skipping oidc provider logout and continuing to logout local user",
  81. ctx_less=True,
  82. display_in_ui=False
  83. )
  84. track_activity("user '{}' has been logged-out".format(current_user.user),
  85. ctx_less=True, display_in_ui=False)
  86. logout_user()
  87. session.clear()
  88. return redirect(not_authenticated_redirection_url('/'))
  89. # TODO shouldn't we rather have /api/v2/users/{identifier}?
  90. #@auth_blueprint.route('/whoami', methods=['GET'])
  91. def whoami():
  92. """
  93. Returns information about the currently authenticated user.
  94. """
  95. # Ensure we are authenticated
  96. if not current_user.is_authenticated:
  97. return response_api_error("Unauthenticated")
  98. # Return the current_user dict
  99. return response_api_success(data=UserSchema(only=[
  100. 'id', 'user_name', 'user_login', 'user_email'
  101. ]).dump(current_user))