2019-05-02 01:57:12 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-03-25 20:04:28 -05:00
|
|
|
class Admin::ApiController < Admin::AdminController
|
2019-11-05 08:10:23 -06:00
|
|
|
# Note: in the REST API, ApiKeys are referred to simply as "key"
|
|
|
|
# If we used "api_key", then our user provider would try to use the value for authentication
|
2013-10-22 14:53:08 -05:00
|
|
|
|
2013-03-25 20:04:28 -05:00
|
|
|
def index
|
2021-11-09 04:18:23 -06:00
|
|
|
offset = (params[:offset] || 0).to_i
|
|
|
|
limit = (params[:limit] || 50).to_i.clamp(1, 50)
|
|
|
|
|
2021-10-21 10:00:17 -05:00
|
|
|
keys =
|
|
|
|
ApiKey
|
|
|
|
.where(hidden: false)
|
|
|
|
.includes(:user, :api_key_scopes)
|
2021-11-09 04:18:23 -06:00
|
|
|
# Sort revoked keys by revoked_at and active keys by created_at
|
|
|
|
.order("revoked_at DESC NULLS FIRST, created_at DESC")
|
|
|
|
.offset(offset)
|
|
|
|
.limit(limit)
|
2023-01-09 06:20:10 -06:00
|
|
|
|
2021-11-09 04:18:23 -06:00
|
|
|
render_json_dump(keys: serialize_data(keys, ApiKeySerializer), offset: offset, limit: limit)
|
2019-11-05 08:10:23 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2020-07-16 13:51:24 -05:00
|
|
|
api_key = ApiKey.includes(:api_key_scopes).find_by!(id: params[:id])
|
2019-11-05 08:10:23 -06:00
|
|
|
render_serialized(api_key, ApiKeySerializer, root: "key")
|
|
|
|
end
|
|
|
|
|
2020-07-16 13:51:24 -05:00
|
|
|
def scopes
|
|
|
|
scopes =
|
|
|
|
ApiKeyScope
|
|
|
|
.scope_mappings
|
|
|
|
.reduce({}) do |memo, (resource, actions)|
|
|
|
|
memo.tap do |m|
|
2020-08-18 13:12:04 -05:00
|
|
|
m[resource] = actions.map do |k, v|
|
|
|
|
{
|
2021-02-17 11:42:44 -06:00
|
|
|
scope_id: "#{resource}:#{k}",
|
2020-08-18 13:12:04 -05:00
|
|
|
key: k,
|
|
|
|
name: k.to_s.gsub("_", " "),
|
|
|
|
params: v[:params],
|
|
|
|
urls: v[:urls],
|
|
|
|
}
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
|
|
|
end
|
2020-08-18 13:12:04 -05:00
|
|
|
end
|
2020-07-16 13:51:24 -05:00
|
|
|
|
|
|
|
render json: { scopes: scopes }
|
|
|
|
end
|
|
|
|
|
2019-11-05 08:10:23 -06:00
|
|
|
def update
|
|
|
|
api_key = ApiKey.find_by!(id: params[:id])
|
|
|
|
ApiKey.transaction do
|
|
|
|
api_key.update!(update_params)
|
|
|
|
log_api_key(api_key, UserHistory.actions[:api_key_update], changes: api_key.saved_changes)
|
|
|
|
end
|
|
|
|
render_serialized(api_key, ApiKeySerializer, root: "key")
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
api_key = ApiKey.find_by!(id: params[:id])
|
|
|
|
ApiKey.transaction do
|
|
|
|
api_key.destroy
|
|
|
|
log_api_key(api_key, UserHistory.actions[:api_key_destroy])
|
|
|
|
end
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
api_key = ApiKey.new(update_params)
|
|
|
|
ApiKey.transaction do
|
|
|
|
api_key.created_by = current_user
|
2020-07-16 13:51:24 -05:00
|
|
|
api_key.api_key_scopes = build_scopes
|
2019-11-05 08:10:23 -06:00
|
|
|
if username = params.require(:key).permit(:username)[:username].presence
|
|
|
|
api_key.user = User.find_by_username(username)
|
|
|
|
raise Discourse::NotFound unless api_key.user
|
|
|
|
end
|
|
|
|
api_key.save!
|
|
|
|
log_api_key(api_key, UserHistory.actions[:api_key_create], changes: api_key.saved_changes)
|
|
|
|
end
|
|
|
|
render_serialized(api_key, ApiKeySerializer, root: "key")
|
2013-10-22 14:53:08 -05:00
|
|
|
end
|
|
|
|
|
2019-11-05 08:10:23 -06:00
|
|
|
def undo_revoke_key
|
2014-05-06 08:41:59 -05:00
|
|
|
api_key = ApiKey.find_by(id: params[:id])
|
2015-05-06 20:00:51 -05:00
|
|
|
raise Discourse::NotFound if api_key.blank?
|
2013-10-22 14:53:08 -05:00
|
|
|
|
2019-11-05 08:10:23 -06:00
|
|
|
ApiKey.transaction do
|
|
|
|
api_key.update(revoked_at: nil)
|
|
|
|
log_api_key_restore(api_key)
|
|
|
|
end
|
2013-10-22 14:53:08 -05:00
|
|
|
render_serialized(api_key, ApiKeySerializer)
|
2013-03-25 20:04:28 -05:00
|
|
|
end
|
|
|
|
|
2013-10-22 14:53:08 -05:00
|
|
|
def revoke_key
|
2014-05-06 08:41:59 -05:00
|
|
|
api_key = ApiKey.find_by(id: params[:id])
|
2015-05-06 20:00:51 -05:00
|
|
|
raise Discourse::NotFound if api_key.blank?
|
2013-10-22 14:53:08 -05:00
|
|
|
|
2019-11-05 08:10:23 -06:00
|
|
|
ApiKey.transaction do
|
|
|
|
api_key.update(revoked_at: Time.zone.now)
|
|
|
|
log_api_key_revoke(api_key)
|
|
|
|
end
|
|
|
|
render_serialized(api_key, ApiKeySerializer)
|
2013-10-22 14:53:08 -05:00
|
|
|
end
|
|
|
|
|
2019-11-05 08:10:23 -06:00
|
|
|
private
|
|
|
|
|
2020-07-16 13:51:24 -05:00
|
|
|
def build_scopes
|
|
|
|
params.require(:key)[:scopes].to_a.map do |scope_params|
|
2021-02-17 11:42:44 -06:00
|
|
|
resource, action = scope_params[:scope_id].split(":")
|
2020-07-16 13:51:24 -05:00
|
|
|
|
|
|
|
mapping = ApiKeyScope.scope_mappings.dig(resource.to_sym, action.to_sym)
|
|
|
|
raise Discourse::InvalidParameters if mapping.nil? # invalid mapping
|
|
|
|
|
|
|
|
ApiKeyScope.new(
|
|
|
|
resource: resource,
|
|
|
|
action: action,
|
|
|
|
allowed_parameters: build_params(scope_params, mapping[:params]),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_params(scope_params, params)
|
|
|
|
return if params.nil?
|
|
|
|
|
|
|
|
scope_params
|
|
|
|
.slice(*params)
|
|
|
|
.tap do |allowed_params|
|
|
|
|
allowed_params.each do |k, v|
|
|
|
|
v.blank? ? allowed_params.delete(k) : allowed_params[k] = v.split(",")
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2020-07-16 13:51:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-05 08:10:23 -06:00
|
|
|
def update_params
|
|
|
|
editable_fields = [:description]
|
|
|
|
permitted_params = params.permit(key: [*editable_fields])[:key]
|
|
|
|
raise Discourse::InvalidParameters unless permitted_params
|
|
|
|
permitted_params
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_api_key(*args)
|
|
|
|
StaffActionLogger.new(current_user).log_api_key(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_api_key_revoke(*args)
|
|
|
|
StaffActionLogger.new(current_user).log_api_key_revoke(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_api_key_restore(*args)
|
|
|
|
StaffActionLogger.new(current_user).log_api_key_restore(*args)
|
2013-03-25 20:04:28 -05:00
|
|
|
end
|
|
|
|
end
|