From 2a7491edcd2150a2889701390904a71398a95aae Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Mon, 17 Apr 2017 00:42:03 +0200 Subject: [PATCH] Google authentication added --- Gemfile | 2 +- Gemfile.lock | 20 +++++++++++++++++++ README.developer.md | 12 ++++------- .../users/omniauth_callbacks_controller.rb | 4 ++++ app/models/user.rb | 20 +++++-------------- app/views/devise/sessions/new.slim | 1 + config/initializers/devise.rb | 6 ++---- 7 files changed, 37 insertions(+), 28 deletions(-) diff --git a/Gemfile b/Gemfile index 1fcb52c..5a78344 100644 --- a/Gemfile +++ b/Gemfile @@ -118,7 +118,7 @@ gem "sprockets" # Authentication against Google, Facebook and others gem 'omniauth' # gem 'omniauth-amazon' -# gem 'omniauth-google-oauth2' +gem 'omniauth-google-oauth2' # gem 'omniauth-github' # gem 'omniauth-twitter' gem 'omniauth-openid' diff --git a/Gemfile.lock b/Gemfile.lock index 79ff53a..6711dfa 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -86,6 +86,8 @@ GEM execjs (2.7.0) fancybox2-rails (0.2.7) railties (>= 3.1.0) + faraday (0.11.0) + multipart-post (>= 1.2, < 3) ffi (1.9.18) font-awesome-rails (4.7.0.1) railties (>= 3.2, < 5.1) @@ -124,6 +126,7 @@ GEM js_cookie_rails (2.1.4) railties (>= 3.1) json (1.8.6) + jwt (1.5.6) libv8 (3.16.14.19) listen (3.0.8) rb-fsevent (~> 0.9, >= 0.9.4) @@ -149,6 +152,8 @@ GEM minitest (>= 5.0) ruby-progressbar multi_json (1.12.1) + multi_xml (0.6.0) + multipart-post (2.0.0) nenv (0.3.0) nio4r (2.0.0) nokogiri (1.7.1) @@ -156,9 +161,23 @@ GEM notiffany (0.1.1) nenv (~> 0.1) shellany (~> 0.0) + oauth2 (1.3.1) + faraday (>= 0.8, < 0.12) + jwt (~> 1.0) + multi_json (~> 1.3) + multi_xml (~> 0.5) + rack (>= 1.2, < 3) omniauth (1.6.1) hashie (>= 3.4.6, < 3.6.0) rack (>= 1.6.2, < 3) + omniauth-google-oauth2 (0.4.1) + jwt (~> 1.5.2) + multi_json (~> 1.3) + omniauth (>= 1.1.1) + omniauth-oauth2 (>= 1.3.1) + omniauth-oauth2 (1.4.0) + oauth2 (~> 1.0) + omniauth (~> 1.2) omniauth-openid (1.0.1) omniauth (~> 1.0) rack-openid (~> 1.3.1) @@ -307,6 +326,7 @@ DEPENDENCIES minitest-rails minitest-reporters omniauth + omniauth-google-oauth2 omniauth-openid paperclip pg diff --git a/README.developer.md b/README.developer.md index c88af9f..cbacf4a 100644 --- a/README.developer.md +++ b/README.developer.md @@ -11,15 +11,11 @@ https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview ## Google+ -The client ID for the debshots application is: +The client ID for the debshots and the private key must be set as environment +variables before running the application: - 398593918966-0fpmit0pb6ptio1ndsie5dfcnqhei63l.apps.googleusercontent.com - -The private key is: - - uRQq_dLs1kx7l5sYIyEyPVRc - -(This information is connected to the christoph.haas@gmail.com account.) +* GOOGLE_CLIENT_ID=… +* GOOGLE_CLIENT_SECRET=… ## Amazon diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb index 74bfae8..c1f6d98 100644 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -11,6 +11,10 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController login_via 'StackExchange' end + def google_oauth2 + login_via 'Google' + end + def login_via(provider_name) # You need to implement the method below in your model (e.g. app/models/user.rb) @user = User.from_omniauth(request.env["omniauth.auth"]) diff --git a/app/models/user.rb b/app/models/user.rb index 0ccd856..41fcb48 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -10,7 +10,11 @@ class User < ApplicationRecord # :validatable, :timeoutable, # :lockable, - :omniauthable, :omniauth_providers => [:launchpad,:stackexchange] + :omniauthable, :omniauth_providers => [ + :launchpad, + :stackexchange, + :google_oauth2 + ] def self.from_omniauth(auth) @@ -23,19 +27,5 @@ class User < ApplicationRecord user.password = Devise.friendly_token[0,20] end end - # - # def self.from_omniauth(access_token) - # data = access_token.info - # user = User.where(:email => data["email"]).first - # - # # Uncomment the section below if you want users to be created if they don't exist - # # unless user - # # user = User.create(name: data["name"], - # # email: data["email"], - # # password: Devise.friendly_token[0,20] - # # ) - # # end - # user - # end end diff --git a/app/views/devise/sessions/new.slim b/app/views/devise/sessions/new.slim index 3b79140..186ed6d 100644 --- a/app/views/devise/sessions/new.slim +++ b/app/views/devise/sessions/new.slim @@ -39,6 +39,7 @@ = link_to "Sign in with Launchpad", user_launchpad_omniauth_authorize_path = link_to "Sign in with StackExchange", user_stackexchange_omniauth_authorize_path += link_to "Sign in with Google", user_google_oauth2_omniauth_authorize_path / div / = link_to image_tag('/images/sso/google.png'), user_google_oauth2_omniauth_authorize_path diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index ced94cb..cff1839 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -274,10 +274,8 @@ Devise.setup do |config| # config.omniauth_path_prefix = '/my_engine/users/auth' # config.omniauth :launchpad, 'debshots' - # config.omniauth :google_oauth2, '398593918966-0fpmit0pb6ptio1ndsie5dfcnqhei63l.apps.googleusercontent.com', 'uRQq_dLs1kx7l5sYIyEyPVRc', :client_options => {:ssl => {:ca_path => '/etc/ssl/certs'}} - # - # # config.omniauth :launchpad, 'Debian Screenshots', :client_options => {:ssl => {:ca_path => '/etc/ssl/certs'}} - # + config.omniauth :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"], :client_options => {:ssl => {:ca_path => '/etc/ssl/certs'}} + # config.omniauth :amazon, 'amzn1.application-oa2-client.78d832919fc24456b0636762cf18efd9', '8c495d0940ca4100313c03e93b8b4240eef256d2fdbe672718506f29f5a20f61', :client_options => {:ssl => {:ca_path => '/etc/ssl/certs'}} config.omniauth :open_id, name: :stackexchange, identifier: 'https://openid.stackexchange.com', :client_options => {:ssl => {:ca_path => '/etc/ssl/certs'}}