Supersede the previous authorization per app and device
CI / Test (pull_request) Successful in 41s

A new consent for the same app on the same device now replaces the previous
authorization, so a device keeps a single active token per app instead of
accumulating duplicates. Destroying the superseded record revokes its token
in Redis and removes its expiry job.

Matching is on (user, client_id, user_agent); a different device or app is
left untouched.

Also documents the storage-first launch (remoteStorage spec section 11) with
an explicit spec proving only the user address is handed over, never an
access token.

Part of #254
This commit is contained in:
raucao committed 2026-11-02 17:04:31 +04:00
1 parent e3bf932c51
commit 44e4488a4f
4 files changed
+79 -2

No files matched your search

@@ -29,6 +29,8 @@ class Services::RsAuthsController < Services::BaseController
current_user.address
end
# Storage-first bearer token issuance (remoteStorage spec §11): the app
# receives the address and runs the application-first flow itself.
launch_url = "#{@auth.launch_url}#remotestorage=#{user_address}"
redirect_to launch_url, allow_other_host: true
@@ -17,6 +17,7 @@ class RemoteStorageAuthorization < ApplicationRecord
before_create :generate_token
before_create :store_token_in_redis
before_create :find_or_create_web_app
before_create :supersede_previous_authorizations
after_create :schedule_token_expiry
after_create :notify_user
before_destroy :delete_token_from_redis
@@ -95,6 +96,16 @@ class RemoteStorageAuthorization < ApplicationRecord
end
end
# A new consent for the same app on the same device replaces the previous
# authorization, so a device only ever has one active token per app.
# Destroying the old record revokes its token in Redis and removes its
# expiry job.
def supersede_previous_authorizations
user.remote_storage_authorizations
.where(client_id: client_id, user_agent: user_agent)
.find_each(&:destroy!)
end
def looks_like_hosted_origin?
uri = URI.parse self.redirect_uri
!!(uri.host =~ /(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)/)
@@ -11,7 +11,7 @@ RSpec.describe Services::RsAuthsController, type: :controller do
])
end
describe "GET /services/storage/rs_auths/:id/launch_app" do
describe "GET /services/storage/rs_auths/:id/launch_app (storage-first)" do
context "when user is signed in" do
before do
sign_in user
@@ -32,10 +32,14 @@ RSpec.describe Services::RsAuthsController, type: :controller do
@auth.destroy
end
it "redirects to the given URL with the correct RS URL fragment params" do
it "hands the app the user address in the URL fragment" do
launch_url = "https://app.example.com#remotestorage=#{user.address}"
expect(response).to redirect_to(launch_url)
end
it "does not hand over an access token" do
expect(response.location).not_to include("access_token")
end
end
context "when remoteStorage is not enabled for the user" do
@@ -311,4 +311,64 @@ RSpec.describe RemoteStorageAuthorization, type: :model do
end
end
end
describe "superseding previous authorizations" do
let(:user_agent) do
"Mozilla/5.0 (X11; Linux x86_64; rv:120.0) Gecko/20100101 Firefox/120.0"
end
def create_auth(device: user_agent, client: "app.example.com")
user.remote_storage_authorizations.create!(
permissions: %w(documents:rw),
client_id: client,
redirect_uri: "https://#{client}",
user_agent: device
)
end
before(:each) { redis_rs_delete_keys("authorizations:*") }
after(:each) { redis_rs_delete_keys("authorizations:*") }
it "replaces a previous authorization for the same app and device" do
first = create_auth
second = create_auth
expect(RemoteStorageAuthorization.exists?(first.id)).to be(false)
expect(user.remote_storage_authorizations.reload).to contain_exactly(second)
end
it "revokes the superseded token from redis" do
first = create_auth
key = "authorizations:#{user.cn}:#{first.token}"
expect(redis_rs.keys(key)).not_to be_empty
create_auth
expect(redis_rs.keys(key)).to be_empty
end
it "keeps authorizations for a different device" do
first = create_auth(device: "Firefox/120.0")
second = create_auth(device: "Chrome/119.0")
expect(user.remote_storage_authorizations.reload).to contain_exactly(first, second)
end
it "keeps authorizations for a different app on the same device" do
first = create_auth(client: "one.example.com")
second = create_auth(client: "two.example.com")
expect(user.remote_storage_authorizations.reload).to contain_exactly(first, second)
end
it "treats a blank user agent as a single device" do
first = create_auth(device: nil)
second = create_auth(device: nil)
expect(RemoteStorageAuthorization.exists?(first.id)).to be(false)
expect(user.remote_storage_authorizations.reload).to contain_exactly(second)
end
end
end