Merge branch 'master' into 16678-login-tokens-lifetime-config
[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(self.url_prefix) == norm(Rails.configuration.Services.Workbench1.ExternalURL) ||
25       norm(self.url_prefix) == norm(Rails.configuration.Services.Workbench2.ExternalURL)
26   end
27
28   def norm url
29     # normalize URL for comparison
30     url = URI(url)
31     if url.scheme == "https"
32       url.port == "443"
33     end
34     if url.scheme == "http"
35       url.port == "80"
36     end
37     url.path = "/"
38     url
39   end
40 end