authentication-zero 0.0.21 → 0.0.22

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
  SHA256:
3
- metadata.gz: e4508d502f129d12c259168c7dc3076ae69adfc30622d512b52069a29bd677d7
4
- data.tar.gz: f845c72250632ffaa4253ec6a51efee05ed7df67b6dff9ae153ccfc301a3f701
3
+ metadata.gz: f745db607e21f39bb22fec9452b30147cebbf4a41d49150450c14035b00b1d0e
4
+ data.tar.gz: f412264a06233f8571dad3640bf9b2ee853e289ad168ad5e12f5fa8d3a6bfb04
5
5
  SHA512:
6
- metadata.gz: 07e8a137c6fcb03ce1de75f40a901d9df43038dc15086efc128ec5f727c160b4109e3956232acbd166948dc4ed01b2309aa868758fe578adc33afa706f318a8f
7
- data.tar.gz: 0b8c33acc4d57eae5cd0fc4ff9272129f01eee5f42cd0f864d55411111b432ffed69c0693dcce1f5e96f83756b3b752af6366242fe58662ed95fcc7146c3ff16
6
+ metadata.gz: d28443fe294ac245251268f5130dc7c4776a793fcff46e89e37fbb34c419455735be7d63ac7a3fa4fbaaac976806e37f8770be6c32a44663cdee4374b2b2ffb3
7
+ data.tar.gz: 902d2c094e0d7fb1cd5c10a8b81d733fda6cb9580baf2312591cce588fe82b9b4302f80a41c6e75bd8dae037411028968cc82ff2159fb4155891f7ebd66587c1
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- authentication-zero (0.0.21)
4
+ authentication-zero (0.0.22)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -19,7 +19,7 @@ The purpose of authentication zero is to generate a pre-built authentication sys
19
19
 
