2016-04-15 03:36:44 -04:00
|
|
|
class PersonalAccessToken < ActiveRecord::Base
|
2016-12-27 11:26:57 -05:00
|
|
|
include Expirable
|
2016-04-25 05:00:59 -04:00
|
|
|
include TokenAuthenticatable
|
|
|
|
add_authentication_token_field :token
|
|
|
|
|
2016-11-22 03:53:53 -05:00
|
|
|
serialize :scopes, Array
|
|
|
|
|
2016-04-15 03:36:44 -04:00
|
|
|
belongs_to :user
|
|
|
|
|
2017-02-23 12:47:06 -05:00
|
|
|
before_save :ensure_token
|
|
|
|
|
2017-02-27 13:56:54 -05:00
|
|
|
scope :active, -> { where("revoked = false AND (expires_at >= NOW() OR expires_at IS NULL)") }
|
2016-04-22 04:33:11 -04:00
|
|
|
scope :inactive, -> { where("revoked = true OR expires_at < NOW()") }
|
2017-02-27 13:56:54 -05:00
|
|
|
scope :with_impersonation, -> { where(impersonation: true) }
|
|
|
|
scope :without_impersonation, -> { where(impersonation: false) }
|
2016-04-15 11:24:20 -04:00
|
|
|
|
2017-02-06 10:39:35 -05:00
|
|
|
validates :scopes, presence: true
|
|
|
|
validate :validate_api_scopes
|
2017-01-31 05:21:29 -05:00
|
|
|
|
2016-04-15 11:24:20 -04:00
|
|
|
def revoke!
|
|
|
|
self.revoked = true
|
|
|
|
self.save
|
|
|
|
end
|
2016-12-27 11:26:57 -05:00
|
|
|
|
|
|
|
def active?
|
|
|
|
!revoked? && !expired?
|
|
|
|
end
|
2017-03-07 11:16:08 -05:00
|
|
|
|
2017-01-31 05:21:29 -05:00
|
|
|
protected
|
|
|
|
|
2017-02-06 10:39:35 -05:00
|
|
|
def validate_api_scopes
|
|
|
|
unless scopes.all? { |scope| Gitlab::Auth::API_SCOPES.include?(scope.to_sym) }
|
2017-01-31 05:21:29 -05:00
|
|
|
errors.add :scopes, "can only contain API scopes"
|
|
|
|
end
|
|
|
|
end
|
2016-04-15 03:36:44 -04:00
|
|
|
end
|