auth_rails 1.0.0 → 1.0.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
  SHA256:
3
- metadata.gz: 7b6047449dbddff4af60565f7c096eae14f8800b47bd129dceb3ea7f90fb42f7
4
- data.tar.gz: 80776e48b7c557c9ab11ec1e9b05ae06e31bc15cca56a4143bfbf46254d2a109
3
+ metadata.gz: a80fe0179e20db84cc1966bd18331d98b3172c4cd5c3b652949ef7bc7a508b0a
4
+ data.tar.gz: 6cc51206c4381735dd92f118c8268df20998739cad769bcd2e01211dabb20e76
5
5
  SHA512:
6
- metadata.gz: 03502ea907678c9ca3122701789e82a145f9c3a143d415153bbde798ad7b5a375cc464ba1e1bf4d8a583d42b298501c00fcf00aadbc7b2be221b8b2044a149fe
7
- data.tar.gz: f90cfeab74aa249aebbe6a33deec189901e90c74bfdc0092d42b18a468a3e3eee3fe806611b24accf61a4c1ffc9fc0d1034b4a35cc4bb460fa94b5008790366f
6
+ metadata.gz: e7d2ccb1bbd06e8cf115267a998cd0490ac8e9583e5a351ca61c486d0ceafd26fda66eee7995a84e01dd4dd82ed29fa29c4af865cbf2f232b445a48bd6d0bc86
7
+ data.tar.gz: 7a2057151c16ea45d74eb88f9a501a232f5e714a713a22dc125b56fbb6f9710c0b02dd39d9364af8ab7a54e002b8cc511d95389d0d1467050470b970f2814ab1
data/README.md CHANGED
@@ -6,8 +6,43 @@ Simple authentication for rails.
6
6
  gem 'auth_rails'
7
7
  ```
8
8
 
9
+ # CLI
10
+
11
+ - init `auth_rails`
12
+
13
+ ```sh
14
+ rails g auth_rails
15
+ ```
16
+
17
+ - init `auth_rails` with strategy
18
+
19
+ ```sh
20
+ rails g auth_rails --strategy allowed_token
21
+ ```
22
+
23
+ - create migration for `allowed_token` strategy
24
+
25
+ ```sh
26
+ rails g auth_rails:migration --strategy allowed_token
27
+ ```
28
+
29
+ - if your model is not User
30
+
31
+ ```sh
32
+ rails g auth_rails:migration --strategy allowed_token --model CustomUser
33
+ ```
34
+
9
35
  # Configuration
10
36
 
37
+ - User model must have `has_secure_password`
38
+
39
+ ```rb
40
+ # app/models/user.rb
41
+ class User < ApplicationRecord
42
+ has_secure_password
43
+ end
44
+ ```
45
+
11
46
  ```rb
12
47
  # config/initializers/auth_rails.rb
13
48
 
@@ -84,6 +119,8 @@ end
84
119
 
85
120
  class User < ApplicationRecord
86
121
  include AuthRails::Concerns::AllowedTokenStrategy
122
+
123
+ has_secure_password
87
124
  end
88
125
  ```
89
126
 
@@ -128,6 +165,10 @@ module Api
128
165
  end
129
166
  ```
130
167
 
168
+ # Strategy list
169
+
170
+ - allowed_token
171
+
131
172
  # License
132
173
 
133
174
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -22,6 +22,11 @@ module AuthRails
22
22
 
23
23
  raise AuthRails.error_class, :unauthenticated if resource.blank?
24
24
 
25
+ resource.allowed_tokens.find_by(
26
+ jti: decoded_payload[:jti],
27
+ aud: decoded_payload[:aud]
28
+ )&.destroy!
29
+
25
30
  respond_to_refresh(generate_token(resource))
26
31
  end
27
32
 
data/auth_rails.gemspec CHANGED
@@ -40,5 +40,5 @@ Gem::Specification.new do |spec|
40
40
 
41
41
  spec.require_paths = ['lib']
42
42
 
43
- spec.add_dependency 'jwt'
43
+ spec.add_runtime_dependency 'jwt', '>= 2.7'
44
44
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AuthRails
4
- VERSION = '1.0.0'
4
+ VERSION = '1.0.2'
5
5
  end
data/lib/auth_rails.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'jwt'
4
+
3
5
  require_relative 'auth_rails/config'
4
6
  require_relative 'auth_rails/version'
