monban-generators 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2a6704fdf1d9d2cdfdca7e03be4cd00934738f62
4
- data.tar.gz: 3c0d14b7ab93988a3a32d2cd2285cf7fa370ec34
3
+ metadata.gz: e7e2fddf7c46a28fce85351b6eb20bc560854c4f
4
+ data.tar.gz: 11dcfbc6a8e3a4d9bd6b09b50a8686cb4982dcb0
5
5
  SHA512:
6
- metadata.gz: e02412f9ee4bdc5caccf3101f11902582f29d996335b22daad851b3fce9e45dcd308458fc4e6768cd8b6e1811498112c6bea387d8e1d957b763f84e9c4af4ad0
7
- data.tar.gz: 7b48302447c59d621acc3b9633c9f30c88062cb26d1ea6224f1cf89086caee08e8dfe43e2edc828aae36e5c33833c3fec06fcd39f300ce488878281d11c90879
6
+ metadata.gz: ac2e74a5cad1a8e5aa60f0d5aa2168ad1a59003211f141b1b2b0d169642dae89c6776b6ee8ee7b4feda95d71e9d434da88a9fd3b4e80b057eb4ed35f5e36a6eb
7
+ data.tar.gz: f16f73cb18538e97346139d28e079940822e0ce0b9c3e72adf425d05b592cdf1120dac083a7160f9848f520e8f5d2322954ed581ac40152b58846ff2c94c027d
data/NEWS.rdoc CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.0.6
2
+ * Add Google Oauth2
3
+
1
4
  ## 0.0.5
2
5
  * Add `raise: false` to `before_action`s for Rails 5 compatibility
3
6
 
@@ -0,0 +1,43 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module Monban
4
+ module Generators
5
+ class GoogleOauth2Generator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+ source_root File.expand_path("../../templates", __FILE__)
8
+
9
+ def add_gems
10
+ gem "omniauth-google-oauth2"
11
+ end
12
+
13
+ def add_services
14
+ template 'app/services/external_authentication.rb', 'app/services/external_authentication.rb'
15
+ end
16
+
17
+ def add_initializer
18
+ template 'config/initializers/omniauth.rb', 'config/initializers/omniauth.rb'
19
+ end
20
+
21
+ def add_routes
22
+ route(%{get "auth/:provider/callback" => "external_credentials#create"})
23
+ end
24
+
25
+ def add_controllers
26
+ template 'app/controllers/external_credentials_controller.rb', 'app/controllers/external_credentials_controller.rb'
27
+ end
28
+
29
+ def self.next_migration_number(dir)
30
+ ActiveRecord::Generators::Base.next_migration_number(dir)
31
+ end
32
+
33
+ def add_model
34
+ template 'app/models/external_credential.rb', 'app/models/external_credential.rb'
35
+ migration_template "db/migrate/create_external_credentials.rb", "db/migrate/create_external_credentials.rb"
36
+ end
37
+
38
+ def display_readme
39
+ readme 'google_oauth2_readme'
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,13 @@
1
+ class ExternalCredentialsController < ApplicationController
2
+ def create
3
+ credential = ExternalAuthentication.new(auth_hash).perform
4
+ sign_in credential.user
5
+ redirect_to root_path
6
+ end
7
+
8
+ private
9
+
10
+ def auth_hash
11
+ request.env["omniauth.auth"]
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ class ExternalCredential < ApplicationRecord
2
+ belongs_to :user
3
+ end
@@ -0,0 +1,55 @@
1
+ class ExternalAuthentication
2
+ def initialize(auth_hash)
3
+ @auth_hash = auth_hash
4
+ end
5
+
6
+ def perform
7
+ if scope.exists?
8
+ scope.first
9
+ else
10
+ transaction { scope.create(user: user) }
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :auth_hash
17
+
18
+ def user
19
+ User.find_by(email: email) || create_user
20
+ end
21
+
22
+ def create_user
23
+ Monban::Services::SignUp.new(user_params).perform
24
+ end
25
+
26
+ def transaction(&block)
27
+ ExternalCredential.transaction(&block)
28
+ end
29
+
30
+ def user_params
31
+ {
32
+ email: email,
33
+ password: random_password
34
+ }
35
+ end
36
+
37
+ def email
38
+ auth_hash["info"]["email"]
39
+ end
40
+
41
+ def random_password
42
+ SecureRandom.hex
43
+ end
44
+
45
+ def scope
46
+ ExternalCredential.where(credential_params)
47
+ end
48
+
49
+ def credential_params
50
+ {
51
+ provider: auth_hash["provider"],
52
+ uid: auth_hash["uid"]
53
+ }
54
+ end
55
+ end
@@ -0,0 +1,4 @@
1
+ Rails.application.config.middleware.use OmniAuth::Builder do
2
+ provider :developer unless Rails.env.production?
3
+ provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET']
4
+ end
@@ -0,0 +1,11 @@
1
+ class CreateExternalCredentials < ActiveRecord::Migration[5.0]
2
+ def change
3
+ create_table :external_credentials do |t|
4
+ t.string :uid
5
+ t.string :provider
6
+ t.references :user, foreign_key: true
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ Final Steps
2
+ Set the following environment variables:
3
+ - GOOGLE_CLIENT_ID
4
+ - GOOGLE_CLIENT_SECRET
5
+
6
+ These can be obtained from: https://console.developers.google.com
7
+
8
+ Run
9
+
10
+ rake db:migrate
11
+
12
+ Add the following to the correct sign in/up templates:
13
+
14
+
15
+ <%= link_to "Sign in with Google+", "/auth/google_oauth2" %>
16
+
17
+
@@ -1,5 +1,5 @@
1
1
  module Monban
