gitlab auth - needs test fix
This commit is contained in:
@ -1,3 +1,11 @@
|
||||
class ApplicationController < ActionController::Base
|
||||
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
|
||||
|
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 %>
|
@ -4,11 +4,14 @@
|
||||
<title>FtpManager</title>
|
||||
<%= csrf_meta_tags %>
|
||||
|
||||
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
|
||||
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
|
||||
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header>
|
||||
<p>Welcome <%= session[:name] %></p>
|
||||
</header>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
||||
|
Reference in New Issue
Block a user