From 12c47ea7549ef36cea3a97579c4765fdd784d84a Mon Sep 17 00:00:00 2001 From: Emelia Smith Date: Tue, 16 Jul 2024 19:30:17 +0200 Subject: [PATCH] Add Doorkeeper::Application.webapp helper --- app/lib/application_extension.rb | 8 ++++++++ app/models/session_activation.rb | 6 +++--- app/models/web/push_subscription.rb | 4 ++-- db/seeds/01_web_app.rb | 2 +- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/app/lib/application_extension.rb b/app/lib/application_extension.rb index d7aaeba5bd..e1350026b7 100644 --- a/app/lib/application_extension.rb +++ b/app/lib/application_extension.rb @@ -19,6 +19,14 @@ module ApplicationExtension before_destroy :close_streaming_sessions, prepend: true end + class_methods do + def webapp + Doorkeeper::Application.find_by!(name: 'Web', superapp: true) + rescue ActiveRecord::RecordNotFound + Doorkeeper::Application.create!(name: 'Web', redirect_uri: Doorkeeper.configuration.native_redirect_uri, scopes: 'read write follow push', superapp: true) + end + end + def confirmation_redirect_uri redirect_uri.lines.first.strip end diff --git a/app/models/session_activation.rb b/app/models/session_activation.rb index d99ecf8adb..82c2eae589 100644 --- a/app/models/session_activation.rb +++ b/app/models/session_activation.rb @@ -28,7 +28,7 @@ class SessionActivation < ApplicationRecord before_create :assign_access_token - DEFAULT_SCOPES = %w(read write follow).freeze + DEFAULT_SCOPES = Doorkeeper::Application.webapp.scopes.without('push').freeze scope :latest, -> { order(id: :desc) } @@ -66,9 +66,9 @@ class SessionActivation < ApplicationRecord def access_token_attributes { - application_id: Doorkeeper::Application.find_by(superapp: true)&.id, + application_id: Doorkeeper::Application.webapp.id, resource_owner_id: user_id, - scopes: DEFAULT_SCOPES.join(' '), + scopes: DEFAULT_SCOPES, expires_in: Doorkeeper.configuration.access_token_expires_in, use_refresh_token: Doorkeeper.configuration.refresh_token_enabled?, } diff --git a/app/models/web/push_subscription.rb b/app/models/web/push_subscription.rb index 656040d2ce..3463f36ee9 100644 --- a/app/models/web/push_subscription.rb +++ b/app/models/web/push_subscription.rb @@ -66,9 +66,9 @@ class Web::PushSubscription < ApplicationRecord def find_or_create_access_token Doorkeeper::AccessToken.find_or_create_for( - application: Doorkeeper::Application.find_by(superapp: true), + application: Doorkeeper::Application.webapp, resource_owner: user_id || session_activation.user_id, - scopes: Doorkeeper::OAuth::Scopes.from_string('read write follow push'), + scopes: Doorkeeper::Application.webapp.scopes, expires_in: Doorkeeper.configuration.access_token_expires_in, use_refresh_token: Doorkeeper.configuration.refresh_token_enabled? ) diff --git a/db/seeds/01_web_app.rb b/db/seeds/01_web_app.rb index a9d8fd3bae..94c2b773ed 100644 --- a/db/seeds/01_web_app.rb +++ b/db/seeds/01_web_app.rb @@ -1,3 +1,3 @@ # frozen_string_literal: true -Doorkeeper::Application.create_with(name: 'Web', redirect_uri: Doorkeeper.configuration.native_redirect_uri, scopes: 'read write follow push').find_or_create_by(superapp: true) +Doorkeeper::Application.webapp