diff --git a/app/controllers/api_keys_controller.rb b/app/controllers/api_keys_controller.rb new file mode 100644 index 000000000..94ddfb448 --- /dev/null +++ b/app/controllers/api_keys_controller.rb @@ -0,0 +1,26 @@ +class ApiKeysController < ApplicationController + respond_to :html, :json, :xml + + def create + @api_key = authorize ApiKey.new(user: CurrentUser.user) + @api_key.save + respond_with(@api_key, location: user_api_keys_path(CurrentUser.user.id)) + end + + def index + params[:search][:user_id] = params[:user_id] if params[:user_id].present? + @api_keys = authorize ApiKey.visible(CurrentUser.user).paginated_search(params, count_pages: true) + respond_with(@api_keys) + end + + def show + @api_key = authorize ApiKey.find(params[:id]) + respond_with(@api_key) + end + + def destroy + @api_key = authorize ApiKey.find(params[:id]) + @api_key.destroy + respond_with(@api_key, location: user_api_keys_path(CurrentUser.user.id)) + end +end diff --git a/app/controllers/maintenance/user/api_keys_controller.rb b/app/controllers/maintenance/user/api_keys_controller.rb deleted file mode 100644 index bb90d1c42..000000000 --- a/app/controllers/maintenance/user/api_keys_controller.rb +++ /dev/null @@ -1,43 +0,0 @@ -module Maintenance - module User - class ApiKeysController < ApplicationController - before_action :check_privilege - before_action :authenticate!, :except => [:show] - rescue_from ::SessionLoader::AuthenticationFailure, :with => :authentication_failed - respond_to :html, :json, :xml - - def view - respond_with(CurrentUser.user, @api_key) - end - - def update - @api_key.regenerate! - respond_with(CurrentUser.user, @api_key) { |format| format.js } - end - - def destroy - @api_key.destroy - respond_with(CurrentUser.user, @api_key, location: CurrentUser.user) - end - - protected - - def check_privilege - raise ::User::PrivilegeError unless params[:user_id].to_i == CurrentUser.id - end - - def authenticate! - if CurrentUser.user.authenticate_password(params[:user][:password]) - @api_key = CurrentUser.user.api_key || ApiKey.generate!(CurrentUser.user) - @password = params[:user][:password] - else - raise ::SessionLoader::AuthenticationFailure - end - end - - def authentication_failed - redirect_to(user_api_key_path(CurrentUser.user), :notice => "Password was incorrect.") - end - end - end -end diff --git a/app/helpers/icon_helper.rb b/app/helpers/icon_helper.rb index 356c73fe9..aa6f2f63f 100644 --- a/app/helpers/icon_helper.rb +++ b/app/helpers/icon_helper.rb @@ -144,4 +144,8 @@ module IconHelper def link_icon(**options) icon_tag("fas fa-link", **options) end + + def plus_icon(**options) + icon_tag("fas fa-plus", **options) + end end diff --git a/app/javascript/src/styles/common/main_layout.scss b/app/javascript/src/styles/common/main_layout.scss index 4a9a0c367..d3eaf4edf 100644 --- a/app/javascript/src/styles/common/main_layout.scss +++ b/app/javascript/src/styles/common/main_layout.scss @@ -55,3 +55,18 @@ footer#page-footer { } } } + +/* A container for the main
An API key is used to give programs access to your <%= Danbooru.config.canonical_app_name %> account.
+ +If you're a developer, you can use an API key to access the + <%= link_to_wiki "#{Danbooru.config.canonical_app_name} API", "help:api" %>. If you're not a + developer, you probably don't need an API key.
+ +Your API key is like your password. Anyone who has it has full access to + your account. Don't give your API key to apps or people you don't trust, and don't post your + API key in public locations.
+ +Example usage:
+
+ <% if @api_keys.present? %>
+ <%= profile_url(format: "json", login: CurrentUser.user.name, api_key: @api_keys.first.key) %>
+ <% else %>
+ <%= profile_url(format: "json", login: CurrentUser.user.name, api_key: "your_api_key_goes_here") %>
+ <% end %>
+
+
See the <%= link_to_wiki "API documentation", "help:api" %> to learn more.
+You must re-enter your password to view or change your API key.
- - <%= edit_form_for CurrentUser.user, url: view_user_api_key_path(CurrentUser.user), method: :post do |f| %> - <%= f.input :password, :as => :password, :input_html => {:autocomplete => "off"} %> - <%= f.button :submit, "Submit" %> - <% end %> -| API Key | -Created | -Updated | -Actions | -
|---|---|---|---|
<%= @api_key.key %> |
- <%= compact_time @api_key.created_at %> | -<%= compact_time @api_key.updated_at %> | -- <%= button_to "Regenerate", user_api_key_path(CurrentUser.user), method: :put, params: { 'user[password]': @password }, remote: true %> - <%= button_to "Delete", user_api_key_path(CurrentUser.user), method: :delete, params: { 'user[password]': @password } %> - | -