gitlab auth - needs test fix
This commit is contained in:
parent
708938ff45
commit
5dc60e0b41
1
Gemfile
1
Gemfile
@ -10,6 +10,7 @@ gem 'jbuilder', '~> 2.6'
|
|||||||
gem 'jquery-rails'
|
gem 'jquery-rails'
|
||||||
gem 'json', '~> 2.0.2'
|
gem 'json', '~> 2.0.2'
|
||||||
gem 'normalize-rails'
|
gem 'normalize-rails'
|
||||||
|
gem 'oauth2'
|
||||||
gem 'puma', '~> 3.0'
|
gem 'puma', '~> 3.0'
|
||||||
gem 'sass-rails', '~> 5.0'
|
gem 'sass-rails', '~> 5.0'
|
||||||
gem 'settingslogic', '~> 2.0.9'
|
gem 'settingslogic', '~> 2.0.9'
|
||||||
|
12
Gemfile.lock
12
Gemfile.lock
@ -57,6 +57,8 @@ GEM
|
|||||||
erubis (2.7.0)
|
erubis (2.7.0)
|
||||||
eventmachine (1.2.0.1)
|
eventmachine (1.2.0.1)
|
||||||
execjs (2.7.0)
|
execjs (2.7.0)
|
||||||
|
faraday (0.9.2)
|
||||||
|
multipart-post (>= 1.2, < 3)
|
||||||
ffi (1.9.14)
|
ffi (1.9.14)
|
||||||
figaro (1.1.1)
|
figaro (1.1.1)
|
||||||
thor (~> 0.14)
|
thor (~> 0.14)
|
||||||
@ -99,6 +101,7 @@ GEM
|
|||||||
railties (>= 4.2.0)
|
railties (>= 4.2.0)
|
||||||
thor (>= 0.14, < 2.0)
|
thor (>= 0.14, < 2.0)
|
||||||
json (2.0.2)
|
json (2.0.2)
|
||||||
|
jwt (1.5.1)
|
||||||
listen (3.1.5)
|
listen (3.1.5)
|
||||||
rb-fsevent (~> 0.9, >= 0.9.4)
|
rb-fsevent (~> 0.9, >= 0.9.4)
|
||||||
rb-inotify (~> 0.9, >= 0.9.7)
|
rb-inotify (~> 0.9, >= 0.9.7)
|
||||||
@ -120,6 +123,8 @@ GEM
|
|||||||
minitest (>= 5.0)
|
minitest (>= 5.0)
|
||||||
ruby-progressbar
|
ruby-progressbar
|
||||||
multi_json (1.12.1)
|
multi_json (1.12.1)
|
||||||
|
multi_xml (0.5.5)
|
||||||
|
multipart-post (2.0.0)
|
||||||
mysql2 (0.4.4)
|
mysql2 (0.4.4)
|
||||||
nenv (0.3.0)
|
nenv (0.3.0)
|
||||||
nio4r (1.2.1)
|
nio4r (1.2.1)
|
||||||
@ -130,6 +135,12 @@ GEM
|
|||||||
notiffany (0.1.1)
|
notiffany (0.1.1)
|
||||||
nenv (~> 0.1)
|
nenv (~> 0.1)
|
||||||
shellany (~> 0.0)
|
shellany (~> 0.0)
|
||||||
|
oauth2 (1.2.0)
|
||||||
|
faraday (>= 0.8, < 0.10)
|
||||||
|
jwt (~> 1.0)
|
||||||
|
multi_json (~> 1.3)
|
||||||
|
multi_xml (~> 0.5)
|
||||||
|
rack (>= 1.2, < 3)
|
||||||
parser (2.3.1.2)
|
parser (2.3.1.2)
|
||||||
ast (~> 2.2)
|
ast (~> 2.2)
|
||||||
pkg-config (1.1.7)
|
pkg-config (1.1.7)
|
||||||
@ -253,6 +264,7 @@ DEPENDENCIES
|
|||||||
minitest-reporters
|
minitest-reporters
|
||||||
mysql2 (>= 0.3.18, < 0.5)
|
mysql2 (>= 0.3.18, < 0.5)
|
||||||
normalize-rails
|
normalize-rails
|
||||||
|
oauth2
|
||||||
pry-byebug
|
pry-byebug
|
||||||
pry-rails
|
pry-rails
|
||||||
puma (~> 3.0)
|
puma (~> 3.0)
|
||||||
|
@ -1,3 +1,11 @@
|
|||||||
class ApplicationController < ActionController::Base
|
class ApplicationController < ActionController::Base
|
||||||
protect_from_forgery with: :exception
|
protect_from_forgery with: :exception
|
||||||
|
|
||||||
|
before_action :verify_session
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def verify_session
|
||||||
|
redirect_to logout_path and return if session[:token].nil?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
40
app/controllers/auth_controller.rb
Normal file
40
app/controllers/auth_controller.rb
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
class AuthController < ApplicationController
|
||||||
|
skip_before_action :verify_session
|
||||||
|
|
||||||
|
def login
|
||||||
|
end
|
||||||
|
|
||||||
|
def logout
|
||||||
|
session.destroy
|
||||||
|
redirect_to login_path
|
||||||
|
end
|
||||||
|
|
||||||
|
def auth
|
||||||
|
redirect_to client.auth_code.authorize_url(redirect_uri: ENV['gitlab_callback'])
|
||||||
|
end
|
||||||
|
|
||||||
|
def callback
|
||||||
|
access_token = client.auth_code.get_token(params[:code], redirect_uri: ENV['gitlab_callback'])
|
||||||
|
session[:token] = access_token.token
|
||||||
|
user_info(access_token)
|
||||||
|
|
||||||
|
redirect_to accounts_path
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def user_info access_token
|
||||||
|
@user_info ||= JSON.parse(access_token.get(ENV['gitlab_oauth'] + '/api/v3/user').body)
|
||||||
|
session[:name] = @user_info['name']
|
||||||
|
session[:avatar] = @user_info['avatar_url']
|
||||||
|
session[:admin] = @user_info['is_admin']
|
||||||
|
end
|
||||||
|
|
||||||
|
def client
|
||||||
|
OAuth2::Client.new(
|
||||||
|
ENV['gitlab_client'],
|
||||||
|
ENV['gitlab_secret'],
|
||||||
|
site: ENV['gitlab_oauth']
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
1
app/views/auth/login.erb
Normal file
1
app/views/auth/login.erb
Normal file
@ -0,0 +1 @@
|
|||||||
|
<%= link_to "Authenticate with gitlab", auth_path %>
|
@ -9,6 +9,9 @@
|
|||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
<header>
|
||||||
|
<p>Welcome <%= session[:name] %></p>
|
||||||
|
</header>
|
||||||
<%= yield %>
|
<%= yield %>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -5,6 +5,10 @@ defaults: &defaults
|
|||||||
mysql_usr: "user"
|
mysql_usr: "user"
|
||||||
mysql_pwd: "password"
|
mysql_pwd: "password"
|
||||||
full_app_url: "localhost:3000"
|
full_app_url: "localhost:3000"
|
||||||
|
gitlab_oauth: provider-url
|
||||||
|
gitlab_client: client-id
|
||||||
|
gitlab_secret: client-secret
|
||||||
|
gitlab_callback: local-callback
|
||||||
|
|
||||||
development:
|
development:
|
||||||
<<: *defaults
|
<<: *defaults
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
Rails.application.routes.draw do
|
Rails.application.routes.draw do
|
||||||
resources :accounts
|
|
||||||
get 'accounts/reveal/:id', to: 'accounts#reveal', as: :reveal_password
|
get 'accounts/reveal/:id', to: 'accounts#reveal', as: :reveal_password
|
||||||
|
resources :accounts
|
||||||
|
|
||||||
|
get "logout", to: "auth#logout", as: :logout
|
||||||
|
get "login", to: "auth#login", as: :login
|
||||||
|
get "auth", to: "auth#auth", as: :auth
|
||||||
|
get "auth/callback", to: 'auth#callback'
|
||||||
|
|
||||||
|
root to: "accounts#index"
|
||||||
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
|
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
|
||||||
end
|
end
|
||||||
|
@ -3,6 +3,9 @@ require 'test_helper'
|
|||||||
class AccountsControllerTest < ActionDispatch::IntegrationTest
|
class AccountsControllerTest < ActionDispatch::IntegrationTest
|
||||||
setup do
|
setup do
|
||||||
@account = accounts(:account1)
|
@account = accounts(:account1)
|
||||||
|
# get login_path
|
||||||
|
# session[:token] = 'fake-oauth-token'
|
||||||
|
# session[:name] = "Fake User"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "should get index" do
|
test "should get index" do
|
||||||
|
8
test/controllers/auth_controller_test.rb
Normal file
8
test/controllers/auth_controller_test.rb
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class AuthControllerTest < ActionDispatch::IntegrationTest
|
||||||
|
# test "should get auth" do
|
||||||
|
# get auth_url
|
||||||
|
# assert_response :redirect
|
||||||
|
# end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user