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 ...@@ -25,9 +25,7 @@ module LfsHelper
def lfs_download_access? def lfs_download_access?
return false unless project.lfs_enabled? return false unless project.lfs_enabled?
return true if project.public? return true if project.public? || ci? || lfs_deploy_key?
return true if ci?
return true if lfs_deploy_key?
(user && user.can?(:download_code, project)) (user && user.can?(:download_code, project))
end end
......
...@@ -80,16 +80,18 @@ module API ...@@ -80,16 +80,18 @@ module API
key = Key.find(params[:key_id]) key = Key.find(params[:key_id])
user = key.user user = key.user
if user token_handler =
token = Gitlab::LfsToken.new(user).generate if user
response = { username: user.username, lfs_token: token } Gitlab::LfsToken.new(user)
else else
token = Gitlab::LfsToken.new(key).generate Gitlab::LfsToken.new(key)
response = { username: "lfs-deploy-key-#{key.id}", lfs_token: token } end
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 end
get "/merge_request_urls" do get "/merge_request_urls" do
......
...@@ -117,15 +117,16 @@ module Gitlab ...@@ -117,15 +117,16 @@ module Gitlab
end end
def lfs_token_check(login, password) def lfs_token_check(login, password)
if login.include?('lfs-deploy-key') actor =
key = DeployKey.find(login.gsub('lfs-deploy-key-', '')) if login.include?('lfs-deploy-key')
token = Gitlab::LfsToken.new(key).value DeployKey.find(login.gsub('lfs-deploy-key-', ''))
Result.new(key, :lfs_deploy_token) if key && token == password else
else User.by_login(login)
user = User.by_login(login) end
token = Gitlab::LfsToken.new(user).value
Result.new(user, :lfs_token) if user && token == password token_handler = Gitlab::LfsToken.new(actor)
end
Result.new(actor, token_handler.type) if actor && token_handler.value == password
end end
end end
end end
......
...@@ -22,6 +22,14 @@ module Gitlab ...@@ -22,6 +22,14 @@ module Gitlab
end end
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 private
def redis_key def redis_key
......
...@@ -23,6 +23,14 @@ describe Gitlab::LfsToken, lib: true do ...@@ -23,6 +23,14 @@ describe Gitlab::LfsToken, lib: true do
let(:handler) { described_class.new(actor) } let(:handler) { described_class.new(actor) }
it_behaves_like 'an LFS token generator' 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 end
context 'when the actor is a deploy key' do context 'when the actor is a deploy key' do
...@@ -30,6 +38,14 @@ describe Gitlab::LfsToken, lib: true do ...@@ -30,6 +38,14 @@ describe Gitlab::LfsToken, lib: true do
let(:handler) { described_class.new(actor) } let(:handler) { described_class.new(actor) }
it_behaves_like 'an LFS token generator' 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 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