Class: Hoboken::AddOns::OmniAuth

Inherits:
Group
  • Object
show all
Defined in:
lib/hoboken/add_ons/omniauth.rb

Overview

OmniAuth authentication (allows you to select a provider).

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Group

#classic?, #modular?, #rspec?, #rubocop?, #sequel?, source_root

Methods included from Hoboken::Actions

#gem, #indent

Instance Attribute Details

#providerObject (readonly)

Returns the value of attribute provider.



8
9
10
# File 'lib/hoboken/add_ons/omniauth.rb', line 8

def provider
  @provider
end

Instance Method Details

#add_gemObject



10
11
12
13
14
# File 'lib/hoboken/add_ons/omniauth.rb', line 10

def add_gem
  @provider = ask('Specify a provider (i.e. twitter, facebook. etc.): ').downcase
  provider_version = ask('Specify provider version: ')
  gem gem_name, version: provider_version
end

#add_routesObject

rubocop:disable Metrics/MethodLength



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hoboken/add_ons/omniauth.rb', line 35

def add_routes
  routes = <<~CODE

    get '/login' do
      '<a href="/auth/#{provider}">Login</a>'
    end

    get '/auth/:provider/callback' do
      # TODO: Insert real authentication logic...
      json request.env['omniauth.auth']
    end

    get '/auth/failure' do
      # TODO: Insert real error handling logic...
      halt 401, params[:message]
    end
  CODE

  if classic?
    append_file('app.rb', routes)
  else
    inject_into_class('app.rb', 'App') { indent(routes, 4) }
  end
end

#add_specsObject

rubocop:disable Metrics/MethodLength



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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/hoboken/add_ons/omniauth.rb', line 102

def add_specs
  return unless rspec?

  append_file('spec/app_spec.rb') do
    <<~CODE

      # rubocop:disable Metrics/BlockLength
      RSpec.describe 'omniauth', rack: true do
        before { OmniAuth.config.test_mode = true }

        describe 'GET /login' do
          before { get '/login' }

          it { expect(last_response).to have_http_status(:ok) }
          it { expect(last_response).to have_content_type(:html) }

          it 'renders a template with a login link' do
            #{provider}_link = '<a href="/auth/#{provider}">Login</a>'
            expect(last_response.body).to include(#{provider}_link)
          end
        end

        describe 'GET /auth/#{provider}/callback' do
          let(:auth_hash) do
            {
              provider: '#{provider}',
              uid: '123545',
              info: {
                name: 'John Doe'
              }
            }
          end

          before do
            OmniAuth.config.mock_auth[:#{provider}] = auth_hash
            get '/auth/#{provider}/callback'
          end

          it { expect(last_response).to have_http_status(:ok) }
          it { expect(last_response).to have_content_type(:json) }

          it 'renders the auth hash result' do
            expect(last_response.body).to eq(JSON.generate(auth_hash))
          end
        end

        describe 'GET /auth/failure' do
          before do
            OmniAuth.config.mock_auth[:#{provider}] = :invalid_credentials
            get '/auth/failure'
          end

          it { expect(last_response).to have_http_status(:not_authorized) }
        end
      end
      # rubocop:enable Metrics/BlockLength
    CODE
  end
end

#add_testsObject

rubocop:disable Metrics/MethodLength



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
# File 'lib/hoboken/add_ons/omniauth.rb', line 62

def add_tests
  return if rspec?

  inject_into_class('test/unit/app_test.rb', 'AppTest') do
    <<-CODE
  setup do
    OmniAuth.config.test_mode = true
  end

  test 'GET /login' do
    get '/login'
    assert_equal('<a href="/auth/#{provider}">Login</a>', last_response.body)
  end

  test 'GET /auth/#{provider}/callback' do
    auth_hash = {
provider: '#{provider}',
uid: '123545',
info: {
  name: 'John Doe'
}
    }

    OmniAuth.config.mock_auth[:#{provider}] = auth_hash
    get '/auth/#{provider}/callback'
    assert_equal(MultiJson.encode(auth_hash), last_response.body)
  end

  test 'GET /auth/failure' do
    OmniAuth.config.mock_auth[:#{provider}] = :invalid_credentials
    get '/auth/failure'
    assert_response :not_authorized
  end

    CODE
  end
end

#remindersObject

rubocop:enable Metrics/MethodLength



163
164
165
# File 'lib/hoboken/add_ons/omniauth.rb', line 163

def reminders
  say "\nGemfile updated... don't forget to 'bundle install'"
end

#setup_middlewareObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hoboken/add_ons/omniauth.rb', line 16

def setup_middleware
  insert_into_file('app.rb', after: %r{require 'sinatra('|/base')}) do
    "\nrequire '#{gem_name}'\nrequire 'sinatra/json'\n"
  end

  snippet = <<~CODE
    use OmniAuth::Builder do
      provider :#{provider}, ENV['#{provider.upcase}_KEY'], ENV['#{provider.upcase}_SECRET']
    end
  CODE

  indentation = classic? ? 2 : 6
  location = /use Rack::Session::Cookie.+\n/
  insert_into_file('config/environment.rb', after: location) do
    "\n#{indent(snippet, indentation)}\n"
  end
end