16923: user/pass api_client is trusted by default
[arvados.git] / services / api / app / models / api_client.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class ApiClient < ArvadosModel
6   include HasUuid
7   include KindAndEtag
8   include CommonApiTemplate
9   has_many :api_client_authorizations
10
11   api_accessible :user, extend: :common do |t|
12     t.add :name
13     t.add :url_prefix
14     t.add :is_trusted
15   end
16
17   def is_trusted
18     (from_trusted_url && Rails.configuration.Login.TokenLifetime == 0) || super
19   end
20
21   protected
22
23   def from_trusted_url
24     norm_url_prefix = norm(self.url_prefix)
25     norm_url_prefix == norm(Rails.configuration.Services.Workbench1.ExternalURL) or
26       norm_url_prefix == norm(Rails.configuration.Services.Workbench2.ExternalURL) or
27       norm_url_prefix == norm("https://controller.api.client.invalid")
28   end
29
30   def norm url
31     # normalize URL for comparison
32     url = URI(url)
33     if url.scheme == "https"
34       url.port == "443"
35     end
36     if url.scheme == "http"
37       url.port == "80"
38     end
39     url.path = "/"
40     url
41   end
42 end