Skip to content
Snippets Groups Projects
Commit 2772109a authored by Patricio Cano's avatar Patricio Cano
Browse files

Handle LFS token creation and retrieval in the same method, and in the same Redis connection.

Reset expiry time of token, if token is retrieved again before it expires.
parent a4944fb7
No related branches found
No related tags found
No related merge requests found
Loading
@@ -90,7 +90,7 @@ module API
Loading
@@ -90,7 +90,7 @@ module API
   
{ {
username: token_handler.actor_name, username: token_handler.actor_name,
lfs_token: token_handler.generate, lfs_token: token_handler.token,
repository_http_path: project.http_url_to_repo repository_http_path: project.http_url_to_repo
} }
end end
Loading
Loading
Loading
@@ -124,7 +124,7 @@ module Gitlab
Loading
@@ -124,7 +124,7 @@ module Gitlab
read_authentication_abilities read_authentication_abilities
end end
   
Result.new(actor, nil, token_handler.type, authentication_abilities) if Devise.secure_compare(token_handler.value, password) Result.new(actor, nil, token_handler.type, authentication_abilities) if Devise.secure_compare(token_handler.token, password)
end end
   
def build_access_token_check(login, password) def build_access_token_check(login, password)
Loading
Loading
Loading
@@ -17,21 +17,18 @@ module Gitlab
Loading
@@ -17,21 +17,18 @@ module Gitlab
end end
end end
   
def generate def token
return value if value
token = Devise.friendly_token(TOKEN_LENGTH)
Gitlab::Redis.with do |redis| Gitlab::Redis.with do |redis|
redis.set(redis_key, token, ex: EXPIRY_TIME) token = redis.get(redis_key)
end
   
token if token
end redis.expire(redis_key, EXPIRY_TIME)
else
token = Devise.friendly_token(TOKEN_LENGTH)
redis.set(redis_key, token, ex: EXPIRY_TIME)
end
   
def value token
Gitlab::Redis.with do |redis|
redis.get(redis_key)
end end
end end
   
Loading
Loading
Loading
@@ -64,7 +64,7 @@ describe Gitlab::Auth, lib: true do
Loading
@@ -64,7 +64,7 @@ describe Gitlab::Auth, lib: true do
it 'recognizes user lfs tokens' do it 'recognizes user lfs tokens' do
user = create(:user) user = create(:user)
ip = 'ip' ip = 'ip'
token = Gitlab::LfsToken.new(user).generate token = Gitlab::LfsToken.new(user).token
   
expect(gl_auth).to receive(:rate_limit!).with(ip, success: true, login: user.username) expect(gl_auth).to receive(:rate_limit!).with(ip, success: true, login: user.username)
expect(gl_auth.find_for_git_client(user.username, token, project: nil, ip: ip)).to eq(Gitlab::Auth::Result.new(user, nil, :lfs_token, full_authentication_abilities)) expect(gl_auth.find_for_git_client(user.username, token, project: nil, ip: ip)).to eq(Gitlab::Auth::Result.new(user, nil, :lfs_token, full_authentication_abilities))
Loading
@@ -73,7 +73,7 @@ describe Gitlab::Auth, lib: true do
Loading
@@ -73,7 +73,7 @@ describe Gitlab::Auth, lib: true do
it 'recognizes deploy key lfs tokens' do it 'recognizes deploy key lfs tokens' do
key = create(:deploy_key) key = create(:deploy_key)
ip = 'ip' ip = 'ip'
token = Gitlab::LfsToken.new(key).generate token = Gitlab::LfsToken.new(key).token
   
expect(gl_auth).to receive(:rate_limit!).with(ip, success: true, login: "lfs+deploy-key-#{key.id}") expect(gl_auth).to receive(:rate_limit!).with(ip, success: true, login: "lfs+deploy-key-#{key.id}")
expect(gl_auth.find_for_git_client("lfs+deploy-key-#{key.id}", token, project: nil, ip: ip)).to eq(Gitlab::Auth::Result.new(key, nil, :lfs_deploy_token, read_authentication_abilities)) expect(gl_auth.find_for_git_client("lfs+deploy-key-#{key.id}", token, project: nil, ip: ip)).to eq(Gitlab::Auth::Result.new(key, nil, :lfs_deploy_token, read_authentication_abilities))
Loading
Loading
require 'spec_helper' require 'spec_helper'
   
describe Gitlab::LfsToken, lib: true do describe Gitlab::LfsToken, lib: true do
describe '#generate and #value' do describe '#token' do
shared_examples 'an LFS token generator' do shared_examples 'an LFS token generator' do
it 'returns a randomly generated token' do it 'returns a randomly generated token' do
token = handler.generate token = handler.token
   
expect(token).not_to be_nil expect(token).not_to be_nil
expect(token).to be_a String expect(token).to be_a String
Loading
@@ -12,9 +12,9 @@ describe Gitlab::LfsToken, lib: true do
Loading
@@ -12,9 +12,9 @@ describe Gitlab::LfsToken, lib: true do
end end
   
it 'returns the correct token based on the key' do it 'returns the correct token based on the key' do
token = handler.generate token = handler.token
   
expect(handler.value).to eq(token) expect(handler.token).to eq(token)
end end
end end
   
Loading
Loading
Loading
@@ -111,7 +111,7 @@ describe API::API, api: true do
Loading
@@ -111,7 +111,7 @@ describe API::API, api: true do
   
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json_response['username']).to eq(user.username) expect(json_response['username']).to eq(user.username)
expect(json_response['lfs_token']).to eq(Gitlab::LfsToken.new(key).value) expect(json_response['lfs_token']).to eq(Gitlab::LfsToken.new(key).token)
   
expect(json_response['repository_http_path']).to eq(project.http_url_to_repo) expect(json_response['repository_http_path']).to eq(project.http_url_to_repo)
end end
Loading
@@ -131,7 +131,7 @@ describe API::API, api: true do
Loading
@@ -131,7 +131,7 @@ describe API::API, api: true do
   
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json_response['username']).to eq("lfs+deploy-key-#{key.id}") expect(json_response['username']).to eq("lfs+deploy-key-#{key.id}")
expect(json_response['lfs_token']).to eq(Gitlab::LfsToken.new(key).value) expect(json_response['lfs_token']).to eq(Gitlab::LfsToken.new(key).token)
expect(json_response['repository_http_path']).to eq(project.http_url_to_repo) expect(json_response['repository_http_path']).to eq(project.http_url_to_repo)
end end
end end
Loading
Loading
Loading
@@ -1133,11 +1133,11 @@ describe 'Git LFS API and storage' do
Loading
@@ -1133,11 +1133,11 @@ describe 'Git LFS API and storage' do
end end
   
def authorize_deploy_key def authorize_deploy_key
ActionController::HttpAuthentication::Basic.encode_credentials("lfs+deploy-key-#{key.id}", Gitlab::LfsToken.new(key).generate) ActionController::HttpAuthentication::Basic.encode_credentials("lfs+deploy-key-#{key.id}", Gitlab::LfsToken.new(key).token)
end end
   
def authorize_user_key def authorize_user_key
ActionController::HttpAuthentication::Basic.encode_credentials(user.username, Gitlab::LfsToken.new(user).generate) ActionController::HttpAuthentication::Basic.encode_credentials(user.username, Gitlab::LfsToken.new(user).token)
end end
   
def fork_project(project, user, object = nil) def fork_project(project, user, object = nil)
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment