authenticates.rb 4.8 KB

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