-
Notifications
You must be signed in to change notification settings - Fork 991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #37936 - As a user, I want to invalidate jwt for specific user #10357
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,8 +12,8 @@ class UsersController < V2::BaseController | |||||
['compute_attributes'] | ||||||
|
||||||
before_action :find_optional_nested_object | ||||||
skip_before_action :authorize, :only => [:extlogin] | ||||||
before_action :authenticate, :only => [:extlogin] | ||||||
skip_before_action :authorize, :only => [:extlogin, :invalidate_jwt] | ||||||
before_action :authenticate, :only => [:extlogin, :invalidate_jwt] | ||||||
|
||||||
api :GET, "/users/", N_("List all users") | ||||||
api :GET, "/auth_source_ldaps/:auth_source_ldap_id/users", N_("List all users for LDAP authentication source") | ||||||
|
@@ -96,7 +96,7 @@ def create | |||||
api :PUT, "/users/:id/", N_("Update a user") | ||||||
description <<-DOC | ||||||
Adds role 'Default role' to the user if it is not already present. | ||||||
Only another admin can change the admin account attribute. | ||||||
Only another admin can change the admin account attribute. | ||||||
DOC | ||||||
param :id, String, :required => true | ||||||
param_group :user_update | ||||||
|
@@ -111,6 +111,23 @@ def update | |||||
end | ||||||
end | ||||||
|
||||||
api :PATCH, "/users/:id/invalidate_jwt", N_("Get vm attributes of host") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copy-paste leftovers? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, my bad |
||||||
param :id, String, :required => true | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose you would want to have an array parameter here. To be able to invalidate tokens for multiple users. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. currently in the scope, (atleast UI wise) we can invalidate tokens for a specific user or all users because otherwise I would have to add a select action in the UI. I was thinking, I can add another endpoint to invalidate all at once. Allowing multiple (but not all) users token invalidation can take some time to integrate |
||||||
description <<-DOC | ||||||
Invalidate JWT for a specific user | ||||||
DOC | ||||||
|
||||||
def invalidate_jwt | ||||||
@user = User.find(params[:id]) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This will filter only the users that Now you will have a set of users that you can safely delete the tokens for: JwtSecret.where(user_id: @users).destroy_all |
||||||
if !@user | ||||||
render :json => { :error => _("User %s does not exist.") % @user.login} | ||||||
elsif !User.current.can?(:edit_users, @user) | ||||||
deny_access N_("User %s does not have permissions to invalidate JWT." % User.current.login) | ||||||
elsif User.current.can?(:edit_users, @user) && @user.invalidate_jwt.blank? | ||||||
process_success _('Successfully invalidated JWT for %s.' % @user.login) | ||||||
end | ||||||
end | ||||||
|
||||||
api :DELETE, "/users/:id/", N_("Delete a user") | ||||||
param :id, String, :required => true | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -8,7 +8,7 @@ class UsersController < ApplicationController | |||
rescue_from ActionController::InvalidAuthenticityToken, with: :login_token_reload | ||||
skip_before_action :require_mail, :only => [:edit, :update, :logout, :stop_impersonation] | ||||
skip_before_action :require_login, :check_user_enabled, :authorize, :session_expiry, :update_activity_time, :set_taxonomy, :set_gettext_locale_db, :only => [:login, :logout, :extlogout] | ||||
skip_before_action :authorize, :only => [:extlogin, :impersonate, :stop_impersonation] | ||||
skip_before_action :authorize, :only => [:extlogin, :impersonate, :stop_impersonation, :invalidate_jwt] | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same as for the API endpoint. You can specify the new action in the security block:
|
||||
before_action :require_admin, :only => :impersonate | ||||
after_action :update_activity_time, :only => :login | ||||
before_action :verify_active_session, :only => :login | ||||
|
@@ -93,6 +93,15 @@ def impersonate | |||
end | ||||
end | ||||
|
||||
def invalidate_jwt | ||||
@user = find_resource(:edit_users) | ||||
if @user.invalidate_jwt.blank? | ||||
process_success( | ||||
:success_msg => _('Successfully invalidated JWT for %s.') % @user.login | ||||
) | ||||
end | ||||
end | ||||
|
||||
def stop_impersonation | ||||
if session[:impersonated_by].present? | ||||
user = User.unscoped.find_by_id(session[:impersonated_by]) | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need to skip authorization for
invalidate_jwt
action?You can specify the action in the security block:
foreman/config/initializers/f_foreman_permissions.rb
Line 566 in eb66c09
This will make sure the current user has the
edit_users
permission before accessing the endpoint.