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

49 lines
940 B
Ruby
Raw Normal View History

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