Commit 8c6e9eb0 authored by Gabriel Mazetto's avatar Gabriel Mazetto

More refactor to Geo Authentication with OAuth

parent d39a9b2f
...@@ -18,7 +18,7 @@ class Oauth::GeoAuthController < ActionController::Base ...@@ -18,7 +18,7 @@ class Oauth::GeoAuthController < ActionController::Base
end end
token = oauth.get_token(params[:code], redirect_uri: oauth_geo_callback_url) token = oauth.get_token(params[:code], redirect_uri: oauth_geo_callback_url)
remote_user = oauth.authenticate(access_token: token) remote_user = Geo::RemoteNode.new.authenticate(token)
user = User.find(remote_user['id']) user = User.find(remote_user['id'])
...@@ -27,7 +27,7 @@ class Oauth::GeoAuthController < ActionController::Base ...@@ -27,7 +27,7 @@ class Oauth::GeoAuthController < ActionController::Base
redirect_to(return_to || root_path) redirect_to(return_to || root_path)
else else
@error = 'Invalid credentials' @error = 'Invalid credentials'
render :new render :error
end end
end end
......
class Geo::OauthSession class Geo::OauthSession
include ActiveModel::Model include ActiveModel::Model
include HTTParty
attr_accessor :state attr_accessor :state
attr_accessor :return_to attr_accessor :return_to
API_PREFIX = '/api/v3/'
def is_oauth_state_valid? def is_oauth_state_valid?
return true unless state return true unless state
salt, hmac, return_to = state.split(':', 3) salt, hmac, return_to = state.split(':', 3)
...@@ -25,15 +22,6 @@ class Geo::OauthSession ...@@ -25,15 +22,6 @@ class Geo::OauthSession
state.split(':', 3)[2] if state state.split(':', 3)[2] if state
end end
def authenticate(access_token)
opts = {
query: access_token
}
response = self.class.get(authenticate_endpoint, default_opts.merge(opts))
build_response(response)
end
def authorize_url(params = {}) def authorize_url(params = {})
oauth_client.auth_code.authorize_url(params) oauth_client.auth_code.authorize_url(params)
end end
...@@ -73,28 +61,7 @@ class Geo::OauthSession ...@@ -73,28 +61,7 @@ class Geo::OauthSession
Gitlab::Geo.oauth_authentication Gitlab::Geo.oauth_authentication
end end
def authenticate_endpoint
File.join(primary_node_url, API_PREFIX, 'user')
end
def primary_node_url def primary_node_url
Gitlab::Geo.primary_node.url Gitlab::Geo.primary_node.url
end end
def default_opts
{
headers: { 'Content-Type' => 'application/json' },
}
end
def build_response(response)
case response.code
when 200
response.parsed_response
when 401
raise UnauthorizedError
else
nil
end
end
end end
class Geo::RemoteNode
class UnauthorizedError < StandardError
end
include HTTParty
API_PREFIX = '/api/v3/'
def authenticate(access_token)
opts = {
query: { access_token: access_token }
}
response = self.class.get(authenticate_endpoint, default_opts.merge(opts))
build_response(response)
end
private
def authenticate_endpoint
File.join(primary_node_url, API_PREFIX, 'user')
end
def primary_node_url
Gitlab::Geo.primary_node.url
end
def default_opts
{
headers: { 'Content-Type' => 'application/json' },
}
end
def build_response(response)
case response.code
when 200
response.parsed_response
when 401
raise UnauthorizedError
else
nil
end
end
end
...@@ -95,7 +95,7 @@ module Gitlab ...@@ -95,7 +95,7 @@ module Gitlab
ENV['GITLAB_PATH_OUTSIDE_HOOK'] = ENV['PATH'] ENV['GITLAB_PATH_OUTSIDE_HOOK'] = ENV['PATH']
# Gitlab Geo Middleware support # Gitlab Geo Middleware support
config.middleware.use 'Gitlab::Middleware::ReadonlyGeo' config.middleware.insert_after ActionDispatch::Flash, 'Gitlab::Middleware::ReadonlyGeo'
config.generators do |g| config.generators do |g|
g.factory_girl false g.factory_girl false
......
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