mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 04:56:18 +08:00
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:
parent
d9db0e6691
commit
e3590d4ead
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user