2
2
  module Generators
3
- VERSION = "0.0.5"
3
+ VERSION = "0.0.6"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monban-generators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - halogenandtoast
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-28 00:00:00.000000000 Z
11
+ date: 2017-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,23 +66,30 @@ files:
66
66
  - NEWS.rdoc
67
67
  - README.md
68
68
  - Rakefile
69
+ - lib/generators/monban/google_oauth2/google_oauth2_generator.rb
69
70
  - lib/generators/monban/password_reset/password_reset_generator.rb
70
71
  - lib/generators/monban/scaffold/scaffold_generator.rb
72
+ - lib/generators/monban/templates/app/controllers/external_credentials_controller.rb
71
73
  - lib/generators/monban/templates/app/controllers/password_resets_controller.rb
72
74
  - lib/generators/monban/templates/app/controllers/sessions_controller.rb
73
75
  - lib/generators/monban/templates/app/controllers/users_controller.rb
74
76
  - lib/generators/monban/templates/app/mailers/password_reset_mailer.rb
77
+ - lib/generators/monban/templates/app/models/external_credential.rb
75
78
  - lib/generators/monban/templates/app/models/password_reset.rb
76
79
  - lib/generators/monban/templates/app/models/user.rb
80
+ - lib/generators/monban/templates/app/services/external_authentication.rb
77
81
  - lib/generators/monban/templates/app/views/password_reset_mailer/change_password.html.erb
78
82
  - lib/generators/monban/templates/app/views/password_resets/create.html.erb
79
83
  - lib/generators/monban/templates/app/views/password_resets/edit.html.erb
80
84
  - lib/generators/monban/templates/app/views/password_resets/new.html.erb
81
85
  - lib/generators/monban/templates/app/views/sessions/new.html.erb
82
86
  - lib/generators/monban/templates/app/views/users/new.html.erb
87
+ - lib/generators/monban/templates/config/initializers/omniauth.rb
83
88
  - lib/generators/monban/templates/config/locales/monban.en.yml
89
+ - lib/generators/monban/templates/db/migrate/create_external_credentials.rb
84
90
  - lib/generators/monban/templates/db/migrate/create_password_resets.rb
85
91
  - lib/generators/monban/templates/db/migrate/create_users.rb
92
+ - lib/generators/monban/templates/google_oauth2_readme
86
93
  - lib/generators/monban/templates/password_reset_readme
87
94
  - lib/generators/monban/templates/scaffold_readme
88
95
  - lib/monban/generators.rb
@@ -108,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
115
  version: '0'
109
116
  requirements: []
110
117
  rubyforge_project:
111
- rubygems_version: 2.4.5.1
118
+ rubygems_version: 2.6.8
112
119
  signing_key:
113
120
  specification_version: 4
114
121
  summary: Rails generators for the monban authentication library