nyauth 0.2.1 → 0.2.2

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: 8f7b92e040021887635fc5cd0c21427d7afc72a3
4
- data.tar.gz: 489d0039f87c0481e739cbe48d9106a74091ae5f
3
+ metadata.gz: 81cfbcefaf5c3f92dbd8937cd999481e3863aed6
4
+ data.tar.gz: ddad74740d111f1e0adbdd4689020ada40e8d4b0
5
5
  SHA512:
6
- metadata.gz: bb13e17f77ddab00ead54b2f455662c09f21bdc5980a945358e2a61833f7ba1416bb0ecd8feb6053c0916b3fcc38966a02c44229c0d42aac64e634382bfacd3c
7
- data.tar.gz: 91814e70d9698c204632932ecdbc142dfd16c6dbde8caa1187307d0b54bca9f865446404eb60607f998972781307375860fedbf70d07dcb40e4c2d6b085183a8
6
+ metadata.gz: 68e8227c95cd08c4fa2ec26ffd6b59d18aeaa23e5b1abde5241205c8ea4edce53375cb8da871c58a30a034d71e52184ffd743406c5826d1464d58a4cbadd4a56
7
+ data.tar.gz: 3c3651402abd49496a1bbbac2398193b6d6f5decdcc070f3c3868fa98103e61013c1f3ec5f901fa51688bd8f389b4bfb50a4d517106dd71aa4dc9a36f77fcec2
data/README.md CHANGED
@@ -44,6 +44,13 @@ class User < ActiveRecord::Base
44
44
  end
45
45
  ```
46
46
 
47
+ ```ruby
48
+ class Admin < ActiveRecord::Base
49
+ include Nyauth::Authenticatable
50
+ #include Nyauth::Confirmable
51
+ end
52
+ ```
53
+
47
54
  ### config/routes.rb
48
55
 
49
56
  ```ruby
@@ -98,6 +105,47 @@ new_session_path_for(:admin) # /admin/session/new
98
105
  rails g nyauth:install
99
106
  ```
100
107
 
108
+ ```
109
+ rails g nyauth <MODEL>
110
+ rails g nyauth User
111
+ rails g nyauth Admin
112
+ ```
113
+
101
114
  ```
102
115
  rails g nyauth:views
