authenticates.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. module ApplicationController::Authenticates
  2. extend ActiveSupport::Concern
  3. private
  4. def permission_check(key)
  5. if @_token_auth
  6. user = Token.check(
  7. action: 'api',
  8. name: @_token_auth,
  9. permission: key,
  10. )
  11. return false if user
  12. raise Exceptions::NotAuthorized, 'Not authorized (token)!'
  13. end
  14. return false if current_user&.permissions?(key)
  15. raise Exceptions::NotAuthorized, 'Not authorized (user)!'
  16. end
  17. def authentication_check(auth_param = {})
  18. user = authentication_check_only(auth_param)
  19. # check if basic_auth fallback is possible
  20. if auth_param[:basic_auth_promt] && !user
  21. return request_http_basic_authentication
  22. end
  23. # return auth not ok
  24. if !user
  25. raise Exceptions::NotAuthorized, 'authentication failed'
  26. end
  27. # return auth ok
  28. true
  29. end
  30. def authentication_check_only(auth_param = {})
  31. #logger.debug 'authentication_check'
  32. #logger.debug params.inspect
  33. #logger.debug session.inspect
  34. #logger.debug cookies.inspect
  35. # already logged in, early exit
  36. if session.id && session[:user_id]
  37. logger.debug { 'session based auth check' }
  38. user = User.lookup(id: session[:user_id])
  39. return authentication_check_prerequesits(user, 'session', auth_param) if user
  40. end
  41. # check http basic based authentication
  42. authenticate_with_http_basic do |username, password|
  43. request.session_options[:skip] = true # do not send a session cookie
  44. logger.debug { "http basic auth check '#{username}'" }
  45. if Setting.get('api_password_access') == false
  46. raise Exceptions::NotAuthorized, 'API password access disabled!'
  47. end
  48. user = User.authenticate(username, password)
  49. return authentication_check_prerequesits(user, 'basic_auth', auth_param) if user
  50. end
  51. # check http token based authentication
  52. authenticate_with_http_token do |token_string, _options|
  53. logger.debug { "http token auth check '#{token_string}'" }
  54. request.session_options[:skip] = true # do not send a session cookie
  55. if Setting.get('api_token_access') == false
  56. raise Exceptions::NotAuthorized, 'API token access disabled!'
  57. end
  58. user = Token.check(
  59. action: 'api',
  60. name: token_string,
  61. inactive_user: true,
  62. )
  63. if user && auth_param[:permission]
  64. user = Token.check(
  65. action: 'api',
  66. name: token_string,
  67. permission: auth_param[:permission],
  68. inactive_user: true,
  69. )
  70. raise Exceptions::NotAuthorized, 'Not authorized (token)!' if !user
  71. end
  72. if user
  73. token = Token.find_by(name: token_string)
  74. token.last_used_at = Time.zone.now
  75. token.save!
  76. if token.expires_at &&
  77. Time.zone.today >= token.expires_at
  78. raise Exceptions::NotAuthorized, 'Not authorized (token expired)!'
  79. end
  80. end
  81. @_token_auth = token_string # remember for permission_check
  82. return authentication_check_prerequesits(user, 'token_auth', auth_param) if user
  83. end
  84. # check oauth2 token based authentication
  85. token = Doorkeeper::OAuth::Token.from_bearer_authorization(request)
  86. if token
  87. request.session_options[:skip] = true # do not send a session cookie
  88. logger.debug { "oauth2 token auth check '#{token}'" }
  89. access_token = Doorkeeper::AccessToken.by_token(token)
  90. raise Exceptions::NotAuthorized, 'Invalid token!' if !access_token
  91. # check expire
  92. if access_token.expires_in && (access_token.created_at + access_token.expires_in) < Time.zone.now
  93. raise Exceptions::NotAuthorized, 'OAuth2 token is expired!'
  94. end
  95. # if access_token.scopes.empty?
  96. # raise Exceptions::NotAuthorized, 'OAuth2 scope missing for token!'
  97. # end
  98. user = User.find(access_token.resource_owner_id)
  99. return authentication_check_prerequesits(user, 'token_auth', auth_param) if user
  100. end
  101. false
  102. end
  103. def authenticate_with_password
  104. user = User.authenticate(params[:username], params[:password])
  105. raise Exceptions::NotAuthorized, 'Wrong Username or Password combination.' if !user
  106. session.delete(:switched_from_user_id)
  107. authentication_check_prerequesits(user, 'session', {})
  108. end
  109. def authenticate_with_sso
  110. user = begin
  111. login = request.env['REMOTE_USER'] ||
  112. request.env['HTTP_REMOTE_USER'] ||
  113. request.headers['X-Forwarded-User']
  114. User.lookup(login: login&.downcase)
  115. end
  116. raise Exceptions::NotAuthorized, 'Missing SSO ENV REMOTE_USER' if !user
  117. session.delete(:switched_from_user_id)
  118. authentication_check_prerequesits(user, 'SSO', {})
  119. end
  120. def authentication_check_prerequesits(user, auth_type, auth_param)
  121. raise Exceptions::NotAuthorized, 'Maintenance mode enabled!' if in_maintenance_mode?(user)
  122. raise Exceptions::NotAuthorized, 'User is inactive!' if !user.active
  123. raise Exceptions::NotAuthorized, 'Not authorized (user)!' if auth_param[:permission] && !user.permissions?(auth_param[:permission])
  124. current_user_set(user, auth_type)
  125. user_device_log(user, auth_type)
  126. logger.debug { "#{auth_type} for '#{user.login}'" }
  127. user
  128. end
  129. end