1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 class ApiClientAuthorization < ArvadosModel
8 include CommonApiTemplate
9 extend CurrentApiClient
11 belongs_to :api_client
13 after_initialize :assign_random_api_token
14 serialize :scopes, Array
16 api_accessible :user, extend: :common do |t|
21 t.add :created_by_ip_address
22 t.add :default_owner_uuid
25 t.add :last_used_by_ip_address
29 UNLOGGED_CHANGES = ['last_used_at', 'last_used_by_ip_address', 'updated_at']
31 def assign_random_api_token
32 self.api_token ||= rand(2**256).to_s(36)
39 self.user_id_changed? ? User.where(id: self.user_id_was).first.andand.uuid : self.user.andand.uuid
41 def owner_uuid_changed?
45 def modified_by_client_uuid
48 def modified_by_client_uuid=(x) end
50 def modified_by_user_uuid
53 def modified_by_user_uuid=(x) end
58 def modified_at=(x) end
60 def scopes_allow?(req_s)
61 scopes.each do |scope|
62 return true if (scope == 'all') or (scope == req_s) or
63 ((scope.end_with? '/') and (req_s.start_with? scope))
68 def scopes_allow_request?(request)
69 method = request.request_method
71 (scopes_allow?(['HEAD', request.path].join(' ')) ||
72 scopes_allow?(['GET', request.path].join(' ')))
74 scopes_allow?([method, request.path].join(' '))
79 super.except 'api_token'
82 def self.default_orders
83 ["#{table_name}.id desc"]
86 def self.remote_host(uuid:)
87 Rails.configuration.remote_hosts[uuid[0..4]] ||
88 (Rails.configuration.remote_hosts_via_dns &&
89 uuid[0..4]+".arvadosapi.com")
92 def self.validate(token:, remote:)
94 remote ||= Rails.configuration.uuid_prefix
98 _, uuid, secret = token.split('/')
99 auth = ApiClientAuthorization.
100 includes(:user, :api_client).
101 where('uuid=? and (expires_at is null or expires_at > CURRENT_TIMESTAMP)', uuid).
103 if auth && auth.user &&
104 (secret == auth.api_token ||
105 secret == OpenSSL::HMAC.hexdigest('sha1', auth.api_token, remote))
107 elsif uuid[0..4] != Rails.configuration.uuid_prefix
108 # Token was issued by a different cluster. If it's expired or
109 # missing in our database, ask the originating cluster to
111 arv = Arvados.new(api_host: remote_host(uuid: uuid),
114 remote_user = arv.user.current(remote: Rails.configuration.uuid_prefix)
116 logger.warn "remote authentication with token #{token.inspect} failed: #{e}"
119 if !remote_user.is_a?(Hash) || !remote_user[:uuid].is_a?(String) || remote_user[:uuid][0..4] != uuid[0..4]
120 logger.warn "remote authentication rejected: remote_user=#{remote_user.inspect}"
123 act_as_system_user do
124 # Add/update user and token in our database so we can
125 # validate subsequent requests faster.
126 user = User.find_or_create_by(uuid: remote_user[:uuid])
127 user.update_attributes!(remote_user.merge(is_admin: false))
128 auth = ApiClientAuthorization.
130 find_or_create_by(uuid: uuid,
134 # Accept this token (and don't reload the user record) for
135 # 5 minutes. TODO: Request the actual api_client_auth
136 # record from the remote server in case it wants the token
138 auth.update_attributes!(expires_at: Time.now + 5.minutes)
143 auth = ApiClientAuthorization.
144 includes(:user, :api_client).
145 where('api_token=? and (expires_at is null or expires_at > CURRENT_TIMESTAMP)', token).
156 def permission_to_create
157 current_user.andand.is_admin or (current_user.andand.id == self.user_id)
160 def permission_to_update
161 (permission_to_create and
162 not uuid_changed? and
163 not user_id_changed? and
164 not owner_uuid_changed?)
168 super unless (changed - UNLOGGED_CHANGES).empty?