2016-04-15 07:36:44 +00:00
|
|
|
class PersonalAccessToken < ActiveRecord::Base
|
2016-04-25 09:00:59 +00:00
|
|
|
include TokenAuthenticatable
|
|
|
|
add_authentication_token_field :token
|
|
|
|
|
2016-11-22 08:53:53 +00:00
|
|
|
serialize :scopes, Array
|
|
|
|
|
2016-04-15 07:36:44 +00:00
|
|
|
belongs_to :user
|
|
|
|
|
2016-04-22 08:33:11 +00:00
|
|
|
scope :active, -> { where(revoked: false).where("expires_at >= NOW() OR expires_at IS NULL") }
|
|
|
|
scope :inactive, -> { where("revoked = true OR expires_at < NOW()") }
|
2016-04-15 15:24:20 +00:00
|
|
|
|
2017-01-31 10:21:29 +00:00
|
|
|
validate :validate_scopes
|
|
|
|
|
2016-04-15 07:36:44 +00:00
|
|
|
def self.generate(params)
|
|
|
|
personal_access_token = self.new(params)
|
2016-04-25 09:00:59 +00:00
|
|
|
personal_access_token.ensure_token
|
2016-04-15 07:36:44 +00:00
|
|
|
personal_access_token
|
|
|
|
end
|
2016-04-15 15:24:20 +00:00
|
|
|
|
|
|
|
def revoke!
|
|
|
|
self.revoked = true
|
|
|
|
self.save
|
|
|
|
end
|
2017-01-31 10:21:29 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def validate_scopes
|
|
|
|
unless Set.new(scopes.map(&:to_sym)).subset?(Set.new(Gitlab::Auth::API_SCOPES))
|
|
|
|
errors.add :scopes, "can only contain API scopes"
|
|
|
|
end
|
|
|
|
end
|
2016-04-15 07:36:44 +00:00
|
|
|
end
|