X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/6a613ba162b66beab17bcdf6192034d6ed335ad4..864c3b0afd16c77e046f0072d8517d34c5a44792:/services/api/app/models/api_client_authorization.rb diff --git a/services/api/app/models/api_client_authorization.rb b/services/api/app/models/api_client_authorization.rb index 1c1c669deb..ee63c4d934 100644 --- a/services/api/app/models/api_client_authorization.rb +++ b/services/api/app/models/api_client_authorization.rb @@ -7,12 +7,15 @@ class ApiClientAuthorization < ArvadosModel include KindAndEtag include CommonApiTemplate extend CurrentApiClient + extend DbCurrentTime belongs_to :api_client belongs_to :user after_initialize :assign_random_api_token serialize :scopes, Array + before_validation :clamp_token_expiration + api_accessible :user, extend: :common do |t| t.add :owner_uuid t.add :user_id @@ -345,11 +348,16 @@ class ApiClientAuthorization < ArvadosModel auth.user = user auth.api_client_id = 0 end + # If stored_secret is set, we save stored_secret in the database + # but return the real secret to the caller. This way, if we end + # up returning the auth record to the client, they see the same + # secret they supplied, instead of the HMAC we saved in the + # database. stored_secret = stored_secret || secret auth.update_attributes!(user: user, api_token: stored_secret, api_client_id: 0, - expires_at: Time.now + Rails.configuration.Login.RemoteTokenRefresh) + expires_at: db_current_time + Rails.configuration.Login.RemoteTokenRefresh) Rails.logger.debug "cached remote token #{token_uuid} with secret #{stored_secret} in local db" auth.api_token = secret return auth @@ -379,6 +387,15 @@ class ApiClientAuthorization < ArvadosModel protected + def clamp_token_expiration + if !current_user.andand.is_admin && Rails.configuration.API.MaxTokenLifetime > 0 + max_token_expiration = db_current_time + Rails.configuration.API.MaxTokenLifetime + if (self.new_record? || self.expires_at_changed?) && (self.expires_at.nil? || self.expires_at > max_token_expiration) + self.expires_at = max_token_expiration + end + end + end + def permission_to_create current_user.andand.is_admin or (current_user.andand.id == self.user_id) end @@ -389,7 +406,6 @@ class ApiClientAuthorization < ArvadosModel end def log_update - super unless (saved_changes.keys - UNLOGGED_CHANGES).empty? end end