Skip to content

Commit db287af

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 4810302 commit db287af

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,13 @@ def api_token_mgr
8686
Environment.user_token_service.token_mgr('api')
8787
end
8888

89-
def auth_user(userid)
90-
auth_user_obj = User.lookup_by_identity(userid, lookup_scope: :api_includes)
89+
def auth_user(user_or_id)
90+
auth_user_obj = if user_or_id.kind_of?(User)
91+
user_or_id
92+
else
93+
User.lookup_by_identity(user_or_id, lookup_scope: :api_includes)
94+
end
95+
9196
authorize_user_group(auth_user_obj)
9297
validate_user_identity(auth_user_obj)
9398
User.current_user = auth_user_obj
@@ -155,7 +160,7 @@ def authenticate_with_jwt
155160
def basic_authentication(username, password)
156161
timeout = ::Settings.api.authentication_timeout.to_i_with_method
157162
user = User.authenticate(username, password, request, :require_user => true, :timeout => timeout, :lookup_scope => :api_includes)
158-
auth_user(user.userid)
163+
auth_user(user)
159164
rescue MiqException::MiqEVMLoginError => e
160165
raise AuthenticationError, e.message
161166
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)