103
116
  ```
117
+
118
+ ### Customize
119
+
120
+ Change redirect path after action.
121
+
122
+ Please set NYAUTH_ENCRYPTION_SECRET ENV like following.
123
+
124
+ ```
125
+ export NYAUTH_ENCRYPTION_SECRET=`bundle exec rake secret`
126
+ ```
127
+
128
+ Or edit `config/initializers/nyauth.rb`
129
+
130
+ ```
131
+ nyauth.configure do |config|
132
+ config.encryption_secret = ENV['NYAUTH_ENCRYPTION_SECRET'] || ENV['SECRET_KEY_BASE']
133
+ config.redirect_path do |urls|
134
+ # config.redirect_path_after_sign_in = -> (client_name) {
135
+ # if client_name == :admin
136
+ # urls.admin_secret_notes_path
137
+ # else
138
+ # urls.root_path
139
+ # end
140
+ #}
141
+ # config.redirect_path_after_sign_in = -> (client_name) {}
142
+ # config.redirect_path_after_sign_out = -> (client_name) {}
143
+ # config.redirect_path_after_registration = -> (client_name) {}
144
+ # config.redirect_path_after_create_request_confirmation = -> (client_name) {}
145
+ # config.redirect_path_after_update_confirmation = -> (client_name) {}
146
+ # config.redirect_path_after_reset_password_request = -> (client_name) {}
147
+ # config.redirect_path_after_reset_password = -> (client_name) {}
148
+ # config.redirect_path_after_update_password = -> (client_name) {}
149
+ end
150
+ end
151
+ ```
@@ -12,7 +12,7 @@ module Nyauth
12
12
 
13
13
  def create
14
14
  @client.request_confirmation
15
- respond_with(@client, location: Nyauth.configuration.redirect_path_after_create_request_confirmation || main_app.root_path)
15
+ respond_with(@client, location: Nyauth.configuration.redirect_path_after_create_request_confirmation.call(client_name) || main_app.root_path)
16
16
  end
17
17
 
18
18
  private
@@ -9,7 +9,7 @@ module Nyauth
9
9
 
10
10
  def update
11
11
  @client.confirm
12
- respond_with(@client, location: Nyauth.configuration.redirect_path_after_update_confirmation || main_app.root_path)
12
+ respond_with(@client, location: Nyauth.configuration.redirect_path_after_update_confirmation.call(client_name) || main_app.root_path)
13
13
  end
14
14
 
15
15
  private
@@ -11,7 +11,7 @@ module Nyauth
11
11
  def update
12
12
  @client.attributes = client_params
13
13
  @client.save(context: :update_password)
14
- respond_with(@client, location: Nyauth.configuration.redirect_path_after_update_password || main_app.root_path)
14
+ respond_with(@client, location: Nyauth.configuration.redirect_path_after_update_password.call(client_name) || main_app.root_path)
15
15
  end
16
16
 
17
17
  private
@@ -11,7 +11,7 @@ module Nyauth
11
11
 
12
12
  def create
13
13
  sign_in(@client) if @client.save
14
- respond_with(@client, location: Nyauth.configuration.redirect_path_after_registration || main_app.root_path)
14
+ respond_with(@client, location: Nyauth.configuration.redirect_path_after_registration.call(client_name) || main_app.root_path)
15
15
  end
16
16
 
17
17
  private
@@ -12,7 +12,7 @@ module Nyauth
12
12
 
13
13
  def create
14
14
  @client.request_reset_password
15
- respond_with(@client, location: Nyauth.configuration.redirect_path_after_reset_password_request || main_app.root_path)
15
+ respond_with(@client, location: Nyauth.configuration.redirect_path_after_reset_password_request.call(client_name) || main_app.root_path)
16
16
  end
17
17
 
18
18
  private
@@ -11,7 +11,7 @@ module Nyauth
11
11
 
12
12
  def update
13
13
  @client.reset_password(client_params)
14
- respond_with(@client, location: Nyauth.configuration.redirect_path_after_reset_password || new_session_path_for(client_name))
14
+ respond_with(@client, location: Nyauth.configuration.redirect_path_after_reset_password.call(client_name) || new_session_path_for(client_name))
15
15
  end
16
16
 
17
17
  private
@@ -11,12 +11,12 @@ module Nyauth
11
11
 
12
12
  def create
13
13
  sign_in(@session_service.client) if @session_service.save(as: client_name)
14
- respond_with @session_service, location: Nyauth.configuration.redirect_path_after_sign_in || main_app.root_path
14
+ respond_with @session_service, location: Nyauth.configuration.redirect_path_after_sign_in.call(client_name) || main_app.root_path
15
15
  end
16
16
 
17
17
  def destroy
18
18
  sign_out
19
- respond_with @session_service, location: Nyauth.configuration.redirect_path_after_sign_out || new_session_path
19
+ respond_with @session_service, location: Nyauth.configuration.redirect_path_after_sign_out.call(client_name) || new_session_path
20
20
  end
21
21
 
22
22
  private
@@ -14,7 +14,8 @@ module Nyauth
14
14
  def detect_url_helper(feature, client_name, *args)
15
15
  @__methods ||= Nyauth::Engine.routes.url_helpers.instance_methods + Rails.application.routes.url_helpers.instance_methods
16
16
  @__candidates ||= @__methods.reject{|m| m =~ /\A(:?rails_|_)/}.map(&:to_s)
17
- if client_name.to_sym == :user
17
+ if respond_to?(:nyauth) && client_name.to_sym == :user
18
+ # mounted as nyauth
18
19
  detect_url_helper_for_nyauth(feature, *args)
19
20
  else
20
21
  detect_url_helper_for_app(feature, client_name, *args)
@@ -0,0 +1,56 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ class Nyauth::NyauthGenerator < ActiveRecord::Generators::Base
4
+ source_root File.expand_path("../templates", __FILE__)
5
+
6
+ def copy_nyauth_migration
7
+ migration_template "migration.rb", "db/migrate/add_nyauth_to_#{table_name}.rb"
8
+ end
9
+
10
+ def inject_devise_content
11
+ content = model_contents
12
+
13
+ class_path = if namespaced?
14
+ class_name.to_s.split("::")
15
+ else
16
+ [class_name]
17
+ end
18
+
19
+ indent_depth = class_path.size - 1
20
+ content = content.split("\n").map { |line| " " * indent_depth + line } .join("\n") << "\n"
21
+
22
+ inject_into_class(model_path, class_path.last, content) if model_exists?
23
+ end
24
+
25
+ def migration_data
26
+ <<RUBY
27
+ # Authenticatable
28
+ t.string :email, null: false
29
+ t.string :password_digest, null: false
30
+ t.string :password_salt, null: false
31
+ t.string :reset_password_key
32
+ t.datetime :reset_password_key_expired_at
33
+ # Confirmable
34
+ #t.datetime :confirmed_at
35
+ #t.string :confirmation_key
36
+ #t.datetime :confirmation_key_expired_at
37
+ RUBY
38
+ end
39
+
40
+ def model_contents
41
+ <<RUBY
42
+ include Nyauth::Authenticatable
43
+ #include Nyauth::Confirmable
44
+ RUBY
45
+ end
46
+
47
+ private
48
+
49
+ def model_exists?
50
+ File.exists?(File.join(destination_root, model_path))
51
+ end
52
+
53
+ def model_path
54
+ @model_path ||= File.join("app", "models", "#{file_path}.rb")
55
+ end
56
+ end
@@ -0,0 +1,16 @@
1
+ class AddNyauthTo<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def self.up
3
+ change_table(:<%= table_name %>) do |t|
4
+ <%= migration_data -%>
5
+ end
6
+
7
+ add_index :<%= table_name %>, :email, unique: true
8
+ add_index :<%= table_name %>, :reset_password_key, unique: true
9
+ # Confirmable
10
+ #add_index :<%= table_name %>, :confirmation_key, unique: true
11
+ end
12
+
13
+ def self.down
14
+ raise ActiveRecord::IrreversibleMigration
15
+ end
16
+ end
@@ -1,7 +1,20 @@
1
1
  Nyauth.configure do |config|
2
+ config.encryption_secret = ENV['NYAUTH_ENCRYPTION_SECRET']
2
3
  config.redirect_path do |urls|
3
- # config.redirect_path_after_sign_in = urls.admin_secret_notes_path
4
- # config.redirect_path_after_sign_out = urls.root_path
5
- # config.redirect_path_after_registration = urls.root_path
4
+ # config.redirect_path_after_sign_in = -> (client_name) {
5
+ # if client_name == :admin
6
+ # urls.admin_secret_notes_path
7
+ # else
8
+ # urls.root_path
9
+ # end
10
+ #}
11
+ # config.redirect_path_after_sign_in = -> (client_name) {}
12
+ # config.redirect_path_after_sign_out = -> (client_name) {}
13
+ # config.redirect_path_after_registration = -> (client_name) {}
14
+ # config.redirect_path_after_create_request_confirmation = -> (client_name) {}
15
+ # config.redirect_path_after_update_confirmation = -> (client_name) {}
16
+ # config.redirect_path_after_reset_password_request = -> (client_name) {}
17
+ # config.redirect_path_after_reset_password = -> (client_name) {}
18
+ # config.redirect_path_after_update_password = -> (client_name) {}
6
19
  end
7
20
  end
@@ -10,20 +10,22 @@ module Nyauth
10
10
  :redirect_path_after_reset_password,
11
11
  :redirect_path_after_update_password,
12
12
  :password_minium,
13
- :password_digest_stretches
13
+ :password_digest_stretches,
14
+ :encryption_secret
14
15
 
15
16
 
16
17
  def initialize
17
- @redirect_path_after_sign_in = nil
18
- @redirect_path_after_sign_out = nil
19
- @redirect_path_after_registration = nil
20
- @redirect_path_after_create_request_confirmation = nil
21
- @redirect_path_after_update_confirmation = nil
22
- @redirect_path_after_reset_password_request = nil
23
- @redirect_path_after_reset_password = nil
24
- @redirect_path_after_update_password = nil
18
+ @redirect_path_after_sign_in = Proc.new {}
19
+ @redirect_path_after_sign_out = Proc.new {}
20
+ @redirect_path_after_registration = Proc.new {}
21
+ @redirect_path_after_create_request_confirmation = Proc.new {}
22
+ @redirect_path_after_update_confirmation = Proc.new {}
23
+ @redirect_path_after_reset_password_request = Proc.new {}
24
+ @redirect_path_after_reset_password = Proc.new {}
25
+ @redirect_path_after_update_password = Proc.new {}
25
26
  @password_minium = 8
26
27
  @password_digest_stretches = 1000
28
+ @encryption_secret = ENV['NYAUTH_ENCRYPTION_SECRET']
27
29
  @redirect_path_block = Proc.new {}
28
30
  end
29
31
 
@@ -3,14 +3,13 @@ module Nyauth
3
3
  include Singleton
4
4
 
5
5
  cattr_writer :secret, :cipher, :digest
6
- self.secret = ENV['ENCRYPTION_SECRET'] || '46d9a00e79b6bbf1c64a829c5640f5798c2e7c5066493f6d7e56ba800fd17d62b9e2bdf9102ae915f22eb40f5d6c83d6b3266baa509de7fc330c88bd4947bf56'
7
6
  self.cipher = 'aes-256-cbc'
8
7
  self.digest = 'SHA512'
9
8
 
10
9
  attr_reader :encryptor
11
10
 
12
11
  def initialize
13
- @encryptor = ::ActiveSupport::MessageEncryptor.new(@@secret, cipher: @@cipher, digest: @@digest)
12
+ @encryptor = ::ActiveSupport::MessageEncryptor.new(Nyauth.configuration.encryption_secret, cipher: @@cipher, digest: @@digest)
14
13
  end
15
14
 
16
15
  class << self
@@ -1,3 +1,3 @@
1
1
  module Nyauth
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -1,5 +1,4 @@
1
1
  class User < ActiveRecord::Base
2
2
  include Nyauth::Authenticatable
3
3
  include Nyauth::Confirmable
4
- include Nyauth::ResetPasswordAbility
5
4
  end
Binary file
Binary file