Distinguish tokens without a recorded device

Tokens issued before user agents were captured cannot be attributed to a
single device, and the per-app tokens issued earlier could even be reused
from several devices at once.

- Add RemoteStorageAuthorization#device_label, falling back to
  "Unknown device(s)" so the card and the details modal share one source
- Show a lower-bound device count with a "+" whenever an app has such a
  token, so it reads as "could be more" instead of claiming a number we
  cannot support
- Drop the now-redundant device-name wrapper from the details modal

Part of #254
This commit is contained in:
raucao committed 2026-11-03 16:55:05 +04:00
1 parent d94fda6d26
commit 614ead90a9
6 files changed
+71 -8

No files matched your search

+13 -3
View File
@@ -24,11 +24,21 @@ class RsAuthComponent < ViewComponent::Base
"#{count} #{'category'.pluralize(count)}"
end
def device_count
@authorizations.map { |auth| auth.device_name.presence || "Unknown device" }.uniq.size
def known_device_count
@authorizations.map(&:device_name).compact.uniq.size
end
def unrecorded_device?
@authorizations.any? { |auth| auth.device_name.blank? }
end
def device_summary
"#{device_count} #{'device'.pluralize(device_count)}"
count = known_device_count
return "#{count} #{'device'.pluralize(count)}" unless unrecorded_device?
# An authorization without a recorded user agent may cover several devices,
# so only a lower bound can be given.
lower_bound = count + 1
"#{lower_bound}+ #{'device'.pluralize(lower_bound)}"
end
end
@@ -24,7 +24,7 @@
<% @authorizations.each do |auth| %>
<li class="flex items-center justify-between gap-4 px-4 py-3">
<div class="min-w-0">
<p class="mb-1 truncate text-base font-medium"><%= device_name(auth) %></p>
<p class="mb-1 truncate text-base font-medium"><%= auth.device_label %></p>
<p class="truncate text-xs text-gray-500">
<%= tag.span "Connected #{auth.created_at.strftime('%-d %b %Y')}",
title: connected_at_title(auth) %>
@@ -16,10 +16,6 @@ class RsAuthDetailsModalComponent < ViewComponent::Base
(permissions & [":r", ":rw"]).any?
end
def device_name(auth)
auth.device_name.presence || "Unknown device"
end
def status_label(auth)
return "Never expires" if auth.expire_at.blank?
return "Expired on #{auth.expire_at.strftime('%-d %b %Y')}" if auth.expired?
@@ -2,6 +2,10 @@ class RemoteStorageAuthorization < ApplicationRecord
belongs_to :user
belongs_to :web_app, class_name: "AppCatalog::WebApp", optional: true
# Tokens issued before user agents were recorded may have been used from any
# number of devices, so they cannot be attributed to a single one.
UNKNOWN_DEVICE = "Unknown device(s)"
serialize :permissions, coder: YAML unless Rails.env.production?
validates_presence_of :permissions
@@ -60,6 +64,10 @@ class RemoteStorageAuthorization < ApplicationRecord
[name, os].compact.join(" on ").presence
end
def device_label
device_name || UNKNOWN_DEVICE
end
def display_name
web_app&.name.presence || app_name.presence || client_id
end
+34
View File
@@ -140,6 +140,40 @@ RSpec.describe "Storage connected apps", type: :feature do
end
end
context "with an authorization from before user agents were recorded" do
let!(:legacy) { create_auth(client: "notes.example.com", device: nil) }
after { legacy.destroy }
it "only claims a lower-bound device count and labels the device as unknown" do
visit apps_services_storage_path
within ".rs-auth-card" do
expect(page).to have_content("1+ device")
end
expect(page).to have_selector("p", text: "Unknown device(s)", visible: false)
end
end
context "with an unrecorded authorization alongside a recorded device" do
let!(:legacy) { create_auth(client: "notes.example.com", device: nil) }
let!(:laptop) { create_auth(client: "notes.example.com", device: firefox_ua) }
after do
legacy.destroy
laptop.destroy
end
it "counts the unrecorded authorization as a possible extra device" do
visit apps_services_storage_path
within ".rs-auth-card" do
expect(page).to have_content("2+ devices")
end
end
end
context "without connected apps" do
it "shows the empty state" do
visit apps_services_storage_path
@@ -372,4 +372,19 @@ RSpec.describe RemoteStorageAuthorization, type: :model do
expect(auth_with_user_agent("Firefox/120.0").device_name).to eq("Firefox")
end
end
describe "#device_label" do
def auth_with_user_agent(user_agent)
RemoteStorageAuthorization.new(user: user, user_agent: user_agent)
end
it "falls back to the unrecorded placeholder without a user agent" do
expect(auth_with_user_agent(nil).device_label).to eq("Unknown device(s)")
end
it "returns the device name when one can be determined" do
ua = "Mozilla/5.0 (X11; Linux x86_64; rv:120.0) Gecko/20100101 Firefox/120.0"
expect(auth_with_user_agent(ua).device_label).to eq("Firefox on Linux")
end
end
end