FEATURE: add user_session_refreshed trigger (#9412)

Trigger an event for plugins to consume when a user session is refreshed.

This allows external auth to be notified about account activity, and be
able to take action such as use oauth refresh tokens to keep oauth
tokens valid.
This commit is contained in:
Jeff Wong 2020-04-14 06:32:24 -10:00 committed by GitHub
parent d9db0e6691
commit e3590d4ead
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View File

@ -148,6 +148,7 @@ class Auth::DefaultCurrentUserProvider
client_ip: @request.ip,
path: @env['REQUEST_PATH'])
cookies[TOKEN_COOKIE] = cookie_hash(@user_token.unhashed_auth_token)
DiscourseEvent.trigger(:user_session_refreshed, user)
end
end
end

View File

@ -595,6 +595,43 @@ describe Auth::DefaultCurrentUserProvider do
end
context "events" do
before do
@refreshes = 0
@increase_refreshes = -> (user) { @refreshes += 1 }
DiscourseEvent.on(:user_session_refreshed, &@increase_refreshes)
end
after do
DiscourseEvent.off(:user_session_refreshed, &@increase_refreshes)
end
it "fires event when updating last seen" do
user = Fabricate(:user)
@provider = provider('/')
cookies = {}
@provider.log_on_user(user, {}, cookies)
unhashed_token = cookies["_t"][:value]
freeze_time 20.minutes.from_now
provider2 = provider("/", "HTTP_COOKIE" => "_t=#{unhashed_token}")
provider2.refresh_session(user, {}, {})
expect(@refreshes).to eq(1)
end
it "does not fire an event when last seen does not update" do
user = Fabricate(:user)
@provider = provider('/')
cookies = {}
@provider.log_on_user(user, {}, cookies)
unhashed_token = cookies["_t"][:value]
freeze_time 2.minutes.from_now
provider2 = provider("/", "HTTP_COOKIE" => "_t=#{unhashed_token}")
provider2.refresh_session(user, {}, {})
expect(@refreshes).to eq(0)
end
end
context "rate limiting" do
before do