gitlab auth - needs test fix

This commit is contained in:
Mark Moser 2016-09-12 22:15:44 -05:00
parent 708938ff45
commit 5dc60e0b41
10 changed files with 88 additions and 2 deletions

View File

@ -10,6 +10,7 @@ gem 'jbuilder', '~> 2.6'
gem 'jquery-rails'
gem 'json', '~> 2.0.2'
gem 'normalize-rails'
gem 'oauth2'
gem 'puma', '~> 3.0'
gem 'sass-rails', '~> 5.0'
gem 'settingslogic', '~> 2.0.9'

View File

@ -57,6 +57,8 @@ GEM
erubis (2.7.0)
eventmachine (1.2.0.1)
execjs (2.7.0)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
ffi (1.9.14)
figaro (1.1.1)
thor (~> 0.14)
@ -99,6 +101,7 @@ GEM
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (2.0.2)
jwt (1.5.1)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
@ -120,6 +123,8 @@ GEM
minitest (>= 5.0)
ruby-progressbar
multi_json (1.12.1)
multi_xml (0.5.5)
multipart-post (2.0.0)
mysql2 (0.4.4)
nenv (0.3.0)
nio4r (1.2.1)
@ -130,6 +135,12 @@ GEM
notiffany (0.1.1)
nenv (~> 0.1)
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)
ast (~> 2.2)
pkg-config (1.1.7)
@ -253,6 +264,7 @@ DEPENDENCIES
minitest-reporters
mysql2 (>= 0.3.18, < 0.5)
normalize-rails
oauth2
pry-byebug
pry-rails
puma (~> 3.0)

View File

@ -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

View 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
View File

@ -0,0 +1 @@
<%= link_to "Authenticate with gitlab", auth_path %>

View File

@ -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>

View File

@ -5,6 +5,10 @@ defaults: &defaults
mysql_usr: "user"
mysql_pwd: "password"
full_app_url: "localhost:3000"
gitlab_oauth: provider-url
gitlab_client: client-id
gitlab_secret: client-secret
gitlab_callback: local-callback
development:
<<: *defaults

View File

@ -1,6 +1,12 @@
Rails.application.routes.draw do
resources :accounts
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
end

View File

@ -3,6 +3,9 @@ require 'test_helper'
class AccountsControllerTest < ActionDispatch::IntegrationTest
setup do
@account = accounts(:account1)
# get login_path
# session[:token] = 'fake-oauth-token'
# session[:name] = "Fake User"
end
test "should get index" do

View 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