2017-12-11 08:07:22 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-10-09 12:10:37 +08:00
|
|
|
require_dependency "auth/current_user_provider"
|
2016-07-28 10:58:49 +08:00
|
|
|
require_dependency "rate_limiter"
|
2013-10-09 12:10:37 +08:00
|
|
|
|
|
|
|
class Auth::DefaultCurrentUserProvider
|
|
|
|
|
2014-05-23 06:13:25 +08:00
|
|
|
CURRENT_USER_KEY ||= "_DISCOURSE_CURRENT_USER".freeze
|
|
|
|
API_KEY ||= "api_key".freeze
|
2016-08-18 12:38:33 +08:00
|
|
|
USER_API_KEY ||= "HTTP_USER_API_KEY".freeze
|
2016-10-14 13:05:27 +08:00
|
|
|
USER_API_CLIENT_ID ||= "HTTP_USER_API_CLIENT_ID".freeze
|
2014-05-23 06:13:25 +08:00
|
|
|
API_KEY_ENV ||= "_DISCOURSE_API".freeze
|
2016-12-16 09:05:20 +08:00
|
|
|
USER_API_KEY_ENV ||= "_DISCOURSE_USER_API".freeze
|
2014-05-23 06:13:25 +08:00
|
|
|
TOKEN_COOKIE ||= "_t".freeze
|
|
|
|
PATH_INFO ||= "PATH_INFO".freeze
|
2016-07-28 10:58:49 +08:00
|
|
|
COOKIE_ATTEMPTS_PER_MIN ||= 10
|
2013-10-09 12:10:37 +08:00
|
|
|
|
|
|
|
# do all current user initialization here
|
|
|
|
def initialize(env)
|
|
|
|
@env = env
|
|
|
|
@request = Rack::Request.new(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
# our current user, return nil if none is found
|
|
|
|
def current_user
|
|
|
|
return @env[CURRENT_USER_KEY] if @env.key?(CURRENT_USER_KEY)
|
|
|
|
|
2014-10-24 10:38:00 +08:00
|
|
|
# bypass if we have the shared session header
|
|
|
|
if shared_key = @env['HTTP_X_SHARED_SESSION_KEY']
|
|
|
|
uid = $redis.get("shared_session_key_#{shared_key}")
|
|
|
|
user = nil
|
|
|
|
if uid
|
|
|
|
user = User.find_by(id: uid.to_i)
|
|
|
|
end
|
|
|
|
@env[CURRENT_USER_KEY] = user
|
|
|
|
return user
|
|
|
|
end
|
|
|
|
|
2014-05-23 06:13:25 +08:00
|
|
|
request = @request
|
2013-10-09 12:10:37 +08:00
|
|
|
|
2017-02-18 00:02:33 +08:00
|
|
|
user_api_key = @env[USER_API_KEY]
|
2017-12-12 16:40:35 +08:00
|
|
|
api_key = @env.blank? ? nil : request[API_KEY]
|
2017-02-18 00:02:33 +08:00
|
|
|
|
|
|
|
auth_token = request.cookies[TOKEN_COOKIE] unless user_api_key || api_key
|
2013-10-09 12:10:37 +08:00
|
|
|
|
|
|
|
current_user = nil
|
|
|
|
|
|
|
|
if auth_token && auth_token.length == 32
|
2017-07-28 09:20:09 +08:00
|
|
|
limiter = RateLimiter.new(nil, "cookie_auth_#{request.ip}", COOKIE_ATTEMPTS_PER_MIN , 60)
|
2016-08-09 08:02:18 +08:00
|
|
|
|
|
|
|
if limiter.can_perform?
|
2017-02-14 03:01:01 +08:00
|
|
|
@user_token = UserAuthToken.lookup(auth_token,
|
|
|
|
seen: true,
|
|
|
|
user_agent: @env['HTTP_USER_AGENT'],
|
2017-03-08 02:27:34 +08:00
|
|
|
path: @env['REQUEST_PATH'],
|
2017-02-14 03:01:01 +08:00
|
|
|
client_ip: @request.ip)
|
|
|
|
|
2017-02-01 06:21:37 +08:00
|
|
|
current_user = @user_token.try(:user)
|
2016-08-09 08:02:18 +08:00
|
|
|
end
|
2016-07-28 10:58:49 +08:00
|
|
|
|
|
|
|
unless current_user
|
|
|
|
begin
|
2016-08-09 08:02:18 +08:00
|
|
|
limiter.performed!
|
2016-07-28 10:58:49 +08:00
|
|
|
rescue RateLimiter::LimitExceeded
|
|
|
|
raise Discourse::InvalidAccess
|
|
|
|
end
|
|
|
|
end
|
2013-10-09 12:10:37 +08:00
|
|
|
end
|
|
|
|
|
2014-05-23 06:13:25 +08:00
|
|
|
if current_user && should_update_last_seen?
|
2014-03-17 08:59:34 +08:00
|
|
|
u = current_user
|
2014-07-18 04:22:46 +08:00
|
|
|
Scheduler::Defer.later "Updating Last Seen" do
|
2014-03-17 08:59:34 +08:00
|
|
|
u.update_last_seen!
|
|
|
|
u.update_ip_address!(request.ip)
|
|
|
|
end
|
2013-10-09 12:10:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# possible we have an api call, impersonate
|
2017-02-18 00:02:33 +08:00
|
|
|
if api_key
|
2014-05-23 06:13:25 +08:00
|
|
|
current_user = lookup_api_user(api_key, request)
|
2017-10-20 22:30:13 +08:00
|
|
|
raise Discourse::InvalidAccess.new(I18n.t('invalid_api_credentials'), nil, custom_message: "invalid_api_credentials") unless current_user
|
2017-02-18 00:02:33 +08:00
|
|
|
raise Discourse::InvalidAccess if current_user.suspended? || !current_user.active
|
2014-05-23 06:13:25 +08:00
|
|
|
@env[API_KEY_ENV] = true
|
2017-12-11 08:07:22 +08:00
|
|
|
|
|
|
|
limiter_min = RateLimiter.new(nil, "admin_api_min_#{api_key}", GlobalSetting.max_admin_api_reqs_per_key_per_minute, 60)
|
|
|
|
limiter_min.performed!
|
2013-10-09 12:10:37 +08:00
|
|
|
end
|
|
|
|
|
2016-08-15 15:58:33 +08:00
|
|
|
# user api key handling
|
2017-02-18 00:02:33 +08:00
|
|
|
if user_api_key
|
2016-08-15 15:58:33 +08:00
|
|
|
|
2017-12-11 08:07:22 +08:00
|
|
|
limiter_min = RateLimiter.new(nil, "user_api_min_#{user_api_key}", GlobalSetting.max_user_api_reqs_per_minute, 60)
|
|
|
|
limiter_day = RateLimiter.new(nil, "user_api_day_#{user_api_key}", GlobalSetting.max_user_api_reqs_per_day, 86400)
|
2016-08-15 15:58:33 +08:00
|
|
|
|
|
|
|
unless limiter_day.can_perform?
|
2016-08-26 08:38:46 +08:00
|
|
|
limiter_day.performed!
|
2016-08-15 15:58:33 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
unless limiter_min.can_perform?
|
2016-08-26 08:38:46 +08:00
|
|
|
limiter_min.performed!
|
2016-08-15 15:58:33 +08:00
|
|
|
end
|
|
|
|
|
2017-02-18 00:02:33 +08:00
|
|
|
current_user = lookup_user_api_user_and_update_key(user_api_key, @env[USER_API_CLIENT_ID])
|
2016-08-15 15:58:33 +08:00
|
|
|
raise Discourse::InvalidAccess unless current_user
|
2017-02-18 00:02:33 +08:00
|
|
|
raise Discourse::InvalidAccess if current_user.suspended? || !current_user.active
|
2016-08-15 15:58:33 +08:00
|
|
|
|
|
|
|
limiter_min.performed!
|
|
|
|
limiter_day.performed!
|
|
|
|
|
2016-12-16 09:05:20 +08:00
|
|
|
@env[USER_API_KEY_ENV] = true
|
2016-08-15 15:58:33 +08:00
|
|
|
end
|
|
|
|
|
2017-02-18 00:02:33 +08:00
|
|
|
# keep this rule here as a safeguard
|
|
|
|
# under no conditions to suspended or inactive accounts get current_user
|
|
|
|
if current_user && (current_user.suspended? || !current_user.active)
|
|
|
|
current_user = nil
|
|
|
|
end
|
|
|
|
|
2013-10-09 12:10:37 +08:00
|
|
|
@env[CURRENT_USER_KEY] = current_user
|
|
|
|
end
|
|
|
|
|
2016-07-25 10:07:31 +08:00
|
|
|
def refresh_session(user, session, cookies)
|
2016-08-15 15:58:33 +08:00
|
|
|
|
2017-02-01 06:21:37 +08:00
|
|
|
# if user was not loaded, no point refreshing session
|
|
|
|
# it could be an anonymous path, this would add cost
|
|
|
|
return if is_api? || !@env.key?(CURRENT_USER_KEY)
|
|
|
|
|
2017-02-18 00:02:33 +08:00
|
|
|
if !is_user_api? && @user_token && @user_token.user == user
|
2017-02-01 06:21:37 +08:00
|
|
|
rotated_at = @user_token.rotated_at
|
|
|
|
|
|
|
|
needs_rotation = @user_token.auth_token_seen ? rotated_at < UserAuthToken::ROTATE_TIME.ago : rotated_at < UserAuthToken::URGENT_ROTATE_TIME.ago
|
|
|
|
|
|
|
|
if !@user_token.legacy && needs_rotation
|
|
|
|
if @user_token.rotate!(user_agent: @env['HTTP_USER_AGENT'],
|
2017-07-28 09:20:09 +08:00
|
|
|
client_ip: @request.ip,
|
|
|
|
path: @env['REQUEST_PATH'])
|
2017-02-01 06:21:37 +08:00
|
|
|
cookies[TOKEN_COOKIE] = cookie_hash(@user_token.unhashed_auth_token)
|
|
|
|
end
|
|
|
|
elsif @user_token.legacy
|
|
|
|
# make a new token
|
|
|
|
log_on_user(user, session, cookies)
|
|
|
|
end
|
2016-07-25 10:07:31 +08:00
|
|
|
end
|
2017-02-01 06:21:37 +08:00
|
|
|
|
2016-07-28 10:58:49 +08:00
|
|
|
if !user && cookies.key?(TOKEN_COOKIE)
|
2017-03-08 02:27:34 +08:00
|
|
|
cookies.delete(TOKEN_COOKIE)
|
2016-07-28 10:58:49 +08:00
|
|
|
end
|
2016-07-25 10:07:31 +08:00
|
|
|
end
|
|
|
|
|
2013-10-09 12:10:37 +08:00
|
|
|
def log_on_user(user, session, cookies)
|
2017-02-01 06:21:37 +08:00
|
|
|
@user_token = UserAuthToken.generate!(user_id: user.id,
|
|
|
|
user_agent: @env['HTTP_USER_AGENT'],
|
2017-03-08 02:27:34 +08:00
|
|
|
path: @env['REQUEST_PATH'],
|
2017-02-01 06:21:37 +08:00
|
|
|
client_ip: @request.ip)
|
2016-07-26 09:37:41 +08:00
|
|
|
|
2017-02-01 06:21:37 +08:00
|
|
|
cookies[TOKEN_COOKIE] = cookie_hash(@user_token.unhashed_auth_token)
|
2013-11-02 07:25:43 +08:00
|
|
|
make_developer_admin(user)
|
2016-04-27 01:08:19 +08:00
|
|
|
enable_bootstrap_mode(user)
|
2013-10-09 12:10:37 +08:00
|
|
|
@env[CURRENT_USER_KEY] = user
|
|
|
|
end
|
|
|
|
|
2017-02-01 06:21:37 +08:00
|
|
|
def cookie_hash(unhashed_auth_token)
|
2017-02-24 01:01:28 +08:00
|
|
|
hash = {
|
2017-02-01 06:21:37 +08:00
|
|
|
value: unhashed_auth_token,
|
2016-10-17 09:11:15 +08:00
|
|
|
httponly: true,
|
|
|
|
expires: SiteSetting.maximum_session_age.hours.from_now,
|
2017-06-22 04:18:24 +08:00
|
|
|
secure: SiteSetting.force_https
|
2016-10-17 09:11:15 +08:00
|
|
|
}
|
2017-02-24 01:01:28 +08:00
|
|
|
|
|
|
|
if SiteSetting.same_site_cookies != "Disabled"
|
|
|
|
hash[:same_site] = SiteSetting.same_site_cookies
|
|
|
|
end
|
|
|
|
|
|
|
|
hash
|
2016-10-17 09:11:15 +08:00
|
|
|
end
|
|
|
|
|
2013-11-02 07:25:43 +08:00
|
|
|
def make_developer_admin(user)
|
|
|
|
if user.active? &&
|
|
|
|
!user.admin &&
|
|
|
|
Rails.configuration.respond_to?(:developer_emails) &&
|
|
|
|
Rails.configuration.developer_emails.include?(user.email)
|
2014-03-24 15:03:39 +08:00
|
|
|
user.admin = true
|
|
|
|
user.save
|
2013-11-02 07:25:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-27 01:08:19 +08:00
|
|
|
def enable_bootstrap_mode(user)
|
|
|
|
Jobs.enqueue(:enable_bootstrap_mode, user_id: user.id) if user.admin && user.last_seen_at.nil? && !SiteSetting.bootstrap_mode_enabled && user.is_singular_admin?
|
|
|
|
end
|
|
|
|
|
2013-10-09 12:10:37 +08:00
|
|
|
def log_off_user(session, cookies)
|
2017-02-01 06:21:37 +08:00
|
|
|
user = current_user
|
|
|
|
if SiteSetting.log_out_strict && user
|
|
|
|
user.user_auth_tokens.destroy_all
|
2016-05-18 15:27:54 +08:00
|
|
|
|
|
|
|
if user.admin && defined?(Rack::MiniProfiler)
|
|
|
|
# clear the profiling cookie to keep stuff tidy
|
2016-07-28 10:58:49 +08:00
|
|
|
cookies.delete("__profilin")
|
2016-05-18 15:27:54 +08:00
|
|
|
end
|
|
|
|
|
2016-07-04 17:20:30 +08:00
|
|
|
user.logged_out
|
2017-02-01 06:21:37 +08:00
|
|
|
elsif user && @user_token
|
|
|
|
@user_token.destroy
|
2015-01-28 09:56:25 +08:00
|
|
|
end
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2016-07-28 10:58:49 +08:00
|
|
|
cookies.delete(TOKEN_COOKIE)
|
2013-10-09 12:10:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# api has special rights return true if api was detected
|
|
|
|
def is_api?
|
|
|
|
current_user
|
2016-12-16 09:05:20 +08:00
|
|
|
!!(@env[API_KEY_ENV])
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_user_api?
|
|
|
|
current_user
|
|
|
|
!!(@env[USER_API_KEY_ENV])
|
2013-10-09 12:10:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def has_auth_cookie?
|
2014-05-23 06:13:25 +08:00
|
|
|
cookie = @request.cookies[TOKEN_COOKIE]
|
2013-10-09 12:10:37 +08:00
|
|
|
!cookie.nil? && cookie.length == 32
|
|
|
|
end
|
2014-05-23 06:13:25 +08:00
|
|
|
|
|
|
|
def should_update_last_seen?
|
2017-03-01 01:34:57 +08:00
|
|
|
if @request.xhr?
|
|
|
|
@env["HTTP_DISCOURSE_VISIBLE".freeze] == "true".freeze
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
2014-05-23 06:13:25 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2016-10-14 13:05:27 +08:00
|
|
|
def lookup_user_api_user_and_update_key(user_api_key, client_id)
|
2016-08-16 15:06:33 +08:00
|
|
|
if api_key = UserApiKey.where(key: user_api_key, revoked_at: nil).includes(:user).first
|
2016-10-14 13:05:27 +08:00
|
|
|
unless api_key.allow?(@env)
|
|
|
|
raise Discourse::InvalidAccess
|
|
|
|
end
|
|
|
|
|
|
|
|
if client_id.present? && client_id != api_key.client_id
|
|
|
|
api_key.update_columns(client_id: client_id)
|
2016-08-15 15:58:33 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
api_key.user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-23 06:13:25 +08:00
|
|
|
def lookup_api_user(api_key_value, request)
|
2016-06-02 03:48:06 +08:00
|
|
|
if api_key = ApiKey.where(key: api_key_value).includes(:user).first
|
2014-05-23 06:13:25 +08:00
|
|
|
api_username = request["api_username"]
|
2014-11-20 12:21:49 +08:00
|
|
|
|
2016-06-02 03:48:06 +08:00
|
|
|
if api_key.allowed_ips.present? && !api_key.allowed_ips.any? { |ip| ip.include?(request.ip) }
|
|
|
|
Rails.logger.warn("[Unauthorized API Access] username: #{api_username}, IP address: #{request.ip}")
|
2014-11-20 12:21:49 +08:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2014-05-23 06:13:25 +08:00
|
|
|
if api_key.user
|
|
|
|
api_key.user if !api_username || (api_key.user.username_lower == api_username.downcase)
|
|
|
|
elsif api_username
|
|
|
|
User.find_by(username_lower: api_username.downcase)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-09 12:10:37 +08:00
|
|
|
end
|