Skip to content
Snippets Groups Projects
Commit 7015ef1b authored by Valery Sizov's avatar Valery Sizov
Browse files

Get rid of private message usage

parent 52e0d475
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -7,6 +7,7 @@ v7.13.0
- Enhance YAML validation
- Redirect back after authorization
- Change favicon
- Refactoring: Get rid of private_token usage in the frontend.
 
v7.12.1
- Runner without tag should pick builds without tag only
Loading
Loading
Loading
Loading
@@ -12,6 +12,12 @@ class ApplicationController < ActionController::Base
 
def current_user
@current_user ||= session[:current_user]
# Backward compatibility. Until 7.13 user session doesn't contain access_token
# Users with old session should be logged out
return nil if @current_user && @current_user.access_token.nil?
@current_user
end
 
def sign_in(user)
Loading
Loading
Loading
Loading
@@ -79,7 +79,7 @@ class ProjectsController < ApplicationController
 
def destroy
project.destroy
Network.new.disable_ci(project.gitlab_id, current_user.private_token)
Network.new.disable_ci(project.gitlab_id, current_user.access_token)
 
EventService.new.remove_project(current_user, project)
 
Loading
Loading
Loading
Loading
@@ -16,18 +16,6 @@ class Network
build_response(response)
end
 
def authenticate_by_token(api_opts)
opts = {
query: api_opts
}
endpoint = File.join(url, API_PREFIX, 'user.json')
response = self.class.get(endpoint, default_opts.merge(opts))
build_response(response)
end
def projects(api_opts, scope = :owned)
# Dont load archived projects
api_opts.merge!(archived: false)
Loading
Loading
@@ -74,12 +62,13 @@ class Network
build_response(response)
end
 
def enable_ci(project_id, api_opts, token)
def enable_ci(project_id, data, api_opts)
opts = {
body: api_opts.to_json
body: data.to_json,
query: api_opts
}
 
query = "projects/#{project_id}/services/gitlab-ci.json?private_token=#{token}"
query = "projects/#{project_id}/services/gitlab-ci.json"
endpoint = File.join(url, API_PREFIX, query)
response = self.class.put(endpoint, default_opts.merge(opts))
 
Loading
Loading
@@ -93,8 +82,8 @@ class Network
end
end
 
def disable_ci(project_id, token)
query = "projects/#{project_id}/services/gitlab-ci.json?private_token=#{token}"
def disable_ci(project_id, access_token)
query = "projects/#{project_id}/services/gitlab-ci.json?access_token=#{access_token}"
 
endpoint = File.join(url, API_PREFIX, query)
response = self.class.delete(endpoint, default_opts)
Loading
Loading
Loading
Loading
@@ -84,7 +84,12 @@ ls -la
end
 
def from_gitlab(user, scope = :owned, options)
opts = { private_token: user.private_token }
opts = if user.access_token
{ access_token: user.access_token }
else
{ private_token: user.private_token }
end
opts.merge! options
 
projects = Network.new.projects(opts.compact, scope)
Loading
Loading
Loading
Loading
@@ -57,7 +57,7 @@ class User
 
def can_manage_project?(project_gitlab_id)
opts = {
private_token: self.private_token,
access_token: self.access_token,
}
 
Rails.cache.fetch(cache_key('manage', project_gitlab_id, sync_at)) do
Loading
Loading
@@ -78,7 +78,7 @@ class User
 
def project_info(project_gitlab_id)
opts = {
private_token: self.private_token,
access_token: self.access_token,
}
 
Rails.cache.fetch(cache_key("project_info", project_gitlab_id, sync_at)) do
Loading
Loading
Loading
Loading
@@ -4,29 +4,17 @@ class UserSession
extend ActiveModel::Naming
 
def authenticate(auth_opts)
authenticate_via(auth_opts) do |network, options|
network.authenticate(options)
end
end
def authenticate_by_token(auth_opts)
result = authenticate_via(auth_opts) do |network, options|
network.authenticate_by_token(options)
end
result
end
private
def authenticate_via(options, &block)
user = block.call(Network.new, options)
network = Network.new
user = network.authenticate(auth_opts)
 
