Skip to content

Commit

Permalink
feat: add P12 certificate generation and display
Browse files Browse the repository at this point in the history
- Add P12 certificate generation button to API user certificates view
- Create new partials for displaying private key and P12 certificate details
- Add expires_at field to P12 certificate display
- Add new routes for P12 certificate generation
- Add translations for new certificate features in English and Estonian
  • Loading branch information
OlegPhenomenon committed Feb 19, 2025
1 parent e1413b6 commit c628ee8
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 2 deletions.
20 changes: 20 additions & 0 deletions app/controllers/certificates/p12_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Certificates
class P12Controller < BaseController
def create
conn = ApiConnector::Certificates::P12::Creator.new(**auth_info)
result = conn.call_action(payload: p12_payload)
handle_response(result); return if performed?

flash.notice = @message
redirect_to api_user_path(params[:api_user_id])
end

private

def p12_payload
{
api_user_id: params[:api_user_id]
}
end
end
end
25 changes: 25 additions & 0 deletions app/services/api_connector/certificates/p12/creator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class ApiConnector
module Certificates
module P12
class Creator < ApiConnector
ACTION = 'create_p12'
ENDPOINT = {
method: 'post',
endpoint: '/certificates/p12',
}.freeze

def create_p12(payload: nil)
request(url: endpoint_url, method: method, params: p12_params(payload))
end

private

def p12_params(payload)
{
api_user_id: payload[:api_user_id]
}
end
end
end
end
end
3 changes: 2 additions & 1 deletion app/views/api_users/partials/_certificates.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
<h2><%= t('api_users.show.certificates') %></h2>
</div>
<div class="col-xs-auto">
<%= link_to "#{t('api_users.show.submit_csr')} <i class='fas fa-upload'></i>".html_safe, 'javascript: void(0);', 'data-dialog': 'upload_csr' %>
<%= link_to "#{t('api_users.show.submit_csr')} <i class='fas fa-upload'></i>".html_safe, 'javascript: void(0);', 'data-dialog': 'upload_csr' %><br />
<%= button_to "#{t('api_users.show.generate_p12')} <i class='fas fa-upload'></i>".html_safe, create_p12_certificates_path(api_user_id: @api_user[:id]) %>
</div>
</div>
</div>
Expand Down
22 changes: 22 additions & 0 deletions app/views/certificates/partials/_p12.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div class="page--block--header">
<div class="row between-xs">
<div class="col-xs-auto">
<h2><%= t('certificates.show.p12') %></h2>
</div>
<div class="col-xs-auto">
<%=
link_to "#{t(:download)} <i class='fas fa-download'></i>".html_safe,
download_api_user_certificate_path(@certificate[:api_user_id], @certificate[:id], 'p12'),
data: { turbo: false }
%>
</div>
</div>
</div>
<table class="table table__basic">
<tbody>
<tr>
<th><%= t('certificates.show.expires_at') %></th>
<td><%= l @certificate[:expires_at].to_datetime if @certificate[:expires_at].present? %></td>
</tr>
</tbody>
</table>
22 changes: 22 additions & 0 deletions app/views/certificates/partials/_private_key.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div class="page--block--header">
<div class="row between-xs">
<div class="col-xs-auto">
<h2><%= t('certificates.show.private_key') %></h2>
</div>
<div class="col-xs-auto">
<%=
link_to "#{t(:download)} <i class='fas fa-download'></i>".html_safe,
download_api_user_certificate_path(@certificate[:api_user_id], @certificate[:id], 'private_key'),
data: { turbo: false }
%>
</div>
</div>
</div>
<table class="table table__basic">
<tbody>
<tr>
<th><%= t('certificates.show.type') %></th>
<td><%= @certificate[:private_key].dig('type') %></td>
</tr>
</tbody>
</table>
20 changes: 20 additions & 0 deletions app/views/certificates/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,24 @@
</div>
</div>
<% end %>

<% unless @certificate[:private_key].blank? %>
<div class="page--block">
<div class="wrapper">
<div class="col-xs-12 col-md-8 col-md-offset-2">
<%= render 'certificates/partials/private_key' %>
</div>
</div>
</div>
<% end %>

<% unless @certificate[:p12].blank? %>
<div class="page--block">
<div class="wrapper">
<div class="col-xs-12 col-md-8 col-md-offset-2">
<%= render 'certificates/partials/p12' %>
</div>
</div>
</div>
<% end %>
</div>
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ en:
updated: "Updated"
by: "by %{user}"
submit_csr: "Submit CSR"
generate_p12: "Generate P12"
edit:
title: "Api User"

Expand Down Expand Up @@ -584,6 +585,10 @@ en:
expired: "Expired"
revoked: "Revoked"
valid: "Valid"
private_key: "Private Key"
p12: "P12 Certificate"
expires_at: "Expires At"
type: "Type"

white_ips:
index:
Expand Down
5 changes: 5 additions & 0 deletions config/locales/et.yml
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ et:
updated: "Muudetud"
by: "[%{user}]"
submit_csr: "Esita CSR"
generate_p12: "Genereeri P12"
edit:
title: "Api kasutaja"

Expand Down Expand Up @@ -586,6 +587,10 @@ et:
expired: "Aegunud"
revoked: "Tühistatud"
valid: "Kehtiv"
private_key: "Privaatvõti"
p12: "P12 Sertifikaat"
expires_at: "Aegub"
type: "Tüüp"

white_ips:
index:
Expand Down
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@
end
end
resources :white_ips, except: %i[new edit]
resources :certificates, only: %i[create]
resources :certificates, only: %i[create] do
scope module: :certificates do
post 'p12', to: 'p12#create', as: :create_p12, on: :collection
end
end
end
end
# rubocop:enable Metrics/BlockLength

0 comments on commit c628ee8

Please sign in to comment.