From fcea11f0e577f0deedffc1c48de83203817db19c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Tue, 24 Oct 2023 13:08:06 +0200 Subject: [PATCH] Associate RS authorizations with web apps --- app/models/app_catalog/web_app.rb | 2 ++ app/models/remote_storage_authorization.rb | 20 +++++++++++++++++++ ...app_id_to_remote_storage_authorizations.rb | 7 +++++++ db/schema.rb | 5 ++++- spec/factories/app_catalog/web_apps.rb | 10 +--------- .../remote_storage_authorizations.rb | 3 ++- 6 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 db/migrate/20231024104909_add_web_app_id_to_remote_storage_authorizations.rb diff --git a/app/models/app_catalog/web_app.rb b/app/models/app_catalog/web_app.rb index 8cc1687..ced6b29 100644 --- a/app/models/app_catalog/web_app.rb +++ b/app/models/app_catalog/web_app.rb @@ -1,6 +1,8 @@ class AppCatalog::WebApp < ApplicationRecord store :metadata, coder: JSON + has_many :remote_storage_authorizations + has_one_attached :icon do |attachable| attachable.variant :medium, resize_to_limit: [128,128] attachable.variant :large, resize_to_limit: [256,256] diff --git a/app/models/remote_storage_authorization.rb b/app/models/remote_storage_authorization.rb index 82ac744..26c3fd4 100644 --- a/app/models/remote_storage_authorization.rb +++ b/app/models/remote_storage_authorization.rb @@ -1,5 +1,6 @@ class RemoteStorageAuthorization < ApplicationRecord belongs_to :user + belongs_to :web_app, class_name: "AppCatalog::WebApp", optional: true serialize :permissions @@ -15,7 +16,9 @@ class RemoteStorageAuthorization < ApplicationRecord before_create :generate_token before_create :store_token_in_redis + before_create :find_or_create_web_app after_create :schedule_token_expiry + # after_create :notify_user before_destroy :delete_token_from_redis after_destroy :remove_token_expiry_job @@ -60,4 +63,21 @@ class RemoteStorageAuthorization < ApplicationRecord job.delete if job.display_args == [id] 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 diff --git a/db/migrate/20231024104909_add_web_app_id_to_remote_storage_authorizations.rb b/db/migrate/20231024104909_add_web_app_id_to_remote_storage_authorizations.rb new file mode 100644 index 0000000..49457b5 --- /dev/null +++ b/db/migrate/20231024104909_add_web_app_id_to_remote_storage_authorizations.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index acee874..f830293 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # 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| t.string "name", 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 "created_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 ["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 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_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" end diff --git a/spec/factories/app_catalog/web_apps.rb b/spec/factories/app_catalog/web_apps.rb index b9ef31a..c07de2e 100644 --- a/spec/factories/app_catalog/web_apps.rb +++ b/spec/factories/app_catalog/web_apps.rb @@ -1,14 +1,6 @@ FactoryBot.define do - factory :app_catalog_web_app, class: 'AppCatalog::WebApp' do + factory :web_app, class: 'AppCatalog::WebApp' do url { "https://myfavoritedrinks.remotestorage.io/" } 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 diff --git a/spec/factories/remote_storage_authorizations.rb b/spec/factories/remote_storage_authorizations.rb index be7c810..76c93bf 100644 --- a/spec/factories/remote_storage_authorizations.rb +++ b/spec/factories/remote_storage_authorizations.rb @@ -4,6 +4,7 @@ FactoryBot.define do client_id { "some-fancy-app" } redirect_uri { "https://example.com/some-fancy-app" } app_name { "Fancy App" } - expire_at { nil } + expire_at { 1.month.from_now } + web_app end end