Merge branch '19177-sharing-links-ui-config'. Refs #19177
[arvados.git] / services / api / app / models / api_client.rb
index 1f95d78c0c2446201c79e47f9200e3ae8123d5a4..c914051a349685aa5f73dc419a16a17449a4b2f5 100644 (file)
@@ -13,4 +13,43 @@ class ApiClient < ArvadosModel
     t.add :url_prefix
     t.add :is_trusted
   end
+
+  def is_trusted
+    (from_trusted_url && Rails.configuration.Login.IssueTrustedTokens) || super
+  end
+
+  protected
+
+  def from_trusted_url
+    norm_url_prefix = norm(self.url_prefix)
+
+    [Rails.configuration.Services.Workbench1.ExternalURL,
+     Rails.configuration.Services.Workbench2.ExternalURL,
+     "https://controller.api.client.invalid"].each do |url|
+      if norm_url_prefix == norm(url)
+        return true
+      end
+    end
+
+    Rails.configuration.Login.TrustedClients.keys.each do |url|
+      if norm_url_prefix == norm(url)
+        return true
+      end
+    end
+
+    false
+  end
+
+  def norm url
+    # normalize URL for comparison
+    url = URI(url.to_s)
+    if url.scheme == "https"
+      url.port == "443"
+    end
+    if url.scheme == "http"
+      url.port == "80"
+    end
+    url.path = "/"
+    url
+  end
 end