From 2d36aeddc28d0468e58673e684a754851ad2be3b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=9A=D0=BE?=
 =?UTF-8?q?=D0=B2=D0=B0=D0=BB=D0=B5=D0=BD=D0=BE=D0=BA?=
 <dmitry.kovalenok@twinslash.com>
Date: Mon, 29 Oct 2012 13:56:33 +0300
Subject: [PATCH] Updated controller redmine_omniauth_controller. Now possible
 login through google

---
 Gemfile                                       |  7 ++-
 .../redmine_omniauth_controller.rb            | 63 ++++++++++++++++++-
 app/views/settings/_google_settings.html.erb  |  8 +++
 config/routes.rb                              |  3 +-
 init.rb                                       |  3 +
 5 files changed, 79 insertions(+), 5 deletions(-)
 create mode 100644 app/views/settings/_google_settings.html.erb

diff --git a/Gemfile b/Gemfile
index 4efc519..d8652ae 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1 +1,6 @@
-gem 'omniauth-google'
\ No newline at end of file
+gem 'oauth2'
+gem 'json'
+
+group :development, :test do
+  gem 'pry'
+end
\ No newline at end of file
diff --git a/app/controllers/redmine_omniauth_controller.rb b/app/controllers/redmine_omniauth_controller.rb
index c81e0e3..3b8b71a 100644
--- a/app/controllers/redmine_omniauth_controller.rb
+++ b/app/controllers/redmine_omniauth_controller.rb
@@ -1,7 +1,64 @@
 require 'account_controller'
+require 'json'
 
-class RedmineOmniauthController < ApplicationController
+class RedmineOmniauthController < AccountController
   def omniauth_google
-    AccountController.new.send(:open_id_authenticate, params[:openid_url])
+    redirect_to oauth_client.auth_code.authorize_url(redirect_uri: oauth_google_callback_url, scope: scopes)
   end
-end
+
+  def oauth_google_callback
+    token = oauth_client.auth_code.get_token(params[:code], redirect_uri: oauth_google_callback_url)
+    result = token.get('https://www.googleapis.com/oauth2/v1/userinfo')
+    info = JSON.parse(result.body)
+    if info["verified_email"]
+      user = User.find_or_initialize_by_mail(info["email"])
+      if user.new_record?
+        # Self-registration off
+        redirect_to(home_url) && return unless Setting.self_registration?
+        # Create on the fly
+        user.login = info["email"].match(/(.+)@/)[1] unless info["email"].nil?
+        user.mail = info["email"] unless info["email"].nil?
+        user.firstname, user.lastname = info["name"].split(' ') unless info['name'].nil?
+        user.random_password
+        user.register
+
+        case Setting.self_registration
+        when '1'
+          register_by_email_activation(user) do
+            onthefly_creation_failed(user)
+          end
+        when '3'
+          register_automatically(user) do
+            onthefly_creation_failed(user)
+          end
+        else
+          register_manually_by_administrator(user) do
+            onthefly_creation_failed(user)
+          end
+        end
+      else
+        # Existing record
+        if user.active?
+          successful_authentication(user)
+        else
+          account_pending
+        end
+      end
+    end
+  end
+
+  def oauth_client
+    @client ||= OAuth2::Client.new(settings[:client_id], settings[:client_secret],
+      site: 'https://accounts.google.com',
+      authorize_url: '/o/oauth2/auth',
+      token_url: '/o/oauth2/token')
+  end
+
+  def settings
+    @settings ||= Setting.plugin_redmine_omniauth_google
+  end
+
+  def scopes
+    'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'
+  end
+end
\ No newline at end of file
diff --git a/app/views/settings/_google_settings.html.erb b/app/views/settings/_google_settings.html.erb
new file mode 100644
index 0000000..1fe7a11
--- /dev/null
+++ b/app/views/settings/_google_settings.html.erb
@@ -0,0 +1,8 @@
+<p>
+  <label>Client ID:</label>
+  <%= text_field_tag 'settings[client_id]', @settings[:client_id] %>
+</p>
+<p>
+  <label>Client Secret:</label>
+  <%= text_field_tag 'settings[client_secret]', @settings[:client_secret] %>
+</p>
\ No newline at end of file
diff --git a/config/routes.rb b/config/routes.rb
index 5a39baa..d7b17ac 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1 +1,2 @@
-get 'omniauth_google', to: 'redmine_omniauth#omniauth_google', as: :omniauth_google
\ No newline at end of file
+get 'omniauth_google', to: 'redmine_omniauth#omniauth_google', as: :omniauth_google
+get 'oauth_google_callback', to: 'redmine_omniauth#oauth_google_callback'
\ No newline at end of file
diff --git a/init.rb b/init.rb
index f451b16..009f2c7 100644
--- a/init.rb
+++ b/init.rb
@@ -8,4 +8,7 @@ Redmine::Plugin.register :redmine_omniauth_google do
   version '0.0.1'
   url 'http://gitlab.tsdv.net/redmine_omniauth_google'
   author_url 'https://tsdv.net/redmine/users/105'
+  settings default: {
+    client_id: '214698823792.apps.googleusercontent.com',
+    client_secret: 'M0HJPMypEgrDAKKHGiP6Y2R-' }, partial: 'settings/google_settings'
 end
-- 
GitLab