diff --git a/Gemfile b/Gemfile
index 4efc519ba1b78878bd2fe3cf81137c993c0c6b10..d8652ae1b90387fdbd1bdcbd2ef2a0f68499ff7b 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 c81e0e39a18ea49414a846a7f8679c4e6aa0165a..3b8b71ababaf2897cc7d7c17217f82a3c5737e7e 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 0000000000000000000000000000000000000000..1fe7a115afc45d76225421abc40db8aa0ed6297c
--- /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 5a39baa3f20dbedf9980948592614395c08d4085..d7b17ac3f17c071e6d0fc14279da7a52bbb94a9a 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 f451b16d517a8c01c4b017197d2693d43d816b57..009f2c77227ea85424e59e116b66d80666ad9c09 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