nyauth 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +1 -0
- data/Rakefile +26 -0
- data/app/assets/javascripts/nyauth/application.js +13 -0
- data/app/assets/stylesheets/nyauth/application.css +15 -0
- data/app/controllers/concerns/nyauth/session_concern.rb +71 -0
- data/app/controllers/nyauth/confirmation_requests_controller.rb +28 -0
- data/app/controllers/nyauth/confirmations_controller.rb +19 -0
- data/app/controllers/nyauth/new_password_requests_controller.rb +28 -0
- data/app/controllers/nyauth/new_passwords_controller.rb +26 -0
- data/app/controllers/nyauth/passwords_controller.rb +26 -0
- data/app/controllers/nyauth/registrations_controller.rb +26 -0
- data/app/controllers/nyauth/sessions_controller.rb +31 -0
- data/app/helpers/nyauth/application_helper.rb +4 -0
- data/app/mailers/nyauth/user_mailer.rb +15 -0
- data/app/models/concerns/nyauth/authenticatable.rb +18 -0
- data/app/models/concerns/nyauth/confirmable.rb +34 -0
- data/app/models/concerns/nyauth/new_password_ability.rb +35 -0
- data/app/models/concerns/nyauth/password_digest_ability.rb +40 -0
- data/app/responders/nyauth/app_responder.rb +6 -0
- data/app/responders/nyauth/confirmation_responder.rb +14 -0
- data/app/services/nyauth/confirmation_request_service.rb +15 -0
- data/app/services/nyauth/session_service.rb +21 -0
- data/app/views/layouts/nyauth/mailer.html.erb +1 -0
- data/app/views/layouts/nyauth/mailer.text.erb +1 -0
- data/app/views/nyauth/confirmation_requests/new.html.slim +5 -0
- data/app/views/nyauth/confirmations/edit.html.slim +5 -0
- data/app/views/nyauth/group_requests/edit.html.slim +0 -0
- data/app/views/nyauth/group_requests/new.html.slim +14 -0
- data/app/views/nyauth/groups/show.html.slim +0 -0
- data/app/views/nyauth/layouts/application.html.slim +15 -0
- data/app/views/nyauth/layouts/mailer.html.slim +1 -0
- data/app/views/nyauth/layouts/mailer.text.slim +1 -0
- data/app/views/nyauth/new_password_requests/new.html.slim +5 -0
- data/app/views/nyauth/new_passwords/edit.html.slim +11 -0
- data/app/views/nyauth/passwords/edit.html.slim +11 -0
- data/app/views/nyauth/registrations/new.html.slim +12 -0
- data/app/views/nyauth/sessions/new.html.slim +15 -0
- data/app/views/nyauth/user_mailer/request_confirmation.html.slim +2 -0
- data/app/views/nyauth/user_mailer/request_confirmation.text.erb +3 -0
- data/app/views/nyauth/user_mailer/request_new_password.html.slim +2 -0
- data/app/views/nyauth/user_mailer/request_new_password.text.erb +3 -0
- data/config/application.yml +1 -0
- data/config/locales/en.yml +46 -0
- data/config/routes.rb +10 -0
- data/lib/nyauth/encryptor.rb +26 -0
- data/lib/nyauth/engine.rb +21 -0
- data/lib/nyauth/version.rb +3 -0
- data/lib/nyauth.rb +5 -0
- data/lib/tasks/nyauth_tasks.rake +4 -0
- data/spec/controllers/application_controller_spec.rb +5 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/controllers/application_controller.rb +7 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/models/user.rb +5 -0
- data/spec/dummy/app/views/layouts/application.html.erb +20 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +29 -0
- data/spec/dummy/config/application.rb +32 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +42 -0
- data/spec/dummy/config/environments/production.rb +79 -0
- data/spec/dummy/config/environments/test.rb +46 -0
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20150303135922_create_users.rb +18 -0
- data/spec/dummy/db/schema.rb +32 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +1906 -0
- data/spec/dummy/log/test.log +6719 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/factories/users.rb +21 -0
- data/spec/featrues/nyauth/confirmation_requests_spec.rb +35 -0
- data/spec/featrues/nyauth/new_password_requests_spec.rb +43 -0
- data/spec/featrues/nyauth/passwords_spec.rb +27 -0
- data/spec/featrues/nyauth/registrations_spec.rb +24 -0
- data/spec/featrues/nyauth/sessions_spec.rb +36 -0
- data/spec/models/user_spec.rb +9 -0
- data/spec/rails_helper.rb +41 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/support/controllers/nyauth/session_concern.rb +39 -0
- data/spec/support/macros/controller_macros.rb +3 -0
- data/spec/support/macros/feature_macros.rb +8 -0
- data/spec/support/models/nyauth/authenticatable.rb +36 -0
- data/spec/support/models/nyauth/confirmable.rb +27 -0
- data/spec/support/models/nyauth/new_password_ability.rb +13 -0
- data/spec/support/models/nyauth/password_digest_ability.rb +18 -0
- metadata +280 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 43f12311bbcbae2757a70708c8729fcd541af09c
|
4
|
+
data.tar.gz: 8911316b67a0cabca61b72a25a0a0e25264fb3f5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5bbb2a9bc1b43dd74198adaca2a241f06d1ae5db25b3753dd9694b1dff49278568a253e9c7beec302fb9819c99d7befd17aaed796b1dd7ab4c233c3343b5cdf0
|
7
|
+
data.tar.gz: a0434f772ab0f52520dadb3af244ef556ef3904f9e589ec472b10ba8b7f30b887a67c978123f77f16453917161e72261242302ee35b1eb57a565098c2917044e
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 koshikawa
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Nyauth
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Nyauth'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
Bundler::GemHelper.install_tasks
|
21
|
+
|
22
|
+
require 'rspec/core'
|
23
|
+
require 'rspec/core/rake_task'
|
24
|
+
desc "Run all specs in spec directory (excluding plugin specs)"
|
25
|
+
RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
|
26
|
+
task :default => :spec
|
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module Nyauth
|
2
|
+
module SessionConcern
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do |base|
|
6
|
+
if base.ancestors.include?(ActionController::Base)
|
7
|
+
helper_method :signed_in?, :current_authenticated
|
8
|
+
class_attribute :allow_actions
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
# ex.)
|
13
|
+
# sign_in(client)
|
14
|
+
def sign_in(client)
|
15
|
+
return unless client
|
16
|
+
store_signed_in_session(client)
|
17
|
+
end
|
18
|
+
|
19
|
+
# ex.)
|
20
|
+
# signed_in?(as: :user)
|
21
|
+
def signed_in?(options = {})
|
22
|
+
options.reverse_merge!(as: :user)
|
23
|
+
session[signed_in_session_key].present?
|
24
|
+
end
|
25
|
+
|
26
|
+
# ex.)
|
27
|
+
# sign_out
|
28
|
+
def sign_out
|
29
|
+
reset_session
|
30
|
+
end
|
31
|
+
|
32
|
+
# ex.)
|
33
|
+
# before_action -> { require_authentication! as: :user }, only: :secret_action
|
34
|
+
def require_authentication!(options = {})
|
35
|
+
options.reverse_merge!(as: :user)
|
36
|
+
return if self.class.allow_actions == :all
|
37
|
+
return if self.class.allow_actions.present? && request[:action].to_sym.in?(self.class.allow_actions)
|
38
|
+
head :unauthorized unless signed_in?(options)
|
39
|
+
end
|
40
|
+
|
41
|
+
def current_authenticated
|
42
|
+
return nil unless session_value = session[signed_in_session_key]
|
43
|
+
klass_name, client_id = Nyauth::Encryptor.decrypt(session_value).split(':')
|
44
|
+
klass_name.constantize.find(client_id)
|
45
|
+
end
|
46
|
+
|
47
|
+
def store_signed_in_session(client)
|
48
|
+
session[signed_in_session_key] = signed_in_session_object(client)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def signed_in_session_key
|
54
|
+
"sign_in_session"
|
55
|
+
end
|
56
|
+
|
57
|
+
def signed_in_session_object(client)
|
58
|
+
Nyauth::Encryptor.encrypt("#{client.class.name}:#{client.id}")
|
59
|
+
end
|
60
|
+
|
61
|
+
module ClassMethods
|
62
|
+
def allow_everyone(options = {})
|
63
|
+
if options[:only]
|
64
|
+
self.allow_actions = options[:only] || []
|
65
|
+
else
|
66
|
+
self.allow_actions = :all
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Nyauth
|
2
|
+
class ConfirmationRequestsController < ApplicationController
|
3
|
+
allow_everyone
|
4
|
+
respond_to :html, :json
|
5
|
+
before_action :set_user, only: [:create]
|
6
|
+
after_action :send_mail, only: [:create], if: -> { @user.confirmation_key.present? }
|
7
|
+
|
8
|
+
def new
|
9
|
+
end
|
10
|
+
|
11
|
+
def create
|
12
|
+
@user.request_confirmation
|
13
|
+
respond_with(@user, location: root_path)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def set_user
|
19
|
+
@user = User.find_by!(email: params[:user][:email])
|
20
|
+
rescue ActiveRecord::RecordNotFound
|
21
|
+
render :new
|
22
|
+
end
|
23
|
+
|
24
|
+
def send_mail
|
25
|
+
Nyauth::UserMailer.request_confirmation(@user).deliver_now
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Nyauth
|
2
|
+
class ConfirmationsController < ApplicationController
|
3
|
+
allow_everyone
|
4
|
+
self.responder = ConfirmationResponder
|
5
|
+
respond_to :html, :json
|
6
|
+
before_action :set_user
|
7
|
+
|
8
|
+
def update
|
9
|
+
@user.confirm
|
10
|
+
respond_with(@user, location: root_path)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def set_user
|
16
|
+
@user = User.find_by!(confirmation_key: params[:confirmation_key])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Nyauth
|
2
|
+
class NewPasswordRequestsController < ApplicationController
|
3
|
+
allow_everyone
|
4
|
+
respond_to :html, :json
|
5
|
+
before_action :set_user, only: [:create]
|
6
|
+
after_action :send_mail, only: [:create], if: -> { @user.new_password_key.present? }
|
7
|
+
|
8
|
+
def new
|
9
|
+
end
|
10
|
+
|
11
|
+
def create
|
12
|
+
@user.request_new_password
|
13
|
+
respond_with(@user, location: root_path)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def set_user
|
19
|
+
@user = User.find_by!(email: params[:user][:email])
|
20
|
+
rescue ActiveRecord::RecordNotFound
|
21
|
+
render :new
|
22
|
+
end
|
23
|
+
|
24
|
+
def send_mail
|
25
|
+
Nyauth::UserMailer.request_new_password(@user).deliver_now
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Nyauth
|
2
|
+
class NewPasswordsController < ApplicationController
|
3
|
+
allow_everyone
|
4
|
+
respond_to :html, :json
|
5
|
+
before_action :set_user
|
6
|
+
|
7
|
+
def edit
|
8
|
+
end
|
9
|
+
|
10
|
+
def update
|
11
|
+
@user.update_new_password(user_params)
|
12
|
+
respond_with(@user, location: nyauth.new_session_path)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def set_user
|
18
|
+
@user = User.find_by!(new_password_key: params[:new_password_key])
|
19
|
+
end
|
20
|
+
|
21
|
+
def user_params
|
22
|
+
params.fetch(:user, {})
|
23
|
+
.permit(:password, :password_confirmation)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Nyauth
|
2
|
+
class PasswordsController < ApplicationController
|
3
|
+
respond_to :html, :json
|
4
|
+
before_action :set_user
|
5
|
+
|
6
|
+
def edit
|
7
|
+
end
|
8
|
+
|
9
|
+
def update
|
10
|
+
@user.attributes = user_params
|
11
|
+
@user.save(context: :update_password)
|
12
|
+
respond_with(@user, location: root_path)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def set_user
|
18
|
+
@user = User.find(current_authenticated.id)
|
19
|
+
end
|
20
|
+
|
21
|
+
def user_params
|
22
|
+
params.fetch(:user, {})
|
23
|
+
.permit(:password, :password_confirmation)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Nyauth
|
2
|
+
class RegistrationsController < ApplicationController
|
3
|
+
allow_everyone
|
4
|
+
respond_to :html, :json
|
5
|
+
before_action :set_user
|
6
|
+
|
7
|
+
def new
|
8
|
+
end
|
9
|
+
|
10
|
+
def create
|
11
|
+
sign_in(@user) if @user.save
|
12
|
+
respond_with(@user, location: root_path)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def set_user
|
18
|
+
@user = User.new(user_params)
|
19
|
+
end
|
20
|
+
|
21
|
+
def user_params
|
22
|
+
params.fetch(:user, {})
|
23
|
+
.permit(:email, :password, :password_confirmation)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Nyauth
|
2
|
+
class SessionsController < ApplicationController
|
3
|
+
allow_everyone only: [:new, :create]
|
4
|
+
respond_to :html, :json
|
5
|
+
before_action :set_session_service
|
6
|
+
|
7
|
+
def new
|
8
|
+
end
|
9
|
+
|
10
|
+
def create
|
11
|
+
sign_in(@session_service.client) if @session_service.save
|
12
|
+
respond_with @session_service, location: root_path
|
13
|
+
end
|
14
|
+
|
15
|
+
def destroy
|
16
|
+
sign_out
|
17
|
+
respond_with @session_service, location: root_path
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def set_session_service
|
23
|
+
@session_service = Nyauth::SessionService.new(session_service_params)
|
24
|
+
end
|
25
|
+
|
26
|
+
def session_service_params
|
27
|
+
params.fetch(:session_service, {})
|
28
|
+
.permit(:email, :password)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Nyauth
|
2
|
+
class UserMailer < ActionMailer::Base
|
3
|
+
default from: "from@example.com"
|
4
|
+
layout 'nyauth/mailer'
|
5
|
+
def request_confirmation(user)
|
6
|
+
@user = user
|
7
|
+
mail to: user.email
|
8
|
+
end
|
9
|
+
|
10
|
+
def request_new_password(user)
|
11
|
+
@user = user
|
12
|
+
mail to: user.email
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Nyauth
|
2
|
+
module Authenticatable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
include Nyauth::PasswordDigestAbility
|
5
|
+
|
6
|
+
included do
|
7
|
+
validates :email, presence: true
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def authenticate(given_email, given_password)
|
12
|
+
record = where(email: given_email).last
|
13
|
+
return nil unless record
|
14
|
+
record.verify_password?(given_password) ? record : nil
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Nyauth
|
2
|
+
module Confirmable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
before_validation :check_confirmation_key, on: :confirm
|
7
|
+
end
|
8
|
+
|
9
|
+
def confirm
|
10
|
+
self.confirmed_at = Time.current
|
11
|
+
save(context: :confirm)
|
12
|
+
end
|
13
|
+
|
14
|
+
def confirmed?
|
15
|
+
self.confirmed_at.present?
|
16
|
+
end
|
17
|
+
|
18
|
+
def request_confirmation
|
19
|
+
self.confirmation_key = SecureRandom.hex(32)
|
20
|
+
self.confirmation_key_expired_at = Time.current + 1.hour
|
21
|
+
save
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def check_confirmation_key
|
27
|
+
if confirmation_key_expired_at.past?
|
28
|
+
errors.add(:confirmation_key, :expired)
|
29
|
+
else
|
30
|
+
self.confirmation_key = nil
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Nyauth
|
2
|
+
module NewPasswordAbility
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
before_validation :check_new_password_key, on: :new_password
|
7
|
+
validates :email, email: { strict_mode: false }
|
8
|
+
validates :password, presence: true,
|
9
|
+
length: { minimum: 8 },
|
10
|
+
on: [:create, :update_password, :new_password]
|
11
|
+
validates :password, confirmation: true
|
12
|
+
end
|
13
|
+
|
14
|
+
def update_new_password(params)
|
15
|
+
self.attributes = params
|
16
|
+
self.save(context: :new_password)
|
17
|
+
end
|
18
|
+
|
19
|
+
def request_new_password
|
20
|
+
self.new_password_key = SecureRandom.hex(32)
|
21
|
+
self.new_password_key_expired_at = Time.current + 1.hour
|
22
|
+
save
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def check_new_password_key
|
28
|
+
if new_password_key_expired_at.past?
|
29
|
+
errors.add(:new_password_key, :expired)
|
30
|
+
else
|
31
|
+
self.new_password_key = nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Nyauth
|
2
|
+
module PasswordDigestAbility
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
DIGEST_STRETCHES = 1000
|
5
|
+
|
6
|
+
included do
|
7
|
+
attr_accessor :password, :password_confirmation
|
8
|
+
validates :password_digest, presence: true
|
9
|
+
before_validation do
|
10
|
+
set_password_salt if password.present?
|
11
|
+
set_password_digest if password.present?
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def verify_password?(raw_password)
|
16
|
+
password_digest == generate_password_digest(raw_password)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def generate_password_digest(password)
|
22
|
+
DIGEST_STRETCHES.times do
|
23
|
+
password = Digest::SHA256.hexdigest("#{password}#{password_salt}")
|
24
|
+
end
|
25
|
+
password
|
26
|
+
end
|
27
|
+
|
28
|
+
def generate_password_salt
|
29
|
+
"#{id}#{SecureRandom.hex(128)}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def set_password_salt
|
33
|
+
self.password_salt = generate_password_salt
|
34
|
+
end
|
35
|
+
|
36
|
+
def set_password_digest
|
37
|
+
self.password_digest = generate_password_digest(password)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Nyauth
|
2
|
+
class SessionService
|
3
|
+
include ActiveModel::Model
|
4
|
+
attr_reader :email, :password, :client
|
5
|
+
|
6
|
+
def initialize(session_service_params = {})
|
7
|
+
@email = session_service_params[:email]
|
8
|
+
@password = session_service_params[:password]
|
9
|
+
end
|
10
|
+
|
11
|
+
def save(options = {})
|
12
|
+
options.reverse_merge!(as: :user)
|
13
|
+
klass = options[:as].to_s.classify.constantize
|
14
|
+
@client = klass.authenticate(email, password)
|
15
|
+
errors.add(:client, 'invalid email or password') unless @client
|
16
|
+
client
|
17
|
+
rescue
|
18
|
+
errors.add(:client, 'invalid email or password')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= yield %>
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= yield %>
|
@@ -0,0 +1,5 @@
|
|
1
|
+
= form_for(User.new, url: confirmation_requests_path, mehotd: :post, html: { class: 'pure-form' }) do |f|
|
2
|
+
fieldset
|
3
|
+
legend= t 'nav.confirmation_requests.new'
|
4
|
+
= f.text_field(:email, placeholder: :email)
|
5
|
+
= f.submit 'request confirmation', data: { disable_with: '...' }, class: 'pure-button pure-button-primary'
|
File without changes
|
@@ -0,0 +1,14 @@
|
|
1
|
+
= form_for(@group_request, html: { class: 'pure-form pure-form-stacked' }) do |f|
|
2
|
+
fieldset
|
3
|
+
legend
|
4
|
+
|
5
|
+
- if @group_request.errors.present?
|
6
|
+
h2 errors
|
7
|
+
ul.errors
|
8
|
+
- @group_request.errors.full_messages.each do |e|
|
9
|
+
li #{e}
|
10
|
+
= f.text_field :key, placeholder: 'your team for domain'
|
11
|
+
|.circleaf.com
|
12
|
+
- unless signed_in?
|
13
|
+
= f.email_field :email, placeholder: 'your email'
|
14
|
+
= f.submit 'Request', data: { disable_with: '...' }, class: 'pure-button pure-button-primary'
|
File without changes
|
@@ -0,0 +1,15 @@
|
|
1
|
+
DOCTYPE
|
2
|
+
html
|
3
|
+
head
|
4
|
+
title circleaf
|
5
|
+
= stylesheet_link_tag 'application', media: 'all'
|
6
|
+
= javascript_include_tag 'application'
|
7
|
+
= csrf_meta_tags
|
8
|
+
body
|
9
|
+
= render 'navigation'
|
10
|
+
- if flash[:notice].present?
|
11
|
+
.messages= flash[:notice]
|
12
|
+
- if flash[:alert].present? # FIXME: DRY
|
13
|
+
.messages= flash[:alert]
|
14
|
+
.main-content
|
15
|
+
== yield
|
@@ -0,0 +1 @@
|
|
1
|
+
== yield
|
@@ -0,0 +1 @@
|
|
1
|
+
== yield
|
@@ -0,0 +1,5 @@
|
|
1
|
+
= form_for(User.new, url: new_password_requests_path, mehotd: :post, html: { class: 'pure-form' }) do |f|
|
2
|
+
fieldset
|
3
|
+
legend= t 'nav.new_password_requests.new'
|
4
|
+
= f.text_field(:email, placeholder: :email)
|
5
|
+
= f.submit 'request new password', data: { disable_with: '...' }, class: 'pure-button pure-button-primary'
|
@@ -0,0 +1,11 @@
|
|
1
|
+
= form_for(@user, url: new_password_path(params[:new_password_key]), html: { class: 'pure-form pure-form-stacked' }) do |f|
|
2
|
+
fieldset
|
3
|
+
legend
|
4
|
+
- if @user.errors.present?
|
5
|
+
h2 errors
|
6
|
+
ul.errors
|
7
|
+
- @user.errors.full_messages.each do |e|
|
8
|
+
li #{e}
|
9
|
+
= f.password_field :password, placeholder: :password
|
10
|
+
= f.password_field :password_confirmation, placeholder: :confirmation
|
11
|
+
= f.submit 'Update', data: { disable_with: '...' }, class: 'pure-button pure-button-primary'
|
@@ -0,0 +1,11 @@
|
|
1
|
+
= form_for(@user, url: password_path, html: { class: 'pure-form pure-form-stacked' }) do |f|
|
2
|
+
fieldset
|
3
|
+
legend
|
4
|
+
- if @user.errors.present?
|
5
|
+
h2 errors
|
6
|
+
ul.errors
|
7
|
+
- @user.errors.full_messages.each do |e|
|
8
|
+
li #{e}
|
9
|
+
= f.password_field :password, placeholder: :password
|
10
|
+
= f.password_field :password_confirmation, placeholder: :confirmation
|
11
|
+
= f.submit 'Update', data: { disable_with: '...' }, class: 'pure-button pure-button-primary'
|