mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-09-12 08:04:34 +08:00
API key management tweaks [SCI-10763]
This commit is contained in:
parent
1b730a8977
commit
23d311473e
6 changed files with 29 additions and 5 deletions
|
@ -210,13 +210,19 @@ class Users::RegistrationsController < Devise::RegistrationsController
|
||||||
def regenerate_api_key
|
def regenerate_api_key
|
||||||
current_user.regenerate_api_key!
|
current_user.regenerate_api_key!
|
||||||
|
|
||||||
redirect_to edit_user_registration_path
|
redirect_to(edit_user_registration_path(anchor: 'api-key'),
|
||||||
|
flash: {
|
||||||
|
success: t('users.registrations.edit.api_key.generated')
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
def revoke_api_key
|
def revoke_api_key
|
||||||
current_user.revoke_api_key!
|
current_user.revoke_api_key!
|
||||||
|
|
||||||
redirect_to edit_user_registration_path
|
redirect_to(edit_user_registration_path(anchor: 'api-key'),
|
||||||
|
flash: {
|
||||||
|
success: t('users.registrations.edit.api_key.revoked')
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
|
@ -516,6 +516,7 @@ class User < ApplicationRecord
|
||||||
def regenerate_api_key!
|
def regenerate_api_key!
|
||||||
update!(
|
update!(
|
||||||
api_key: SecureRandom.urlsafe_base64(33),
|
api_key: SecureRandom.urlsafe_base64(33),
|
||||||
|
api_key_created_at: Time.current,
|
||||||
api_key_expires_at: Constants::API_KEY_EXPIRES_IN.from_now
|
api_key_expires_at: Constants::API_KEY_EXPIRES_IN.from_now
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,11 +1,19 @@
|
||||||
<div class="mb-8">
|
<div class="mb-8">
|
||||||
<h3><%= t("users.registrations.edit.api_key.title") %></h3>
|
<h3 id="api-key"><%= t("users.registrations.edit.api_key.title") %></h3>
|
||||||
<p>
|
<p>
|
||||||
<%= t("users.registrations.edit.api_key.description") %>
|
<%= t("users.registrations.edit.api_key.description") %>
|
||||||
</p>
|
</p>
|
||||||
<% if current_user.api_key %>
|
<% if current_user.api_key %>
|
||||||
<div class="api-key-display">
|
<div class="api-key-display">
|
||||||
<%= text_field_tag :api_key, current_user.api_key, class: "p-3 mb-2 w-full", disabled: "disabled" %>
|
<div class="form-group sci-input-container right-icon !w-1/2">
|
||||||
|
<%= password_field_tag :api_key,
|
||||||
|
current_user.api_key,
|
||||||
|
name: 'api_key',
|
||||||
|
class: 'form-control sci-input-field !text-sn-black !font-mono !cursor-text',
|
||||||
|
disabled: 'disabled'
|
||||||
|
%>
|
||||||
|
<i class="sn-icon sn-icon-visibility-show show-password" style="cursor: pointer; z-index: 10"></i>
|
||||||
|
</div>
|
||||||
<% if current_user.api_key_expires_at < Time.current %>
|
<% if current_user.api_key_expires_at < Time.current %>
|
||||||
<p class="">
|
<p class="">
|
||||||
<%= t("users.registrations.edit.api_key.expired") %>
|
<%= t("users.registrations.edit.api_key.expired") %>
|
||||||
|
|
|
@ -13,5 +13,5 @@ Rails.application.configure do
|
||||||
|
|
||||||
config.x.core_api_v2_enabled = ENV['CORE_API_V2_ENABLED'] || false
|
config.x.core_api_v2_enabled = ENV['CORE_API_V2_ENABLED'] || false
|
||||||
|
|
||||||
config.x.core_api_key_enabled = ENV['CORE_API_KEY_ENABLED'] || false
|
config.x.core_api_key_enabled = ENV['CORE_API_KEY_ENABLED'] == 'true'
|
||||||
end
|
end
|
||||||
|
|
|
@ -2832,6 +2832,8 @@ en:
|
||||||
regenerate: "Regenerate"
|
regenerate: "Regenerate"
|
||||||
revoke: "Revoke"
|
revoke: "Revoke"
|
||||||
expired: "This key has expired!"
|
expired: "This key has expired!"
|
||||||
|
generated: "API key generated!"
|
||||||
|
revoked: "API key revoked!"
|
||||||
new:
|
new:
|
||||||
head_title: "Sign up"
|
head_title: "Sign up"
|
||||||
team_name_label: "Team name"
|
team_name_label: "Team name"
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class AddApiKeyCreatedAtToUsers < ActiveRecord::Migration[7.0]
|
||||||
|
def change
|
||||||
|
add_column :users, :api_key_created_at, :timestamp
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Reference in a new issue