-
Notifications
You must be signed in to change notification settings - Fork 993
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #37936 - Invalidate jwt for any user or users(API)
- Loading branch information
1 parent
bc2dbbf
commit a2b09f5
Showing
5 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module Api | ||
module V2 | ||
class RegistrationTokensController < V2::BaseController | ||
include Foreman::Controller::UsersMixin | ||
|
||
include Foreman::Controller::Parameters::User | ||
include Foreman::Controller::AutoCompleteSearch | ||
before_action :authenticate, :only => [:invalidate_tokens, :invalidate_jwt] | ||
|
||
def resource_class | ||
User | ||
end | ||
|
||
def find_resource(permission = :view_users) | ||
editing_self? ? User.find(User.current.id) : User.authorized(permission).except_hidden.find(params[:id]) | ||
end | ||
|
||
def action_permission | ||
case params[:action] | ||
when 'invalidate_tokens', 'invalidate_jwt' | ||
'edit' | ||
else | ||
super | ||
end | ||
end | ||
|
||
api :DELETE, '/users/:id/registration_tokens', N_("Invalidate all registration Tokens for a specific user.") | ||
description <<-DOC | ||
The user you specify will no longer be able to register hosts by using their JWTs. | ||
DOC | ||
param :id, String, :desc => N_("ID of the user"), :required => true | ||
|
||
def invalidate_jwt | ||
@user = find_resource(:edit_users) | ||
unless @user | ||
raise ::Foreman::Exception.new(N_("No record found for %s"), params[:id]) | ||
end | ||
@user.jwt_secret&.destroy | ||
process_success _('Successfully invalidated JWTs for %s.' % @user.login) | ||
end | ||
|
||
api :DELETE, "/registration_tokens", N_("Invalidate all JSON Web Tokens (JWTs) for multiple users.") | ||
param :search, String, :required => true | ||
description <<-DOC | ||
The users you specify will no longer be able to register hosts by using their JWTs. | ||
DOC | ||
|
||
def invalidate_tokens | ||
raise ::Foreman::Exception.new(N_("Please provide search parameters")) if params[:search].blank? | ||
@users = resource_scope_for_index(:permission => :edit_users).except_hidden.uniq | ||
if @users.blank? | ||
raise ::Foreman::Exception.new(N_("No record found %s"), params[:search]) | ||
end | ||
JwtSecret.where(user_id: @users).destroy_all | ||
process_success _("Successfully invalidated JWTs for %s.\n" % @users.pluck(:login).to_sentence) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
test/controllers/api/v2/registration_tokens_controller_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'test_helper' | ||
|
||
class Api::V2::RegistrationTokensControllerTest < ActionController::TestCase | ||
test 'user shall invalidate tokens for self' do | ||
user = users(:one) | ||
FactoryBot.create(:jwt_secret, token: 'test_jwt_secret', user: user) | ||
delete :invalidate_jwt, params: { :id => user.id.to_s}, session: set_session_user(user) | ||
user.reload | ||
assert_nil user.jwt_secret | ||
end | ||
|
||
test 'user with edit permission should be able to invalidate jwt for another user' do | ||
setup_user 'edit', 'users' | ||
user = users(:one) | ||
FactoryBot.create(:jwt_secret, token: 'test_jwt_secret', user: user) | ||
delete :invalidate_tokens, params: { :search => "id ^ (#{user.id})"}, session: set_session_user(User.current) | ||
user.reload | ||
assert_nil user.jwt_secret | ||
end | ||
|
||
test 'user without edit permission should not be able to invalidate jwt for another user' do | ||
User.current = users(:one) | ||
user = users(:two) | ||
FactoryBot.create(:jwt_secret, token: 'test_jwt_secret', user: user) | ||
delete :invalidate_tokens, params: { :search => "id ^ (#{user.id})"}, session: set_session_user(User.current) | ||
user.reload | ||
assert_response :forbidden | ||
end | ||
|
||
test 'invalidating jwt should fail without search params' do | ||
setup_user 'edit', 'users' | ||
user = users(:two) | ||
FactoryBot.create(:jwt_secret, token: 'test_jwt_secret', user: user) | ||
delete :invalidate_tokens, session: set_session_user(User.current) | ||
user.reload | ||
assert_response :error | ||
end | ||
end |