2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2014-05-22 18:13:25 -04:00
|
|
|
require_dependency 'auth/default_current_user_provider'
|
|
|
|
|
|
|
|
describe Auth::DefaultCurrentUserProvider do
|
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
class TestProvider < Auth::DefaultCurrentUserProvider
|
|
|
|
attr_reader :env
|
|
|
|
def initialize(env)
|
|
|
|
super(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def provider(url, opts = nil)
|
|
|
|
opts ||= { method: "GET" }
|
2014-05-22 18:13:25 -04:00
|
|
|
env = Rack::MockRequest.env_for(url, opts)
|
2017-01-31 17:21:37 -05:00
|
|
|
TestProvider.new(env)
|
2014-05-22 18:13:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises errors for incorrect api_key" do
|
2017-07-27 21:20:09 -04:00
|
|
|
expect {
|
2014-05-22 18:13:25 -04:00
|
|
|
provider("/?api_key=INCORRECT").current_user
|
|
|
|
}.to raise_error(Discourse::InvalidAccess)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "finds a user for a correct per-user api key" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
ApiKey.create!(key: "hello", user_id: user.id, created_by_id: -1)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(provider("/?api_key=hello").current_user.id).to eq(user.id)
|
2017-02-17 11:02:33 -05:00
|
|
|
|
|
|
|
user.update_columns(active: false)
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
expect {
|
2017-02-17 11:02:33 -05:00
|
|
|
provider("/?api_key=hello").current_user
|
|
|
|
}.to raise_error(Discourse::InvalidAccess)
|
|
|
|
|
|
|
|
user.update_columns(active: true, suspended_till: 1.day.from_now)
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
expect {
|
2017-02-17 11:02:33 -05:00
|
|
|
provider("/?api_key=hello").current_user
|
|
|
|
}.to raise_error(Discourse::InvalidAccess)
|
2014-05-22 18:13:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises for a user pretending" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
ApiKey.create!(key: "hello", user_id: user.id, created_by_id: -1)
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
expect {
|
2014-05-22 18:13:25 -04:00
|
|
|
provider("/?api_key=hello&api_username=#{user2.username.downcase}").current_user
|
|
|
|
}.to raise_error(Discourse::InvalidAccess)
|
|
|
|
end
|
|
|
|
|
2014-11-19 23:21:49 -05:00
|
|
|
it "raises for a user with a mismatching ip" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
ApiKey.create!(key: "hello", user_id: user.id, created_by_id: -1, allowed_ips: ['10.0.0.0/24'])
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
expect {
|
2014-11-19 23:21:49 -05:00
|
|
|
provider("/?api_key=hello&api_username=#{user.username.downcase}", "REMOTE_ADDR" => "10.1.0.1").current_user
|
|
|
|
}.to raise_error(Discourse::InvalidAccess)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows a user with a matching ip" do
|
|
|
|
user = Fabricate(:user)
|
2014-11-24 01:16:11 -05:00
|
|
|
ApiKey.create!(key: "hello", user_id: user.id, created_by_id: -1, allowed_ips: ['100.0.0.0/24'])
|
2014-11-19 23:21:49 -05:00
|
|
|
|
|
|
|
found_user = provider("/?api_key=hello&api_username=#{user.username.downcase}",
|
2014-11-24 01:16:11 -05:00
|
|
|
"REMOTE_ADDR" => "100.0.0.22").current_user
|
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(found_user.id).to eq(user.id)
|
2014-11-19 23:21:49 -05:00
|
|
|
|
2014-11-24 01:16:11 -05:00
|
|
|
found_user = provider("/?api_key=hello&api_username=#{user.username.downcase}",
|
|
|
|
"HTTP_X_FORWARDED_FOR" => "10.1.1.1, 100.0.0.22").current_user
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(found_user.id).to eq(user.id)
|
2014-11-19 23:21:49 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-05-22 18:13:25 -04:00
|
|
|
it "finds a user for a correct system api key" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
ApiKey.create!(key: "hello", created_by_id: -1)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(provider("/?api_key=hello&api_username=#{user.username.downcase}").current_user.id).to eq(user.id)
|
2014-05-22 18:13:25 -04:00
|
|
|
end
|
|
|
|
|
2017-02-28 12:34:57 -05:00
|
|
|
it "should not update last seen for ajax calls without Discourse-Visible header" do
|
|
|
|
expect(provider("/topic/anything/goes",
|
|
|
|
:method => "POST",
|
|
|
|
"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"
|
|
|
|
).should_update_last_seen?).to eq(false)
|
2014-05-22 18:13:25 -04:00
|
|
|
end
|
|
|
|
|
2017-02-28 12:34:57 -05:00
|
|
|
it "should update ajax reqs with discourse visible" do
|
|
|
|
expect(provider("/topic/anything/goes",
|
|
|
|
:method => "POST",
|
|
|
|
"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
|
|
|
|
"HTTP_DISCOURSE_VISIBLE" => "true"
|
|
|
|
).should_update_last_seen?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should update last seen for non ajax" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(provider("/topic/anything/goes", method: "POST").should_update_last_seen?).to eq(true)
|
|
|
|
expect(provider("/topic/anything/goes", method: "GET").should_update_last_seen?).to eq(true)
|
2014-05-22 18:13:25 -04:00
|
|
|
end
|
2016-07-24 22:07:31 -04:00
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
it "correctly supports legacy tokens" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
token = SecureRandom.hex(16)
|
|
|
|
user_token = UserAuthToken.create!(user_id: user.id, auth_token: token,
|
|
|
|
prev_auth_token: token, legacy: true,
|
|
|
|
rotated_at: Time.zone.now
|
|
|
|
)
|
|
|
|
|
|
|
|
prov = provider("/", "HTTP_COOKIE" => "_t=#{user_token.auth_token}")
|
|
|
|
expect(prov.current_user.id).to eq(user.id)
|
|
|
|
|
|
|
|
# sets a new token up cause it got a global token
|
|
|
|
cookies = {}
|
|
|
|
prov.refresh_session(user, {}, cookies)
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
expect(user.user_auth_tokens.count).to eq(2)
|
|
|
|
expect(cookies["_t"][:value]).not_to eq(token)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly rotates tokens" do
|
2016-07-24 22:07:31 -04:00
|
|
|
SiteSetting.maximum_session_age = 3
|
|
|
|
user = Fabricate(:user)
|
2017-01-31 17:21:37 -05:00
|
|
|
@provider = provider('/')
|
|
|
|
cookies = {}
|
|
|
|
@provider.log_on_user(user, {}, cookies)
|
|
|
|
|
|
|
|
unhashed_token = cookies["_t"][:value]
|
|
|
|
|
|
|
|
token = UserAuthToken.find_by(user_id: user.id)
|
|
|
|
|
|
|
|
expect(token.auth_token_seen).to eq(false)
|
|
|
|
expect(token.auth_token).not_to eq(unhashed_token)
|
|
|
|
expect(token.auth_token).to eq(UserAuthToken.hash_token(unhashed_token))
|
|
|
|
|
|
|
|
# at this point we are going to try to rotate token
|
|
|
|
freeze_time 20.minutes.from_now
|
|
|
|
|
|
|
|
provider2 = provider("/", "HTTP_COOKIE" => "_t=#{unhashed_token}")
|
|
|
|
provider2.current_user
|
|
|
|
|
|
|
|
token.reload
|
|
|
|
expect(token.auth_token_seen).to eq(true)
|
2016-07-24 22:07:31 -04:00
|
|
|
|
|
|
|
cookies = {}
|
2017-01-31 17:21:37 -05:00
|
|
|
provider2.refresh_session(user, {}, cookies)
|
|
|
|
expect(cookies["_t"][:value]).not_to eq(unhashed_token)
|
|
|
|
|
|
|
|
token.reload
|
|
|
|
expect(token.auth_token_seen).to eq(false)
|
|
|
|
|
|
|
|
freeze_time 21.minutes.from_now
|
|
|
|
|
|
|
|
old_token = token.prev_auth_token
|
|
|
|
unverified_token = token.auth_token
|
|
|
|
|
|
|
|
# old token should still work
|
|
|
|
provider2 = provider("/", "HTTP_COOKIE" => "_t=#{unhashed_token}")
|
|
|
|
expect(provider2.current_user.id).to eq(user.id)
|
|
|
|
|
|
|
|
provider2.refresh_session(user, {}, cookies)
|
|
|
|
|
|
|
|
token.reload
|
|
|
|
|
|
|
|
# because this should cause a rotation since we can safely
|
|
|
|
# assume it never reached the client
|
|
|
|
expect(token.prev_auth_token).to eq(old_token)
|
|
|
|
expect(token.auth_token).not_to eq(unverified_token)
|
2016-07-24 22:07:31 -04:00
|
|
|
|
2016-07-25 21:37:41 -04:00
|
|
|
end
|
|
|
|
|
2016-07-27 22:58:49 -04:00
|
|
|
it "can only try 10 bad cookies a minute" do
|
2016-08-08 20:02:18 -04:00
|
|
|
user = Fabricate(:user)
|
2017-01-31 17:21:37 -05:00
|
|
|
token = UserAuthToken.generate!(user_id: user.id)
|
|
|
|
|
2016-08-08 20:02:18 -04:00
|
|
|
provider('/').log_on_user(user, {}, {})
|
|
|
|
|
2016-07-27 22:58:49 -04:00
|
|
|
RateLimiter.stubs(:disabled?).returns(false)
|
|
|
|
|
|
|
|
RateLimiter.new(nil, "cookie_auth_10.0.0.1", 10, 60).clear!
|
|
|
|
RateLimiter.new(nil, "cookie_auth_10.0.0.2", 10, 60).clear!
|
|
|
|
|
|
|
|
ip = "10.0.0.1"
|
|
|
|
env = { "HTTP_COOKIE" => "_t=#{SecureRandom.hex}", "REMOTE_ADDR" => ip }
|
|
|
|
|
|
|
|
10.times do
|
|
|
|
provider('/', env).current_user
|
|
|
|
end
|
2016-08-08 20:02:18 -04:00
|
|
|
|
2016-07-27 22:58:49 -04:00
|
|
|
expect {
|
|
|
|
provider('/', env).current_user
|
|
|
|
}.to raise_error(Discourse::InvalidAccess)
|
|
|
|
|
2016-08-08 20:02:18 -04:00
|
|
|
expect {
|
2017-01-31 17:21:37 -05:00
|
|
|
env["HTTP_COOKIE"] = "_t=#{token.unhashed_auth_token}"
|
2016-08-08 20:02:18 -04:00
|
|
|
provider("/", env).current_user
|
|
|
|
}.to raise_error(Discourse::InvalidAccess)
|
|
|
|
|
2016-07-27 22:58:49 -04:00
|
|
|
env["REMOTE_ADDR"] = "10.0.0.2"
|
2017-01-31 17:21:37 -05:00
|
|
|
|
|
|
|
expect {
|
|
|
|
provider('/', env).current_user
|
|
|
|
}.not_to raise_error
|
2016-07-27 22:58:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly removes invalid cookies" do
|
2017-07-27 21:20:09 -04:00
|
|
|
cookies = { "_t" => SecureRandom.hex }
|
2017-02-22 12:36:58 -05:00
|
|
|
provider('/').refresh_session(nil, {}, cookies)
|
2016-07-27 22:58:49 -04:00
|
|
|
expect(cookies.key?("_t")).to eq(false)
|
|
|
|
end
|
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
it "logging on user always creates a new token" do
|
2016-07-25 21:37:41 -04:00
|
|
|
user = Fabricate(:user)
|
2016-07-24 22:07:31 -04:00
|
|
|
|
2016-07-25 21:37:41 -04:00
|
|
|
provider('/').log_on_user(user, {}, {})
|
|
|
|
provider('/').log_on_user(user, {}, {})
|
2017-01-31 17:21:37 -05:00
|
|
|
|
|
|
|
expect(UserAuthToken.where(user_id: user.id).count).to eq(2)
|
2016-07-24 22:07:31 -04:00
|
|
|
end
|
|
|
|
|
2017-02-23 12:01:28 -05:00
|
|
|
it "sets secure, same site lax cookies" do
|
|
|
|
SiteSetting.force_https = false
|
|
|
|
SiteSetting.same_site_cookies = "Lax"
|
|
|
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
cookies = {}
|
|
|
|
provider('/').log_on_user(user, {}, cookies)
|
|
|
|
|
|
|
|
expect(cookies["_t"][:same_site]).to eq("Lax")
|
|
|
|
expect(cookies["_t"][:httponly]).to eq(true)
|
|
|
|
expect(cookies["_t"][:secure]).to eq(false)
|
|
|
|
|
|
|
|
SiteSetting.force_https = true
|
|
|
|
SiteSetting.same_site_cookies = "Disabled"
|
|
|
|
|
|
|
|
cookies = {}
|
|
|
|
provider('/').log_on_user(user, {}, cookies)
|
|
|
|
|
|
|
|
expect(cookies["_t"][:secure]).to eq(true)
|
|
|
|
expect(cookies["_t"].key?(:same_site)).to eq(false)
|
|
|
|
end
|
|
|
|
|
2016-07-24 22:07:31 -04:00
|
|
|
it "correctly expires session" do
|
|
|
|
SiteSetting.maximum_session_age = 2
|
|
|
|
user = Fabricate(:user)
|
2017-01-31 17:21:37 -05:00
|
|
|
token = UserAuthToken.generate!(user_id: user.id)
|
|
|
|
|
2016-07-24 22:07:31 -04:00
|
|
|
provider('/').log_on_user(user, {}, {})
|
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
expect(provider("/", "HTTP_COOKIE" => "_t=#{token.unhashed_auth_token}").current_user.id).to eq(user.id)
|
2016-07-24 22:07:31 -04:00
|
|
|
|
|
|
|
freeze_time 3.hours.from_now
|
2017-01-31 17:21:37 -05:00
|
|
|
expect(provider("/", "HTTP_COOKIE" => "_t=#{token.unhashed_auth_token}").current_user).to eq(nil)
|
2016-07-24 22:07:31 -04:00
|
|
|
end
|
2016-08-15 03:58:33 -04:00
|
|
|
|
|
|
|
context "user api" do
|
|
|
|
let :user do
|
|
|
|
Fabricate(:user)
|
|
|
|
end
|
|
|
|
|
|
|
|
let :api_key do
|
|
|
|
UserApiKey.create!(
|
|
|
|
application_name: 'my app',
|
|
|
|
client_id: '1234',
|
2016-10-14 01:05:27 -04:00
|
|
|
scopes: ['read'],
|
2016-08-15 03:58:33 -04:00
|
|
|
key: SecureRandom.hex,
|
|
|
|
user_id: user.id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows user API access correctly" do
|
|
|
|
params = {
|
|
|
|
"REQUEST_METHOD" => "GET",
|
2016-08-18 00:38:33 -04:00
|
|
|
"HTTP_USER_API_KEY" => api_key.key,
|
2016-08-15 03:58:33 -04:00
|
|
|
}
|
|
|
|
|
2016-12-15 20:05:20 -05:00
|
|
|
good_provider = provider("/", params)
|
|
|
|
|
|
|
|
expect(good_provider.current_user.id).to eq(user.id)
|
|
|
|
expect(good_provider.is_api?).to eq(false)
|
|
|
|
expect(good_provider.is_user_api?).to eq(true)
|
2016-08-15 03:58:33 -04:00
|
|
|
|
|
|
|
expect {
|
2017-07-27 21:20:09 -04:00
|
|
|
provider("/", params.merge("REQUEST_METHOD" => "POST")).current_user
|
2016-08-15 03:58:33 -04:00
|
|
|
}.to raise_error(Discourse::InvalidAccess)
|
|
|
|
|
2017-02-17 11:02:33 -05:00
|
|
|
user.update_columns(suspended_till: 1.year.from_now)
|
|
|
|
|
|
|
|
expect {
|
|
|
|
provider("/", params).current_user
|
|
|
|
}.to raise_error(Discourse::InvalidAccess)
|
|
|
|
|
2016-08-15 03:58:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "rate limits api usage" do
|
|
|
|
|
|
|
|
RateLimiter.stubs(:disabled?).returns(false)
|
|
|
|
limiter1 = RateLimiter.new(nil, "user_api_day_#{api_key.key}", 10, 60)
|
|
|
|
limiter2 = RateLimiter.new(nil, "user_api_min_#{api_key.key}", 10, 60)
|
|
|
|
limiter1.clear!
|
|
|
|
limiter2.clear!
|
|
|
|
|
|
|
|
SiteSetting.max_user_api_reqs_per_day = 3
|
|
|
|
SiteSetting.max_user_api_reqs_per_minute = 4
|
|
|
|
|
|
|
|
params = {
|
|
|
|
"REQUEST_METHOD" => "GET",
|
2016-08-18 00:38:33 -04:00
|
|
|
"HTTP_USER_API_KEY" => api_key.key,
|
2016-08-15 03:58:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
3.times do
|
|
|
|
provider("/", params).current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
expect {
|
|
|
|
provider("/", params).current_user
|
|
|
|
}.to raise_error(RateLimiter::LimitExceeded)
|
|
|
|
|
|
|
|
SiteSetting.max_user_api_reqs_per_day = 4
|
|
|
|
SiteSetting.max_user_api_reqs_per_minute = 3
|
|
|
|
|
|
|
|
limiter1.clear!
|
|
|
|
limiter2.clear!
|
|
|
|
|
|
|
|
3.times do
|
|
|
|
provider("/", params).current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
expect {
|
|
|
|
provider("/", params).current_user
|
|
|
|
}.to raise_error(RateLimiter::LimitExceeded)
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2014-05-22 18:13:25 -04:00
|
|
|
end
|