debian-mirror-gitlab/lib/gitlab/lfs_token.rb

55 lines
1.1 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2016-09-29 09:46:39 +05:30
module Gitlab
class LfsToken
2018-12-23 12:14:25 +05:30
attr_accessor :actor
2016-09-29 09:46:39 +05:30
2019-01-03 12:48:30 +05:30
TOKEN_LENGTH = 50
EXPIRY_TIME = 1800
2016-09-29 09:46:39 +05:30
def initialize(actor)
@actor =
case actor
when DeployKey, User
actor
when Key
actor.user
else
raise 'Bad Actor'
end
end
2019-01-03 12:48:30 +05:30
def token
Gitlab::Redis::SharedState.with do |redis|
token = redis.get(redis_shared_state_key)
token ||= Devise.friendly_token(TOKEN_LENGTH)
redis.set(redis_shared_state_key, token, ex: EXPIRY_TIME)
2016-09-29 09:46:39 +05:30
2019-01-03 12:48:30 +05:30
token
end
2016-09-29 09:46:39 +05:30
end
2018-03-17 18:26:18 +05:30
def deploy_key_pushable?(project)
actor.is_a?(DeployKey) && actor.can_push_to?(project)
end
2019-01-03 12:48:30 +05:30
def user?
actor.is_a?(User)
2016-09-29 09:46:39 +05:30
end
2019-01-03 12:48:30 +05:30
def type
actor.is_a?(User) ? :lfs_token : :lfs_deploy_token
2016-09-29 09:46:39 +05:30
end
2019-01-03 12:48:30 +05:30
def actor_name
actor.is_a?(User) ? actor.username : "lfs+deploy-key-#{actor.id}"
end
2016-09-29 09:46:39 +05:30
2019-01-03 12:48:30 +05:30
private
2018-12-23 12:14:25 +05:30
2019-01-03 12:48:30 +05:30
def redis_shared_state_key
"gitlab:lfs_token:#{actor.class.name.underscore}_#{actor.id}" if actor
2016-09-29 09:46:39 +05:30
end
end
end