debian-mirror-gitlab/app/models/personal_access_token.rb

109 lines
3.3 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
class PersonalAccessToken < ApplicationRecord
2017-08-17 22:00:37 +05:30
include Expirable
2016-06-22 15:30:34 +05:30
include TokenAuthenticatable
2020-01-01 13:55:28 +05:30
include Sortable
2020-05-24 23:13:21 +05:30
extend ::Gitlab::Utils::Override
2019-07-07 11:18:12 +05:30
add_authentication_token_field :token, digest: true
2016-06-22 15:30:34 +05:30
2018-03-17 18:26:18 +05:30
REDIS_EXPIRY_TIME = 3.minutes
2021-02-22 17:27:13 +05:30
# PATs are 20 characters + optional configurable settings prefix (0..20)
TOKEN_LENGTH_RANGE = (20..40).freeze
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
serialize :scopes, Array # rubocop:disable Cop/ActiveRecordSerialize
2017-08-17 22:00:37 +05:30
2016-06-22 15:30:34 +05:30
belongs_to :user
2017-08-17 22:00:37 +05:30
before_save :ensure_token
2020-07-28 23:09:34 +05:30
scope :active, -> { where("revoked = false AND (expires_at >= CURRENT_DATE OR expires_at IS NULL)") }
scope :expiring_and_not_notified, ->(date) { where(["revoked = false AND expire_notification_delivered = false AND expires_at >= CURRENT_DATE AND expires_at <= ?", date]) }
2020-10-24 23:57:45 +05:30
scope :expired_today_and_not_notified, -> { where(["revoked = false AND expires_at = CURRENT_DATE AND after_expiry_notification_delivered = false"]) }
2020-07-28 23:09:34 +05:30
scope :inactive, -> { where("revoked = true OR expires_at < CURRENT_DATE") }
2017-08-17 22:00:37 +05:30
scope :with_impersonation, -> { where(impersonation: true) }
scope :without_impersonation, -> { where(impersonation: false) }
2020-07-28 23:09:34 +05:30
scope :revoked, -> { where(revoked: true) }
scope :not_revoked, -> { where(revoked: [false, nil]) }
2020-01-01 13:55:28 +05:30
scope :for_user, -> (user) { where(user: user) }
2021-01-29 00:20:46 +05:30
scope :for_users, -> (users) { where(user: users) }
2020-01-01 13:55:28 +05:30
scope :preload_users, -> { preload(:user) }
2020-05-24 23:13:21 +05:30
scope :order_expires_at_asc, -> { reorder(expires_at: :asc) }
scope :order_expires_at_desc, -> { reorder(expires_at: :desc) }
2016-06-22 15:30:34 +05:30
2017-08-17 22:00:37 +05:30
validates :scopes, presence: true
2017-09-10 17:25:29 +05:30
validate :validate_scopes
2016-06-22 15:30:34 +05:30
2018-03-17 18:26:18 +05:30
after_initialize :set_default_scopes, if: :persisted?
2016-06-22 15:30:34 +05:30
def revoke!
2017-09-10 17:25:29 +05:30
update!(revoked: true)
2016-06-22 15:30:34 +05:30
end
2017-08-17 22:00:37 +05:30
def active?
!revoked? && !expired?
end
2018-03-17 18:26:18 +05:30
def self.redis_getdel(user_id)
Gitlab::Redis::SharedState.with do |redis|
2020-05-24 23:13:21 +05:30
redis_key = redis_shared_state_key(user_id)
encrypted_token = redis.get(redis_key)
redis.del(redis_key)
2018-11-18 11:00:15 +05:30
begin
Gitlab::CryptoHelper.aes256_gcm_decrypt(encrypted_token)
rescue => ex
2020-05-24 23:13:21 +05:30
logger.warn "Failed to decrypt #{self.name} value stored in Redis for key ##{redis_key}: #{ex.class}"
2018-11-18 11:00:15 +05:30
encrypted_token
end
2018-03-17 18:26:18 +05:30
end
end
def self.redis_store!(user_id, token)
2018-11-18 11:00:15 +05:30
encrypted_token = Gitlab::CryptoHelper.aes256_gcm_encrypt(token)
2018-03-17 18:26:18 +05:30
Gitlab::Redis::SharedState.with do |redis|
2018-11-18 11:00:15 +05:30
redis.set(redis_shared_state_key(user_id), encrypted_token, ex: REDIS_EXPIRY_TIME)
2018-03-17 18:26:18 +05:30
end
end
2020-05-24 23:13:21 +05:30
override :simple_sorts
def self.simple_sorts
super.merge(
{
'expires_at_asc' => -> { order_expires_at_asc },
'expires_at_desc' => -> { order_expires_at_desc }
}
)
end
2021-02-22 17:27:13 +05:30
def self.token_prefix
Gitlab::CurrentSettings.current_application_settings.personal_access_token_prefix
end
override :format_token
def format_token(token)
"#{self.class.token_prefix}#{token}"
end
2017-08-17 22:00:37 +05:30
protected
2017-09-10 17:25:29 +05:30
def validate_scopes
2019-07-31 22:56:46 +05:30
unless revoked || scopes.all? { |scope| Gitlab::Auth.all_available_scopes.include?(scope.to_sym) }
2017-09-10 17:25:29 +05:30
errors.add :scopes, "can only contain available scopes"
2017-08-17 22:00:37 +05:30
end
end
2018-03-17 18:26:18 +05:30
def set_default_scopes
self.scopes = Gitlab::Auth::DEFAULT_SCOPES if self.scopes.empty?
end
def self.redis_shared_state_key(user_id)
"gitlab:personal_access_token:#{user_id}"
end
2016-06-22 15:30:34 +05:30
end
2020-01-01 13:55:28 +05:30
PersonalAccessToken.prepend_if_ee('EE::PersonalAccessToken')