Skip to content

Commit

Permalink
Added user certificate REPP endpoint and mailer
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergei Tsoganov authored and Sergei Tsoganov committed Jun 8, 2023
1 parent 61c7b59 commit 0e9808d
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 0 deletions.
51 changes: 51 additions & 0 deletions app/controllers/repp/v1/certificates_controller.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions app/mailers/certificate_mailer.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def super # Registrar/api_user dynamic role
billing
can :manage, ApiUser
can :manage, WhiteIp
can :create, Certificate
end

def epp # Registrar/api_user dynamic role
Expand Down
2 changes: 2 additions & 0 deletions app/models/certificate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def validate_csr_and_crt
validate :assign_metadata, on: :create

def assign_metadata
return if errors.any?

origin = crt ? parsed_crt : parsed_csr
parse_metadata(origin)
rescue NoMethodError
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
end
resources :api_users, 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
resources :notifications, only: %i[index show update] do
collection do
Expand Down

0 comments on commit 0e9808d

Please sign in to comment.