Associate RS authorizations with web apps

This commit is contained in:
Râu Cao 2023-10-24 13:08:06 +02:00
parent 261a782963
commit fcea11f0e5
Signed by: raucao
GPG Key ID: 15E65F399D084BA9
6 changed files with 36 additions and 11 deletions

View File

@ -1,6 +1,8 @@
class AppCatalog::WebApp < ApplicationRecord class AppCatalog::WebApp < ApplicationRecord
store :metadata, coder: JSON store :metadata, coder: JSON
has_many :remote_storage_authorizations
has_one_attached :icon do |attachable| has_one_attached :icon do |attachable|
attachable.variant :medium, resize_to_limit: [128,128] attachable.variant :medium, resize_to_limit: [128,128]
attachable.variant :large, resize_to_limit: [256,256] attachable.variant :large, resize_to_limit: [256,256]

View File

@ -1,5 +1,6 @@
class RemoteStorageAuthorization < ApplicationRecord class RemoteStorageAuthorization < ApplicationRecord
belongs_to :user belongs_to :user
belongs_to :web_app, class_name: "AppCatalog::WebApp", optional: true
serialize :permissions serialize :permissions
@ -15,7 +16,9 @@ class RemoteStorageAuthorization < ApplicationRecord
before_create :generate_token before_create :generate_token
before_create :store_token_in_redis before_create :store_token_in_redis
before_create :find_or_create_web_app
after_create :schedule_token_expiry after_create :schedule_token_expiry
# after_create :notify_user
before_destroy :delete_token_from_redis before_destroy :delete_token_from_redis
after_destroy :remove_token_expiry_job after_destroy :remove_token_expiry_job
@ -60,4 +63,21 @@ class RemoteStorageAuthorization < ApplicationRecord
job.delete if job.display_args == [id] job.delete if job.display_args == [id]
end end
end end
def find_or_create_web_app
if looks_like_hosted_origin?
web_app = AppCatalog::WebApp.find_or_create_by!(url: self.url)
self.web_app = web_app
self.app_name = web_app.name.presence || client_id
else
self.app_name = client_id
end
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}$)/)
rescue URI::InvalidURIError
false
end
end end

View File

@ -0,0 +1,7 @@
class AddWebAppIdToRemoteStorageAuthorizations < ActiveRecord::Migration[7.0]
def change
add_reference :remote_storage_authorizations, :web_app, foreign_key: {
to_table: :app_catalog_web_apps
}
end
end

View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_10_19_125135) do ActiveRecord::Schema[7.0].define(version: 2023_10_24_104909) do
create_table "active_storage_attachments", force: :cascade do |t| create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
t.string "record_type", null: false t.string "record_type", null: false
@ -96,8 +96,10 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_19_125135) do
t.datetime "expire_at" t.datetime "expire_at"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.integer "web_app_id"
t.index ["permissions"], name: "index_remote_storage_authorizations_on_permissions" t.index ["permissions"], name: "index_remote_storage_authorizations_on_permissions"
t.index ["user_id"], name: "index_remote_storage_authorizations_on_user_id" t.index ["user_id"], name: "index_remote_storage_authorizations_on_user_id"
t.index ["web_app_id"], name: "index_remote_storage_authorizations_on_web_app_id"
end end
create_table "settings", force: :cascade do |t| create_table "settings", force: :cascade do |t|
@ -132,5 +134,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_19_125135) do
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "remote_storage_authorizations", "app_catalog_web_apps", column: "web_app_id"
add_foreign_key "remote_storage_authorizations", "users" add_foreign_key "remote_storage_authorizations", "users"
end end

View File

@ -1,14 +1,6 @@
FactoryBot.define do FactoryBot.define do
factory :app_catalog_web_app, class: 'AppCatalog::WebApp' do factory :web_app, class: 'AppCatalog::WebApp' do
url { "https://myfavoritedrinks.remotestorage.io/" } url { "https://myfavoritedrinks.remotestorage.io/" }
name { "My Favorite Drinks" } name { "My Favorite Drinks" }
short_name { "Drinks" }
description { nil }
theme_color { nil }
background_color { nil }
display { nil }
start_url { nil }
scope { nil }
share_target { nil }
end end
end end

View File

@ -4,6 +4,7 @@ FactoryBot.define do
client_id { "some-fancy-app" } client_id { "some-fancy-app" }
redirect_uri { "https://example.com/some-fancy-app" } redirect_uri { "https://example.com/some-fancy-app" }
app_name { "Fancy App" } app_name { "Fancy App" }
expire_at { nil } expire_at { 1.month.from_now }
web_app
end end
end end