Devolver la llave pública de VAPID

This commit is contained in:
fauno 2019-08-13 20:24:24 -03:00
parent fbf30112e9
commit 20d5c811a1
No known key found for this signature in database
GPG key ID: 456032D717A4CD9C
6 changed files with 38 additions and 3 deletions

View file

@ -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)

View file

@ -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

View file

@ -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),

View file

@ -0,0 +1,5 @@
# frozen_string_literal: true
json.vapid do
json.public_key Rails.application.credentials.vapid[:public_key]
end

View file

@ -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

View file

@ -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