Welcome to mirror list, hosted at ThFree Co, Russian Federation.

config.ru « examples « omniauth-google-oauth2 « gems « vendor - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ee17929094c59971b5ae3dfc7f1a77911c594177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true

# Sample app for Google OAuth2 Strategy
# Make sure to setup the ENV variables GOOGLE_KEY and GOOGLE_SECRET
# Run with "bundle exec rackup"

require 'rubygems'
require 'bundler'
require 'sinatra'
require 'omniauth'
require 'omniauth-google-oauth2'

# Do not use for production code.
# This is only to make setup easier when running through the sample.
#
# If you do have issues with certs in production code, this could help:
# http://railsapps.github.io/openssl-certificate-verify-failed.html
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE

# Main example app for omniauth-google-oauth2
class App < Sinatra::Base
  get '/' do
    <<-HTML
    <!DOCTYPE html>
    <html>
      <head>
        <title>Google OAuth2 Example</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script>
          jQuery(function() {
            return $.ajax({
              url: 'https://apis.google.com/js/client:plus.js?onload=gpAsyncInit',
              dataType: 'script',
              cache: true
            });
          });

          window.gpAsyncInit = function() {
            gapi.auth.authorize({
              immediate: true,
              response_type: 'code',
              cookie_policy: 'single_host_origin',
              client_id: '#{ENV['GOOGLE_KEY']}',
              scope: 'email profile'
            }, function(response) {
              return;
            });
            $('.googleplus-login').click(function(e) {
              e.preventDefault();
              gapi.auth.authorize({
                immediate: false,
                response_type: 'code',
                cookie_policy: 'single_host_origin',
                client_id: '#{ENV['GOOGLE_KEY']}',
                scope: 'email profile'
              }, function(response) {
                if (response && !response.error) {
                  // google authentication succeed, now post data to server.
                  jQuery.ajax({type: 'POST', url: "/auth/google_oauth2/callback", data: response,
                    success: function(data) {
                      // Log the data returning from google.
                      console.log(data)
                    }
                  });
                } else {
                  // google authentication failed.
                  console.log("FAILED")
                }
              });
            });
          };
        </script>
      </head>
      <body>
      <ul>
        <li><a href='/auth/google_oauth2'>Sign in with Google</a></li>
        <li><a href='#' class="googleplus-login">Sign in with Google via AJAX</a></li>
      </ul>
      </body>
    </html>
    HTML
  end

  post '/auth/:provider/callback' do
    content_type 'text/plain'
    begin
      request.env['omniauth.auth'].to_hash.inspect
    rescue StandardError
      'No Data'
    end
  end

  get '/auth/:provider/callback' do
    content_type 'text/plain'
    begin
      request.env['omniauth.auth'].to_hash.inspect
    rescue StandardError
      'No Data'
    end
  end

  get '/auth/failure' do
    content_type 'text/plain'
    begin
      request.env['omniauth.auth'].to_hash.inspect
    rescue StandardError
      'No Data'
    end
  end
end

use Rack::Session::Cookie, secret: ENV['RACK_COOKIE_SECRET']

use OmniAuth::Builder do
  # For additional provider examples please look at 'omni_auth.rb'
  # The key provider_ignores_state is only for AJAX flows. It is not recommended for normal logins.
  provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], access_type: 'offline', prompt: 'consent', provider_ignores_state: true, scope: 'email,profile,calendar'
end

run App.new