Commit c25630ee authored by Patricio Cano's avatar Patricio Cano

Refactored handling of the `LfsToken` and added functionality to it to simplify external code.

parent 48f1a61f
......@@ -25,9 +25,7 @@ module LfsHelper
def lfs_download_access?
return false unless project.lfs_enabled?
return true if project.public?
return true if ci?
return true if lfs_deploy_key?
return true if project.public? || ci? || lfs_deploy_key?
(user && user.can?(:download_code, project))
end
......
......@@ -80,16 +80,18 @@ module API
key = Key.find(params[:key_id])
user = key.user
token_handler =
if user
token = Gitlab::LfsToken.new(user).generate
response = { username: user.username, lfs_token: token }
Gitlab::LfsToken.new(user)
else
token = Gitlab::LfsToken.new(key).generate
response = { username: "lfs-deploy-key-#{key.id}", lfs_token: token }
Gitlab::LfsToken.new(key)
end
response[:repository_http_path] = project.http_url_to_repo
response
{
username: token_handler.actor_name,
lfs_token: token_handler.generate,
repository_http_path: project.http_url_to_repo
}
end
get "/merge_request_urls" do
......
......@@ -117,15 +117,16 @@ module Gitlab
end
def lfs_token_check(login, password)
actor =
if login.include?('lfs-deploy-key')
key = DeployKey.find(login.gsub('lfs-deploy-key-', ''))
token = Gitlab::LfsToken.new(key).value
Result.new(key, :lfs_deploy_token) if key && token == password
DeployKey.find(login.gsub('lfs-deploy-key-', ''))
else
user = User.by_login(login)
token = Gitlab::LfsToken.new(user).value
Result.new(user, :lfs_token) if user && token == password
User.by_login(login)
end
token_handler = Gitlab::LfsToken.new(actor)
Result.new(actor, token_handler.type) if actor && token_handler.value == password
end
end
end
......
......@@ -22,6 +22,14 @@ module Gitlab
end
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
def redis_key
......
......@@ -23,6 +23,14 @@ describe Gitlab::LfsToken, lib: true do
let(:handler) { described_class.new(actor) }
it_behaves_like 'an LFS token generator'
it 'returns the correct username' do
expect(handler.actor_name).to eq(actor.username)
end
it 'returns the correct token type' do
expect(handler.type).to eq(:lfs_token)
end
end
context 'when the actor is a deploy key' do
......@@ -30,6 +38,14 @@ describe Gitlab::LfsToken, lib: true do
let(:handler) { described_class.new(actor) }
it_behaves_like 'an LFS token generator'
it 'returns the correct username' do
expect(handler.actor_name).to eq("lfs-deploy-key-#{actor.id}")
end
it 'returns the correct token type' do
expect(handler.type).to eq(:lfs_deploy_token)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment