2016-08-15 03:58:33 -04:00
|
|
|
class UserApiKeysController < ApplicationController
|
|
|
|
|
2016-08-16 01:10:32 -04:00
|
|
|
layout 'no_ember'
|
|
|
|
|
2018-01-31 23:17:59 -05:00
|
|
|
requires_login only: [:create, :revoke, :undo_revoke]
|
2017-08-31 00:06:56 -04:00
|
|
|
skip_before_action :redirect_to_login_if_required, only: [:new]
|
|
|
|
skip_before_action :check_xhr, :preload_json
|
2016-08-15 03:58:33 -04:00
|
|
|
|
2018-10-25 05:46:34 -04:00
|
|
|
AUTH_API_VERSION ||= 3
|
2016-08-16 19:58:19 -04:00
|
|
|
|
2016-08-15 03:58:33 -04:00
|
|
|
def new
|
2016-08-16 19:58:19 -04:00
|
|
|
|
|
|
|
if request.head?
|
|
|
|
head :ok, auth_api_version: AUTH_API_VERSION
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-08-16 01:10:32 -04:00
|
|
|
require_params
|
2016-08-23 02:48:00 -04:00
|
|
|
validate_params
|
2016-08-16 01:10:32 -04:00
|
|
|
|
|
|
|
unless current_user
|
|
|
|
cookies[:destination_url] = request.fullpath
|
2016-09-15 23:48:50 -04:00
|
|
|
|
|
|
|
if SiteSetting.enable_sso?
|
|
|
|
redirect_to path('/session/sso')
|
|
|
|
else
|
|
|
|
redirect_to path('/login')
|
|
|
|
end
|
2016-08-16 01:10:32 -04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-09-12 01:42:06 -04:00
|
|
|
unless meets_tl?
|
2016-08-23 02:48:00 -04:00
|
|
|
@no_trust_level = true
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-08-16 01:10:32 -04:00
|
|
|
@application_name = params[:application_name]
|
|
|
|
@public_key = params[:public_key]
|
|
|
|
@nonce = params[:nonce]
|
|
|
|
@client_id = params[:client_id]
|
|
|
|
@auth_redirect = params[:auth_redirect]
|
|
|
|
@push_url = params[:push_url]
|
2017-07-27 21:20:09 -04:00
|
|
|
@localized_scopes = params[:scopes].split(",").map { |s| I18n.t("user_api_key.scopes.#{s}") }
|
2016-10-14 01:05:27 -04:00
|
|
|
@scopes = params[:scopes]
|
2016-08-17 02:44:20 -04:00
|
|
|
|
2016-08-23 02:48:00 -04:00
|
|
|
rescue Discourse::InvalidAccess
|
|
|
|
@generic_error = true
|
2016-08-15 03:58:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
|
2016-08-16 01:10:32 -04:00
|
|
|
require_params
|
|
|
|
|
2019-01-03 22:46:18 -05:00
|
|
|
if params.key?(:auth_redirect) && SiteSetting.allowed_user_api_auth_redirects
|
2017-07-27 21:20:09 -04:00
|
|
|
.split('|')
|
2019-01-03 22:46:18 -05:00
|
|
|
.none? { |u| params[:auth_redirect] == u }
|
2016-08-15 03:58:33 -04:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
raise Discourse::InvalidAccess
|
2016-08-15 03:58:33 -04:00
|
|
|
end
|
|
|
|
|
2016-09-12 01:42:06 -04:00
|
|
|
raise Discourse::InvalidAccess unless meets_tl?
|
2016-08-15 03:58:33 -04:00
|
|
|
|
2016-08-16 01:10:32 -04:00
|
|
|
validate_params
|
2019-01-03 22:46:18 -05:00
|
|
|
@application_name = params[:application_name]
|
2016-08-15 03:58:33 -04:00
|
|
|
|
2016-08-16 03:06:33 -04:00
|
|
|
# destroy any old keys we had
|
|
|
|
UserApiKey.where(user_id: current_user.id, client_id: params[:client_id]).destroy_all
|
|
|
|
|
2016-08-15 03:58:33 -04:00
|
|
|
key = UserApiKey.create!(
|
2019-01-03 22:46:18 -05:00
|
|
|
application_name: @application_name,
|
2016-08-15 03:58:33 -04:00
|
|
|
client_id: params[:client_id],
|
|
|
|
user_id: current_user.id,
|
2016-10-14 01:05:27 -04:00
|
|
|
push_url: params[:push_url],
|
2016-08-15 03:58:33 -04:00
|
|
|
key: SecureRandom.hex,
|
2016-10-14 01:05:27 -04:00
|
|
|
scopes: params[:scopes].split(",")
|
2016-08-15 03:58:33 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# we keep the payload short so it encrypts easily with public key
|
|
|
|
# it is often restricted to 128 chars
|
2019-01-03 22:46:18 -05:00
|
|
|
@payload = {
|
2016-08-15 03:58:33 -04:00
|
|
|
key: key.key,
|
|
|
|
nonce: params[:nonce],
|
2016-10-14 01:05:27 -04:00
|
|
|
push: key.has_push?,
|
|
|
|
api: AUTH_API_VERSION
|
2016-08-15 03:58:33 -04:00
|
|
|
}.to_json
|
|
|
|
|
|
|
|
public_key = OpenSSL::PKey::RSA.new(params[:public_key])
|
2019-01-03 22:46:18 -05:00
|
|
|
@payload = Base64.encode64(public_key.public_encrypt(@payload))
|
|
|
|
|
|
|
|
if params[:auth_redirect]
|
|
|
|
redirect_to("#{params[:auth_redirect]}?payload=#{CGI.escape(@payload)}")
|
|
|
|
else
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :show }
|
|
|
|
format.json do
|
|
|
|
instructions = I18n.t("user_api_key.instructions", application_name: @application_name)
|
|
|
|
render json: { payload: @payload, instructions: instructions }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-15 03:58:33 -04:00
|
|
|
end
|
|
|
|
|
2016-08-16 03:06:33 -04:00
|
|
|
def revoke
|
2016-09-02 03:08:46 -04:00
|
|
|
revoke_key = find_key if params[:id]
|
|
|
|
|
2016-09-02 02:57:41 -04:00
|
|
|
if current_key = request.env['HTTP_USER_API_KEY']
|
|
|
|
request_key = UserApiKey.find_by(key: current_key)
|
2016-09-02 03:08:46 -04:00
|
|
|
revoke_key ||= request_key
|
2016-10-14 01:05:27 -04:00
|
|
|
if request_key && request_key.id != revoke_key.id && !request_key.scopes.include?("write")
|
2016-09-02 02:57:41 -04:00
|
|
|
raise Discourse::InvalidAccess
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-02 03:08:46 -04:00
|
|
|
raise Discourse::NotFound unless revoke_key
|
|
|
|
|
2016-09-02 02:57:41 -04:00
|
|
|
revoke_key.update_columns(revoked_at: Time.zone.now)
|
|
|
|
|
2016-08-16 03:06:33 -04:00
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def undo_revoke
|
|
|
|
find_key.update_columns(revoked_at: nil)
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_key
|
|
|
|
key = UserApiKey.find(params[:id])
|
|
|
|
raise Discourse::InvalidAccess unless current_user.admin || key.user_id = current_user.id
|
|
|
|
key
|
|
|
|
end
|
|
|
|
|
2016-08-16 01:10:32 -04:00
|
|
|
def require_params
|
|
|
|
[
|
|
|
|
:public_key,
|
|
|
|
:nonce,
|
2016-10-14 01:05:27 -04:00
|
|
|
:scopes,
|
2016-08-16 01:10:32 -04:00
|
|
|
:client_id,
|
|
|
|
:application_name
|
2017-07-27 21:20:09 -04:00
|
|
|
].each { |p| params.require(p) }
|
2016-08-16 01:10:32 -04:00
|
|
|
end
|
|
|
|
|
2016-08-25 23:23:06 -04:00
|
|
|
def validate_params
|
2016-10-14 01:05:27 -04:00
|
|
|
requested_scopes = Set.new(params[:scopes].split(","))
|
2016-08-16 01:10:32 -04:00
|
|
|
|
2016-10-14 01:05:27 -04:00
|
|
|
raise Discourse::InvalidAccess unless UserApiKey.allowed_scopes.superset?(requested_scopes)
|
2016-08-16 01:10:32 -04:00
|
|
|
|
|
|
|
# our pk has got to parse
|
|
|
|
OpenSSL::PKey::RSA.new(params[:public_key])
|
|
|
|
end
|
|
|
|
|
2016-09-12 01:42:06 -04:00
|
|
|
def meets_tl?
|
|
|
|
current_user.staff? || current_user.trust_level >= SiteSetting.min_trust_level_for_user_api_key
|
|
|
|
end
|
|
|
|
|
2016-08-15 03:58:33 -04:00
|
|
|
end
|