File: config.ru

package info (click to toggle)
ruby-omniauth-google-oauth2 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 236 kB
  • sloc: ruby: 1,045; makefile: 3
file content (145 lines) | stat: -rw-r--r-- 3,935 bytes parent folder | download
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# 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
  configure do
    set :sessions, true
    set :inline_templates, true
  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'
  end

  get '/' do
    <<-HTML
    <!DOCTYPE html>
    <html>
      <head>
        <title>Google OAuth2 Example</title>
      </head>

      <body>
        <ul>
          <li>
            <form method="post" action="/auth/google_oauth2">
              <input type="hidden" name="authenticity_token" value="#{request.env['rack.session']['csrf']}">
              <button type="submit">Login with Google</button>
            </form>
          </li>

          <li>
            <a href="#" class="googleplus-login">Sign in with Google via AJAX</a>
          </li>
        </ul>

        <script>
          const a = document.querySelector('.googleplus-login');

          const handleGoogleOauthSignIn = () => {
            const oauth2Endpoint = 'https://accounts.google.com/o/oauth2/v2/auth';

            const params = new URLSearchParams({
              client_id: '#{ENV['GOOGLE_KEY']}',
              prompt: 'select_account',
              redirect_uri: 'http://localhost:3000/callback',
              response_type: 'code',
              scope: 'email openid profile',
            });

            const url = `${oauth2Endpoint}?${params.toString()}`;
            window.location.href = url;
          }

          a.addEventListener('click', event => {
            event.preventDefault();
            handleGoogleOauthSignIn();
          });
        </script>
      </body>
    </html>
    HTML
  end

  get '/callback' do
    <<-HTML
    <!DOCTYPE html>
    <html>
      <head>
        <title>Google OAuth2 Example</title>
      </head>

      <body>
        <p>Redirected</p>

        <script>
          const handleGoogleOauthCallback = async () => {
            const params = new URL(document.location.toString()).searchParams;
            const code = params.get('code');

            const response = fetch('http://localhost:3000/auth/google_oauth2/callback', {
              body: JSON.stringify({ code, redirect_uri: 'http://localhost:3000/callback' }),
              headers: {
                'Content-type': 'application/json',
              },
              method: 'POST',
            });
          }

          handleGoogleOauthCallback();
        </script>
      </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

run App.new