diff --git a/Gemfile b/Gemfile index 3db2165..de1ffcf 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index c58f802..59ee7eb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1c07694..5ab006f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/auth_controller.rb b/app/controllers/auth_controller.rb new file mode 100644 index 0000000..ee7b2f4 --- /dev/null +++ b/app/controllers/auth_controller.rb @@ -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 diff --git a/app/views/auth/login.erb b/app/views/auth/login.erb new file mode 100644 index 0000000..2d1d14c --- /dev/null +++ b/app/views/auth/login.erb @@ -0,0 +1 @@ +<%= link_to "Authenticate with gitlab", auth_path %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 1574739..71b8c52 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -4,11 +4,14 @@
Welcome <%= session[:name] %>
+