5
7
  require_relative 'auth_rails/class_methods'
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AuthRails
4
+ class MigrationGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ source_root File.expand_path('templates', __dir__)
8
+
9
+ class_option :strategy,
10
+ aliases: '-strat',
11
+ type: :string,
12
+ desc: 'Strategy to use, default is AuthRails::Strategies::BaseStrategy',
13
+ default: 'base'
14
+
15
+ class_option :model,
16
+ aliases: '-m',
17
+ type: :string,
18
+ desc: 'Model for strategy to associate with',
19
+ default: 'user'
20
+
21
+ def create_migration_files
22
+ @model = (options[:model] || 'user').underscore.to_sym
23
+
24
+ case options[:strategy]
25
+ when 'allowed_token'
26
+ migration_template(
27
+ 'allowed_tokens.tt',
28
+ 'db/migrate/create_allowed_tokens.rb',
29
+ migration_version: migration_version
30
+ )
31
+ end
32
+ end
33
+
34
+ class << self
35
+ def next_migration_number(dirname)
36
+ next_migration_number = current_migration_number(dirname) + 1
37
+ ActiveRecord::Migration.next_migration_number(next_migration_number)
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def versioned_migrations?
44
+ Rails::VERSION::MAJOR >= 5
45
+ end
46
+
47
+ def migration_version
48
+ return unless versioned_migrations?
49
+
50
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateAllowedTokens < ActiveRecord::Migration<%= migration_version %>
4
+ def change
5
+ create_table :allowed_tokens do |t|
6
+ t.string :jti, null: false
7
+ t.string :aud
8
+ t.datetime :exp, null: false
9
+
10
+ t.timestamps
11
+
12
+ t.references :<%= @model %>, foreign_key: { on_delete: :cascade }, null: false
13
+
14
+ t.index %i[jti aud]
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AuthRailsGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('templates', __dir__)
5
+
6
+ class_option :strategy,
7
+ aliases: '-strat',
8
+ type: :string,
9
+ desc: 'Strategy to use, default is AuthRails::Strategies::BaseStrategy',
10
+ default: 'base'
11
+
12
+ class_option :model,
13
+ aliases: '-m',
14
+ type: :string,
15
+ desc: 'Model for strategy to associate with',
16
+ default: 'user'
17
+
18
+ def generate_auth_rails
19
+ @model = (options[:model] || 'user').camelcase
20
+ @is_allowed_token = options[:strategy] == 'allowed_token'
21
+
22
+ template(
23
+ 'auth_rails.tt',
24
+ 'config/initializers/auth_rails.rb'
25
+ )
26
+ end
27
+
28
+ def create_allowed_tokens_strategy
29
+ return if options[:strategy].blank? || options[:strategy] != 'allowed_token'
30
+
31
+ invoke(
32
+ 'auth_rails:migration',
33
+ [],
34
+ strategy: 'allowed_token',
35
+ model: (options[:model] || 'user').camelcase
36
+ )
37
+ end
38
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ AuthRails.configure do |config|
4
+ config.jwt do |jwt|
5
+ jwt.access_token do |access_token|
6
+ access_token.exp = 1.hour.since
7
+ access_token.secret_key = ENV.fetch('JWT_SECRET', '')
8
+ end
9
+
10
+ <%= @is_allowed_token ? '' : '# ' %>jwt.strategy = AuthRails::Strategies::AllowedTokenStrategy
11
+
12
+ # if you wanna use refresh token
13
+ # uncomment those lines below
14
+ # jwt.refresh_token do |refresh_token|
15
+ # refresh_token.http_only = true
16
+ # refresh_token.exp = 1.year.since
17
+ # refresh_token.algorithm = 'HS256'
18
+ # refresh_token.cookie_key = :ref_tok
19
+ # refresh_token.secret_key = ENV.fetch('JWT_SECRET', '')
20
+ # end
21
+ end
22
+ end
23
+
24
+ Rails.application.config.to_prepare do
25
+ AuthRails.configure do |config|
26
+ config.resource_class = <%= @model %>
27
+
28
+ # if you wanna use custom error classes
29
+ # uncomment code below
30
+ # config.error_class = AuthError
31
+ end
32
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auth_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alpha
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-25 00:00:00.000000000 Z
11
+ date: 2024-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '2.7'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '2.7'
27
27
  description: Simple authentication for Rails
28
28
  email:
29
29
  - alphanolucifer@gmail.com
@@ -47,6 +47,10 @@ files:
47
47
  - lib/auth_rails/strategies/allowed_token_strategy.rb
48
48
  - lib/auth_rails/strategies/base_strategy.rb
49
49
  - lib/auth_rails/version.rb
50
+ - lib/generators/auth_rails/migration_generator.rb
51
+ - lib/generators/auth_rails/templates/allowed_tokens.tt
52
+ - lib/generators/auth_rails_generator.rb
53
+ - lib/generators/templates/auth_rails.tt
50
54
  homepage: https://github.com/zgid123/auth_rails
51
55
  licenses:
52
56
  - MIT