Class: PersonalAccessToken

Inherits:
ApplicationRecord show all
Extended by:
Gitlab::Utils::Override
Includes:
Expirable, Sortable, TokenAuthenticatable
Defined in:
app/models/personal_access_token.rb

Constant Summary collapse

REDIS_EXPIRY_TIME =
3.minutes
TOKEN_LENGTH =
20

Constants included from Expirable

Expirable::DAYS_TO_EXPIRE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::Override

extended, extensions, included, method_added, override, prepended, queue_verification, verify!

Methods included from Expirable

#expired?, #expires?, #expires_soon?

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.redis_getdel(user_id) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/personal_access_token.rb', line 46

def self.redis_getdel(user_id)
  Gitlab::Redis::SharedState.with do |redis|
    redis_key = redis_shared_state_key(user_id)
    encrypted_token = redis.get(redis_key)
    redis.del(redis_key)

    begin
      Gitlab::CryptoHelper.aes256_gcm_decrypt(encrypted_token)
    rescue => ex
      logger.warn "Failed to decrypt #{self.name} value stored in Redis for key ##{redis_key}: #{ex.class}"
      encrypted_token
    end
  end
end

.redis_store!(user_id, token) ⇒ Object


61
62
63
64
65
66
67
# File 'app/models/personal_access_token.rb', line 61

def self.redis_store!(user_id, token)
  encrypted_token = Gitlab::CryptoHelper.aes256_gcm_encrypt(token)

  Gitlab::Redis::SharedState.with do |redis|
    redis.set(redis_shared_state_key(user_id), encrypted_token, ex: REDIS_EXPIRY_TIME)
  end
end

.simple_sortsObject


70
71
72
73
74
75
76
77
# File 'app/models/personal_access_token.rb', line 70

def self.simple_sorts
  super.merge(
    {
      'expires_at_asc' => -> { order_expires_at_asc },
      'expires_at_desc' => -> { order_expires_at_desc }
    }
  )
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)

42
43
44
# File 'app/models/personal_access_token.rb', line 42

def active?
  !revoked? && !expired?
end

#revoke!Object


38
39
40
# File 'app/models/personal_access_token.rb', line 38

def revoke!
  update!(revoked: true)
end