Added user certificate REPP endpoint and mailer

This commit is contained in:
Sergei Tsoganov 2023-06-08 15:02:17 +03:00
parent a63dc448c3
commit b558c80e83
7 changed files with 84 additions and 0 deletions

View file

@ -0,0 +1,51 @@
module Repp
module V1
class CertificatesController < BaseController
THROTTLED_ACTIONS = %i[create].freeze
include Shunter::Integration::Throttle
api :POST, '/repp/v1/certificates'
desc 'Submit a new api user certificate signing request'
def create
authorize! :create, Certificate
@api_user = current_user.registrar.api_users.find(cert_params[:api_user_id])
csr = decode_cert_params(cert_params[:csr])
@certificate = @api_user.certificates.build(csr: csr)
unless @certificate.save
handle_non_epp_errors(@certificate)
return
end
notify_admins
render_success(data: { api_user: { id: @api_user.id } })
end
private
def cert_params
params.require(:certificate).permit(:api_user_id, csr: %i[body type])
end
def decode_cert_params(csr_params)
return if csr_params.blank?
Base64.decode64(csr_params[:body])
end
def notify_admins
admin_users_emails = User.all.select { |u| u.roles.include? 'admin' }.pluck(:email)
return if admin_users_emails.empty?
admin_users_emails.each do |email|
CertificateMailer.new_certificate_signing_request(email: email,
api_user: @api_user,
csr: @certificate)
.deliver_now
end
end
end
end
end

View file

@ -0,0 +1,8 @@
class CertificateMailer < ApplicationMailer
def new_certificate_signing_request(email:, api_user:, csr:)
@certificate = csr
@api_user = api_user
subject = 'New Certificate Signing Request Received'
mail(to: email, subject: subject)
end
end

View file

@ -30,6 +30,7 @@ class Ability
billing billing
can :manage, ApiUser can :manage, ApiUser
can :manage, WhiteIp can :manage, WhiteIp
can :create, Certificate
end end
def epp # Registrar/api_user dynamic role def epp # Registrar/api_user dynamic role

View file

@ -36,6 +36,8 @@ class Certificate < ApplicationRecord
validate :assign_metadata, on: :create validate :assign_metadata, on: :create
def assign_metadata def assign_metadata
return if errors.any?
origin = crt ? parsed_crt : parsed_csr origin = crt ? parsed_crt : parsed_csr
parse_metadata(origin) parse_metadata(origin)
rescue NoMethodError rescue NoMethodError

View file

@ -0,0 +1,11 @@
<p>New certificate signing request (CSR) has been received. Please review the details below:</p>
<h3>CSR Details:</h3>
<ul>
<li>Subject: <%= link_to(@certificate.parsed_csr.try(:subject),
admin_api_user_certificate_url(@api_user, @certificate)) %></li>
<li>Requested By: <%= @certificate.creator_str %></li>
<li>Requested Date: <%= l(@certificate.created_at) %></li>
</ul>
<p>Please take the necessary steps to process the certificate signing request.</p>

View file

@ -0,0 +1,10 @@
New certificate signing request (CSR) has been received. Please review the details below:
CSR Details:
Subject: <%= link_to(@certificate.parsed_csr.try(:subject),
admin_api_user_certificate_url(@api_user, @certificate)) %>
Requested By: <%= @certificate.creator_str %>
Requested Date: <%= l(@certificate.created_at) %>
Please take the necessary steps to process the certificate signing request.

View file

@ -110,6 +110,7 @@ Rails.application.routes.draw do
end end
resources :api_users, only: %i[index show update create destroy] resources :api_users, only: %i[index show update create destroy]
resources :white_ips, only: %i[index show update create destroy] resources :white_ips, only: %i[index show update create destroy]
resources :certificates, only: %i[create]
namespace :registrar do namespace :registrar do
resources :notifications, only: %i[index show update] do resources :notifications, only: %i[index show update] do
collection do collection do