20
20
  - [has_secure_password](https://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html#method-i-has_secure_password): Adds methods to set and authenticate against a BCrypt password.
21
21
  - [has_secure_token](https://api.rubyonrails.org/classes/ActiveRecord/SecureToken/ClassMethods.html#method-i-has_secure_token): Adds methods to generate unique tokens.
22
- - [encrypts](https://guides.rubyonrails.org/active_record_encryption.html) Encrypts the session_token on database so if an attacker gained access to your database, a snapshot of it, or your application logs, they wouldn't be able to make sense of the encrypted information.
22
+ - [signed cookies](https://api.rubyonrails.org/classes/ActionDispatch/Cookies.html): Returns a jar that'll automatically generate a signed representation of cookie value and verify it when reading from the cookie again.
23
23
  - [httponly cookies](https://api.rubyonrails.org/classes/ActionDispatch/Cookies.html): A cookie with the httponly attribute is inaccessible to the JavaScript, this precaution helps mitigate cross-site scripting (XSS) attacks.
24
24
  - [signed_id](https://api.rubyonrails.org/classes/ActiveRecord/SignedId.html): Returns a signed id that is tamper proof, so it's safe to send in an email or otherwise share with the outside world.
25
25
  - [Current attributes](https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html): Abstract super class that provides a thread-isolated attributes singleton, which resets automatically before and after each request.
@@ -38,12 +38,6 @@ gem "authentication-zero"
38
38
 
39
39
  Then run `bundle install`
40
40
 
41
- First, you need to [set up active record encryption](https://guides.rubyonrails.org/active_record_encryption.html#setup), you must generate your keys and put them in your credentials:
42
- ```
43
- $ rails db:encryption:init
44
- $ rails credentials:edit
45
- ```
46
-
47
41
  You'll need to set the root path in your routes.rb, for this example let's use the following:
48
42
 
49
43
  ```ruby
@@ -1,3 +1,3 @@
1
1
  module AuthenticationZero
2
- VERSION = "0.0.21"
2
+ VERSION = "0.0.22"
3
3
  end
@@ -60,7 +60,7 @@ class AuthenticationGenerator < Rails::Generators::NamedBase
60
60
  private
61
61
  def authenticate
62
62
  authenticate_or_request_with_http_token do |token, _options|
63
- Current.#{singular_table_name} = #{class_name}.find_by_session_token(token)
63
+ Current.#{singular_table_name} = #{class_name}.find_signed_session_token(token)
64
64
  end
65
65
  end
66
66
  CODE
@@ -71,7 +71,7 @@ class AuthenticationGenerator < Rails::Generators::NamedBase
71
71
 
72
72
  private
73
73
  def authenticate
74
- if #{singular_table_name} = #{class_name}.find_by_session_token(cookies[:session_token])
74
+ if #{singular_table_name} = #{class_name}.find_by_session_token(cookies.signed[:session_token])
75
75
  Current.#{singular_table_name} = #{singular_table_name}
76
76
  else
77
77
  redirect_to sign_in_path, alert: "You need to sign in or sign up before continuing"
@@ -5,7 +5,7 @@ class SessionsController < ApplicationController
5
5
  @<%= singular_table_name %> = <%= class_name %>.find_by_email(params[:email])
6
6
 
7
7
  if @<%= singular_table_name %>.try(:authenticate, params[:password])
8
- render json: { session_token: @<%= singular_table_name %>.session_token }
8
+ render json: { session_token: @<%= singular_table_name %>.signed_session_token }
9
9
  else
10
10
  render json: { error: "Invalid email or password" }, status: :unauthorized
11
11
  end
@@ -9,7 +9,7 @@ class RegistrationsController < ApplicationController
9
9
  @<%= singular_table_name %> = <%= class_name %>.new(<%= "#{singular_table_name}_params" %>)
10
10
 
11
11
  if @<%= singular_table_name %>.save
12
- cookies[:session_token] = { value: @<%= singular_table_name %>.session_token, httponly: true }
12
+ cookies.signed[:session_token] = { value: @<%= singular_table_name %>.session_token, httponly: true }
13
13
  redirect_to root_path, notice: "Welcome! You have signed up successfully"
14
14
  else
15
15
  render :new, status: :unprocessable_entity
@@ -10,9 +10,9 @@ class SessionsController < ApplicationController
10
10
 
11
11
  if @<%= singular_table_name %>.try(:authenticate, params[:password])
12
12
  if params[:remember_me] == "1"
13
- cookies.permanent[:session_token] = { value: @<%= singular_table_name %>.session_token, httponly: true }
13
+ cookies.signed.permanent[:session_token] = { value: @<%= singular_table_name %>.session_token, httponly: true }
14
14
  else
15
- cookies[:session_token] = { value: @<%= singular_table_name %>.session_token, httponly: true }
15
+ cookies.signed[:session_token] = { value: @<%= singular_table_name %>.session_token, httponly: true }
16
16
  end
17
17
 
18
18
  redirect_to root_path, notice: "Signed in successfully"
@@ -6,8 +6,6 @@ class <%= class_name %> < ApplicationRecord
6
6
  validates :email, format: { with: /\A[^@\s]+@[^@\s]+\z/ }
7
7
  validates_length_of :password, minimum: 8, allow_blank: true
8
8
 
9
- encrypts :session_token, deterministic: true
10
-
11
9
  before_validation do
12
10
  self.email = email.downcase.strip
13
11
  end
@@ -23,4 +21,16 @@ class <%= class_name %> < ApplicationRecord
23
21
  PasswordMailer.with(<%= singular_table_name %>: self).changed.deliver_later
24
22
  end
25
23
  end
24
+
25
+ <% if options.api? -%>
26
+ def signed_session_token
27
+ self.class.signed_id_verifier.generate(session_token)
28
+ end
29
+
30
+ def self.find_signed_session_token(signed_session_token)
31
+ if session_token = signed_id_verifier.verified(signed_session_token)
32
+ find_by_session_token(session_token)
33
+ end
34
+ end
35
+ <% end -%>
26
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authentication-zero
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.21
4
+ version: 0.0.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nixon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-18 00:00:00.000000000 Z
11
+ date: 2022-02-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: