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
2021-04-17 20:07:23 +05:30
include EachBatch
2022-11-25 23:54:43 +05:30
include CreatedAtFilterable
include Gitlab :: SQL :: Pattern
2020-05-24 23:13:21 +05:30
extend :: Gitlab :: Utils :: Override
2019-07-07 11:18:12 +05:30
2023-05-27 22:25:52 +05:30
add_authentication_token_field :token ,
digest : true ,
format_with_prefix : :prefix_from_application_current_settings
2016-06-22 15:30:34 +05:30
2021-02-22 17:27:13 +05:30
# PATs are 20 characters + optional configurable settings prefix (0..20)
TOKEN_LENGTH_RANGE = ( 20 .. 40 ) . freeze
2023-07-09 08:55:56 +05:30
MAX_PERSONAL_ACCESS_TOKEN_LIFETIME_IN_DAYS = 365
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
2023-03-04 22:38:38 +05:30
after_initialize :set_default_scopes , if : :persisted?
2017-08-17 22:00:37 +05:30
before_save :ensure_token
2023-03-17 16:20:25 +05:30
# During the implementation of Admin Mode for API, tokens of
# administrators should automatically get the `admin_mode` scope as well
# See https://gitlab.com/gitlab-org/gitlab/-/issues/42692
2023-04-23 21:23:45 +05:30
before_create :add_admin_mode_scope , if : - > { Feature . disabled? ( :admin_mode_for_api ) && user_admin? }
2023-03-17 16:20:25 +05:30
2022-08-27 11:52:29 +05:30
scope :active , - > { not_revoked . not_expired }
2020-07-28 23:09:34 +05:30
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 " ) }
2022-10-11 01:57:18 +05:30
scope :last_used_before_or_unused , - > ( date ) { where ( " personal_access_tokens.created_at < :date AND (last_used_at < :date OR last_used_at IS NULL) " , date : 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 ) }
2022-08-27 11:52:29 +05:30
scope :order_expires_at_asc_id_desc , - > { reorder ( expires_at : :asc , id : :desc ) }
2023-06-20 00:43:36 +05:30
scope :project_access_token , - > { includes ( :user ) . references ( :user ) . merge ( User . project_bot ) }
scope :owner_is_human , - > { includes ( :user ) . references ( :user ) . merge ( User . human ) }
2022-11-25 23:54:43 +05:30
scope :last_used_before , - > ( date ) { where ( " last_used_at <= ? " , date ) }
scope :last_used_after , - > ( date ) { where ( " last_used_at >= ? " , date ) }
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
2023-07-09 08:55:56 +05:30
validate :expires_at_before_instance_max_expiry_date , on : :create
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
2023-07-09 08:55:56 +05:30
# fall back to default value until background migration has updated all
# existing PATs and we can add a validation
# https://gitlab.com/gitlab-org/gitlab/-/issues/369123
def expires_at = ( value )
datetime = if Feature . enabled? ( :default_pat_expiration )
value . presence || MAX_PERSONAL_ACCESS_TOKEN_LIFETIME_IN_DAYS . days . from_now
else
value
end
super ( datetime )
end
2020-05-24 23:13:21 +05:30
override :simple_sorts
def self . simple_sorts
super . merge (
{
2022-08-27 11:52:29 +05:30
'expires_at_asc_id_desc' = > - > { order_expires_at_asc_id_desc }
2020-05-24 23:13:21 +05:30
}
)
end
2021-02-22 17:27:13 +05:30
def self . token_prefix
Gitlab :: CurrentSettings . current_application_settings . personal_access_token_prefix
end
2022-11-25 23:54:43 +05:30
def self . search ( query )
fuzzy_search ( query , [ :name ] )
end
2022-04-04 11:22:00 +05:30
def project_access_token?
user & . project_bot?
end
2017-08-17 22:00:37 +05:30
protected
2017-09-10 17:25:29 +05:30
def validate_scopes
2023-04-23 21:23:45 +05:30
valid_scopes = Gitlab :: Auth . all_available_scopes
valid_scopes += [ Gitlab :: Auth :: ADMIN_MODE_SCOPE ] if Feature . disabled? ( :admin_mode_for_api )
2023-03-17 16:20:25 +05:30
unless revoked || scopes . all? { | scope | valid_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
2021-04-17 20:07:23 +05:30
# When only loading a select set of attributes, for example using `EachBatch`,
# the `scopes` attribute is not present, so we can't initialize it.
return unless has_attribute? ( :scopes )
2018-03-17 18:26:18 +05:30
self . scopes = Gitlab :: Auth :: DEFAULT_SCOPES if self . scopes . empty?
end
2023-03-17 16:20:25 +05:30
def user_admin?
user . admin? # rubocop: disable Cop/UserAdmin
end
def add_admin_mode_scope
self . scopes += [ Gitlab :: Auth :: ADMIN_MODE_SCOPE . to_s ]
end
2023-05-27 22:25:52 +05:30
def prefix_from_application_current_settings
self . class . token_prefix
end
2023-07-09 08:55:56 +05:30
def expires_at_before_instance_max_expiry_date
return unless Feature . enabled? ( :default_pat_expiration )
return unless expires_at
if expires_at > MAX_PERSONAL_ACCESS_TOKEN_LIFETIME_IN_DAYS . days . from_now
errors . add ( :expires_at , _ ( 'must expire in 365 days' ) )
end
end
2016-06-22 15:30:34 +05:30
end
2020-01-01 13:55:28 +05:30
2021-06-08 01:23:25 +05:30
PersonalAccessToken . prepend_mod_with ( 'PersonalAccessToken' )