Skip to content
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

User/Role/Feature lookup improvements #888

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/api/auth_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class AuthController < BaseController
def show
requester_type = fetch_and_validate_requester_type
token_service = Environment.user_token_service
auth_token = token_service.generate_token(User.current_user.userid, requester_type)
NickLaMuro marked this conversation as resolved.
Show resolved Hide resolved
auth_token = token_service.generate_token(User.current_user, requester_type)
token_info = token_service.token_mgr(requester_type).token_get_info(auth_token)
res = {
:auth_token => auth_token,
Expand Down
13 changes: 9 additions & 4 deletions app/controllers/api/base_controller/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ def api_token_mgr
Environment.user_token_service.token_mgr('api')
end

def auth_user(userid)
auth_user_obj = User.lookup_by_identity(userid)
def auth_user(user_or_id)
auth_user_obj = if user_or_id.kind_of?(User)
user_or_id
else
User.lookup_by_identity(user_or_id, lookup_scope: :api_includes)
end

authorize_user_group(auth_user_obj)
validate_user_identity(auth_user_obj)
User.current_user = auth_user_obj
Expand Down Expand Up @@ -154,8 +159,8 @@ def authenticate_with_jwt

def basic_authentication(username, password)
timeout = ::Settings.api.authentication_timeout.to_i_with_method
user = User.authenticate(username, password, request, :require_user => true, :timeout => timeout)
auth_user(user.userid)
user = User.authenticate(username, password, request, :require_user => true, :timeout => timeout, :lookup_scope => :api_includes)
auth_user(user)
rescue MiqException::MiqEVMLoginError => e
raise AuthenticationError, e.message
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/base_controller/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def log_api_auth
group = User.current_user.current_group
log_request("Authorization", :user => User.current_user.userid,
:group => group.description,
:role => group.miq_user_role_name,
:role => group.miq_user_role.name,
:tenant => group.tenant.name)
end
end
Expand Down
10 changes: 7 additions & 3 deletions lib/services/api/user_token_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ def api_config
@api_config ||= ::Settings[base_config[:module]].to_hash
end

def generate_token(userid, requester_type, token_ttl: nil)
userid = userid.downcase
validate_userid(userid)
def generate_token(user_or_id, requester_type, token_ttl: nil)
NickLaMuro marked this conversation as resolved.
Show resolved Hide resolved
if user_or_id.kind_of?(User)
userid = user_or_id.userid.downcase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we still need to do the validation? (at least the "in my region" aspect of what the validate method does)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I may have screwed up the if here, and the end should be a line earlier... let me look into this...

(sorry I took so long to respond to this...)

else
userid = user_or_id.downcase
validate_userid(userid)
end
validate_requester_type(requester_type)

# Additional Requester type token ttl's for authentication
Expand Down
Loading