16306: Merge branch 'master'
[arvados.git] / services / api / lib / create_superuser_token.rb
index 72b1ae7bc97c708368a92844fa60c020f67d7783..7a18d970582786e860a0378717340e5e6ba3f3f1 100755 (executable)
@@ -1,3 +1,7 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
 # Install the supplied string (or a randomly generated token, if none
 # is given) as an API token that authenticates to the system user account.
 
@@ -26,15 +30,18 @@ module CreateSuperUserToken
       # need to create a token
       if !api_client_auth
         # Get (or create) trusted api client
-        apiClient =  ApiClient.find_or_create_by_url_prefix_and_is_trusted("ssh://root@localhost/", true)
+        apiClient =  ApiClient.
+          find_or_create_by(url_prefix: "ssh://root@localhost/",
+                            is_trusted: true)
 
         # Check if there is an unexpired superuser token corresponding to this api client
-        api_client_auth = ApiClientAuthorization.where(
-                'user_id = ? AND
-                 api_client_id = ? AND
-                 scopes = ? AND
-                 (expires_at IS NULL OR expires_at > CURRENT_TIMESTAMP)',
-               system_user.id, apiClient.id, ['all'].to_yaml).first
+        api_client_auth =
+          ApiClientAuthorization.
+          where(user_id: system_user.id).
+          where(api_client_id: apiClient.id).
+          where_serialized(:scopes, ['all']).
+          where('(expires_at IS NULL OR expires_at > CURRENT_TIMESTAMP)').
+          first
 
         # none exist; create one with the supplied token
         if !api_client_auth
@@ -47,7 +54,7 @@ module CreateSuperUserToken
         end
       end
 
-      api_client_auth.api_token
+      "v2/" + api_client_auth.uuid + "/" + api_client_auth.api_token
     end
   end
 end