diff --git a/Gemfile.lock b/Gemfile.lock index b28099e..35aede1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -88,7 +88,7 @@ GEM msgpack (1.2.9) multi_json (1.13.1) nio4r (2.3.1) - nokogiri (1.10.3) + nokogiri (1.10.4) mini_portile2 (~> 2.4.0) parallel (1.17.0) parser (2.6.3.0) diff --git a/app/controllers/webpush_subscriptions_controller.rb b/app/controllers/webpush_subscriptions_controller.rb index 43fb5a7..5a14c0a 100644 --- a/app/controllers/webpush_subscriptions_controller.rb +++ b/app/controllers/webpush_subscriptions_controller.rb @@ -4,6 +4,13 @@ class WebpushSubscriptionsController < ApplicationController before_action :authenticate! + # POST /webpush_subscriptions + # + # Genera una suscripción Webpush + # + # @param Hash { + # webpush_subscription: { + # endpoint: @string, auth: @string, p256dh: @string } } def create @subscription = current_pirata.webpush_subscriptions .build(subscriptions_params) @@ -17,6 +24,15 @@ class WebpushSubscriptionsController < ApplicationController end end + # GET /webpush_subscriptions/vapid/public_key + # + # Devuelve la llave pública VAPID para suscribirse a Webpush + # + # @return Hash { vapid: { public_key: @string } } + def vapid_public_key + render 'webpush_subscriptions/vapid/public_key' + end + private def subscriptions_params diff --git a/app/models/webpush_subscription.rb b/app/models/webpush_subscription.rb index 33efcd2..5169e87 100644 --- a/app/models/webpush_subscription.rb +++ b/app/models/webpush_subscription.rb @@ -14,8 +14,8 @@ class WebpushSubscription < ApplicationRecord Webpush.payload_send( vapid: { subject: 'mailto:lumi@partidopirata.com.ar', - public_key: Rails.application.credentials.vapid.public_key, - private_key: Rails.application.credentials.vapid.private_key + public_key: Rails.application.credentials.vapid[:public_key], + private_key: Rails.application.credentials.vapid[:private_key] }, endpoint: endpoint, auth: auth, p256dh: p256dh, ttl: ttl.to_i, urgency: normalize_urgency(urgency), diff --git a/app/views/webpush_subscriptions/vapid/public_key.json.jbuilder b/app/views/webpush_subscriptions/vapid/public_key.json.jbuilder new file mode 100644 index 0000000..c62e46b --- /dev/null +++ b/app/views/webpush_subscriptions/vapid/public_key.json.jbuilder @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +json.vapid do + json.public_key Rails.application.credentials.vapid[:public_key] +end diff --git a/config/routes.rb b/config/routes.rb index fab4ab7..45d559e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,9 @@ Rails.application.routes.draw do get '/piratas/yo', to: 'piratas#yo' # No queremos un índice de piratas resources :piratas, only: %i[create] + # Obtener la llave VAPID para suscripciones + get '/webpush_subscriptions/vapid/public_key', + to: 'webpush_subscriptions#vapid_public_key' # Solo se pueden crear suscripciones resources :webpush_subscriptions, only: %i[create] # Podemos crear barcas y dentro de ellas consensos diff --git a/test/controllers/webpush_subscriptions_controller_test.rb b/test/controllers/webpush_subscriptions_controller_test.rb index 7581bb2..eb36a1b 100644 --- a/test/controllers/webpush_subscriptions_controller_test.rb +++ b/test/controllers/webpush_subscriptions_controller_test.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'test_helper' class PiratasControllerTest < ActionDispatch::IntegrationTest setup do @pirata = create :pirata @@ -36,4 +37,14 @@ class PiratasControllerTest < ActionDispatch::IntegrationTest assert_equal 204, @response.status end + + test 'se puede obtener la llave pública VAPID' do + get webpush_subscriptions_vapid_public_key_url, + as: :json, headers: @auth + + body = JSON.parse response.body + + assert body['vapid'].present? + assert body['vapid']['public_key'].present? + end end