if user
user["access_token"] = auth_opts[:access_token]
return User.new(user)
else
nil
end
user
rescue
nil
end
Loading
Loading
Loading
Loading
@@ -7,12 +7,18 @@ class CreateProjectService
Project.transaction do
@project.save!
 
opts = {
data = {
token: @project.token,
project_url: project_route.gsub(":project_id", @project.id.to_s),
}
 
unless Network.new.enable_ci(@project.gitlab_id, opts, current_user.private_token)
auth_opts = if current_user.access_token
{ access_token: current_user.access_token }
else
{ private_token: current_user.private_token }
end
unless Network.new.enable_ci(@project.gitlab_id, data, auth_opts)
raise ActiveRecord::Rollback
end
end
Loading
Loading
Loading
Loading
@@ -18,7 +18,7 @@ module API
authenticate_project_token!(project)
 
user_session = UserSession.new
user = user_session.authenticate_by_token(private_token: params[:private_token])
user = user_session.authenticate(private_token: params[:private_token])
 
fork = CreateProjectService.new.execute(
user,
Loading
Loading
Loading
Loading
@@ -9,7 +9,7 @@ module API
options = {
private_token: (params[PRIVATE_TOKEN_PARAM] || env[PRIVATE_TOKEN_HEADER])
}
UserSession.new.authenticate_by_token(options)
UserSession.new.authenticate(options)
end
end
 
Loading
Loading
Loading
Loading
@@ -15,5 +15,6 @@
"is_admin":false,
"can_create_group":false,
"can_create_project":false,
"private_token":"Wvjy2Krpb7y8xi93owUz"
"private_token":"Wvjy2Krpb7y8xi93owUz",
"access_token":"Wvjy2Krpb7y8xi93owUz"
}
\ No newline at end of file
Loading
Loading
@@ -15,5 +15,6 @@
"is_admin":false,
"can_create_group":false,
"can_create_project":false,
"private_token":"Wvjy2Krpb7y8xi93owUz"
"private_token":"Wvjy2Krpb7y8xi93owUz",
"access_token":"Wvjy2Krpb7y8xi93owUz"
}
\ No newline at end of file
Loading
Loading
@@ -2,7 +2,6 @@ module StubGitlabCalls
def stub_gitlab_calls
stub_session
stub_user
stub_oauth_user
stub_project_8
stub_project_8_hooks
stub_projects
Loading
Loading
@@ -32,13 +31,9 @@ module StubGitlabCalls
def stub_user
f = File.read(Rails.root.join('spec/support/gitlab_stubs/user.json'))
 
stub_request(:get, "#{gitlab_url}api/v3/user.json?private_token=Wvjy2Krpb7y8xi93owUz").
stub_request(:get, "#{gitlab_url}api/v3/user?private_token=Wvjy2Krpb7y8xi93owUz").
with(:headers => {'Content-Type'=>'application/json'}).
to_return(:status => 200, :body => f, :headers => {'Content-Type'=>'application/json'})
end
def stub_oauth_user
f = File.read(Rails.root.join('spec/support/gitlab_stubs/user.json'))
 
stub_request(:get, "#{gitlab_url}api/v3/user?access_token=some_token").
with(:headers => {'Content-Type'=>'application/json'}).
Loading
Loading
@@ -57,6 +52,7 @@ module StubGitlabCalls
 
def stub_projects
f = File.read(Rails.root.join('spec/support/gitlab_stubs/projects.json'))
stub_request(:get, "#{gitlab_url}api/v3/projects.json?archived=false&private_token=Wvjy2Krpb7y8xi93owUz").
with(:headers => {'Content-Type'=>'application/json'}).
to_return(:status => 200, :body => f, :headers => {'Content-Type'=>'application/json'})
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