+ # 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
+ (expires_at IS NULL OR expires_at > CURRENT_TIMESTAMP)',
+ system_user.id, apiClient.id).first
+
+ # none exist; create one with the supplied token
+ if !api_client_auth
+ api_client_auth = ApiClientAuthorization.
+ new(user: system_user,
+ api_client_id: apiClient.id,
+ created_by_ip_address: '::1',
+ api_token: supplied_token)
+ api_client_auth.save!
+ end
+ end
+
+ api_client_auth.api_token
+ end