diff --git a/app/assets/stylesheets/specific/api_keys.css.scss b/app/assets/stylesheets/specific/api_keys.css.scss new file mode 100644 index 000000000..0c43e98e2 --- /dev/null +++ b/app/assets/stylesheets/specific/api_keys.css.scss @@ -0,0 +1,11 @@ +@import "../common/020_base.css.scss"; + +#c-maintenance-user-api-keys { + #api-key { + @extend code; + } + + .button_to { + display: inline-block; + } +} diff --git a/app/controllers/api_keys_controller.rb b/app/controllers/api_keys_controller.rb deleted file mode 100644 index 706ccdc43..000000000 --- a/app/controllers/api_keys_controller.rb +++ /dev/null @@ -1,17 +0,0 @@ -class ApiKeysController < ApplicationController - before_filter :member_only - - def new - @api_key = ApiKey.new(:user_id => CurrentUser.user.id) - end - - def create - @api_key = ApiKey.generate!(CurrentUser.user) - - if @api_key.errors.empty? - redirect_to user_path(CurrentUser.user), :notice => "API key generated" - else - render :action => "new" - end - end -end diff --git a/app/controllers/maintenance/user/api_keys_controller.rb b/app/controllers/maintenance/user/api_keys_controller.rb new file mode 100644 index 000000000..8f4844be4 --- /dev/null +++ b/app/controllers/maintenance/user/api_keys_controller.rb @@ -0,0 +1,44 @@ +module Maintenance + module User + class ApiKeysController < ApplicationController + before_filter :member_only + before_filter :check_privilege + before_filter :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 ::User.authenticate(CurrentUser.user.name, params[:user][:password]) == CurrentUser.user + @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/models/api_key.rb b/app/models/api_key.rb index 58ac9ba8a..2037a89fa 100644 --- a/app/models/api_key.rb +++ b/app/models/api_key.rb @@ -7,4 +7,8 @@ class ApiKey < ActiveRecord::Base def self.generate!(user) create(:user_id => user.id, :key => SecureRandom.urlsafe_base64(32)) end + + def regenerate! + update!(:key => SecureRandom.urlsafe_base64(32)) + end end diff --git a/app/views/api_keys/new.html.erb b/app/views/api_keys/new.html.erb deleted file mode 100644 index 5fd33dd4f..000000000 --- a/app/views/api_keys/new.html.erb +++ /dev/null @@ -1,19 +0,0 @@ -
You can generate a new API key to authenticate against <%= Danbooru.config.app_name %>.
- - <%= error_messages_for :api_key %> - - <%= simple_form_for(@api_key) do |f| %> - <%= submit_tag "Generate" %> - <% end %> -You must re-enter your password to view or change your API key.
+ + <%= simple_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 } %> + | +