Skip to content

Commit 890fe16

Browse files
committed
Various user_or_id fixes
There are a few places in the API where we re-fetch the User when it is already available. These fixes allow for making use of the existing user that already exists instead of calling another lookup. The two places involved are: - When authenticating using .basic_authentication - When generating a token for the user (on login)
1 parent 92385ac commit 890fe16

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

app/controllers/api/auth_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class AuthController < BaseController
55
def show
66
requester_type = fetch_and_validate_requester_type
77
token_service = Environment.user_token_service
8-
auth_token = token_service.generate_token(User.current_user.userid, requester_type)
8+
auth_token = token_service.generate_token(User.current_user, requester_type)
99
token_info = token_service.token_mgr(requester_type).token_get_info(auth_token)
1010
res = {
1111
:auth_token => auth_token,

app/controllers/api/base_controller/authentication.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ def api_token_mgr
9191
Environment.user_token_service.token_mgr('api')
9292
end
9393

94-
def auth_user(userid)
95-
auth_user_obj = User.lookup_by_identity(userid, lookup_scope: :api_includes)
94+
def auth_user(user_or_id)
95+
auth_user_obj = user_or_id if user_or_id.kind_of?(User)
96+
auth_user_obj ||= User.lookup_by_identity(user_or_id, lookup_scope: :api_includes)
9697
authorize_user_group(auth_user_obj)
9798
validate_user_identity(auth_user_obj)
9899
User.current_user = auth_user_obj
@@ -160,7 +161,7 @@ def authenticate_with_jwt
160161
def basic_authentication(username, password)
161162
timeout = ::Settings.api.authentication_timeout.to_i_with_method
162163
user = User.authenticate(username, password, request, :require_user => true, :timeout => timeout, :lookup_scope => :api_includes)
163-
auth_user(user.userid)
164+
auth_user(user)
164165
rescue MiqException::MiqEVMLoginError => e
165166
raise AuthenticationError, e.message
166167
end

lib/services/api/user_token_service.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ def api_config
2323
@api_config ||= ::Settings[base_config[:module]].to_hash
2424
end
2525

26-
def generate_token(userid, requester_type, token_ttl: nil)
27-
userid = userid.downcase
28-
validate_userid(userid)
26+
def generate_token(user_or_id, requester_type, token_ttl: nil)
27+
if user_or_id.kind_of?(User)
28+
userid = user_or_id.userid.downcase
29+
else
30+
userid = user_or_id.downcase
31+
validate_userid(userid)
32+
end
2933
validate_requester_type(requester_type)
3034

3135
# Additional Requester type token ttl's for authentication

0 commit comments

Comments
 (0)