Commit b3e53024 authored by Gabriel Mazetto's avatar Gabriel Mazetto

handle decription failures and make token validation more standard implementation

parent f6300b21
...@@ -15,14 +15,14 @@ class Oauth::GeoAuthController < ActionController::Base ...@@ -15,14 +15,14 @@ class Oauth::GeoAuthController < ActionController::Base
def callback def callback
oauth = Gitlab::Geo::OauthSession.new(state: params[:state]) oauth = Gitlab::Geo::OauthSession.new(state: params[:state])
unless oauth.is_oauth_state_valid? unless oauth.is_oauth_state_valid?
redirect_to new_user_sessions_path redirect_to new_user_session_path
return return
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_with_gitlab(token) remote_user = oauth.authenticate_with_gitlab(token)
user = User.find(remote_user['id']) user = User.find_by(id: remote_user['id'])
if user && sign_in(user, bypass: true) if user && sign_in(user, bypass: true)
session[:access_token] = token session[:access_token] = token
...@@ -38,17 +38,22 @@ class Oauth::GeoAuthController < ActionController::Base ...@@ -38,17 +38,22 @@ class Oauth::GeoAuthController < ActionController::Base
token_string = oauth.extract_logout_token token_string = oauth.extract_logout_token
logout = Oauth2::LogoutTokenValidationService.new(current_user, token_string) logout = Oauth2::LogoutTokenValidationService.new(current_user, token_string)
if logout.valid? result = logout.validate
if result[:status] == :success
sign_out current_user sign_out current_user
redirect_to root_path
else else
access_token_error(logout.status) access_token_error(result[:error])
end end
redirect_to root_path
end end
private private
def invalid_credentials
@error = 'Cannot find user to login. Your account must have been deleted.'
render :error, layout: 'errors'
end
def undefined_oauth_application def undefined_oauth_application
@error = 'There are no OAuth application defined for this Geo node. Please ask your administrator to visit "Geo Nodes" on admin screen and click on "Repair authentication".' @error = 'There are no OAuth application defined for this Geo node. Please ask your administrator to visit "Geo Nodes" on admin screen and click on "Repair authentication".'
render :error, layout: 'errors' render :error, layout: 'errors'
......
module Oauth2 module Oauth2
class LogoutTokenValidationService class LogoutTokenValidationService < ::BaseService
attr_reader :status, :current_user attr_reader :status, :current_user
def initialize(user, access_token_string) def initialize(user, access_token_string)
...@@ -10,16 +10,16 @@ module Oauth2 ...@@ -10,16 +10,16 @@ module Oauth2
def validate def validate
return false unless access_token return false unless access_token
@status = Oauth2::AccessTokenValidationService.validate(access_token) status = Oauth2::AccessTokenValidationService.validate(access_token)
if @status == Oauth2::AccessTokenValidationService::VALID if status == Oauth2::AccessTokenValidationService::VALID
user = User.find(access_token.resource_owner_id) user = User.find(access_token.resource_owner_id)
if current_user == user if current_user == user
true success
end end
else else
false error(status)
end end
end end
......
...@@ -28,6 +28,8 @@ module Gitlab ...@@ -28,6 +28,8 @@ module Gitlab
cipher = logout_token_cipher(oauth_salt, :encrypt) cipher = logout_token_cipher(oauth_salt, :encrypt)
encrypted = cipher.update(access_token) + cipher.final encrypted = cipher.update(access_token) + cipher.final
self.state = "#{oauth_salt}:#{Base64.urlsafe_encode64(encrypted)}" self.state = "#{oauth_salt}:#{Base64.urlsafe_encode64(encrypted)}"
rescue OpenSSL::OpenSSLError
return false
end end
def extract_logout_token def extract_logout_token
......
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