usps-jwt_auth 0.2.6 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 34bde76a473a9a091c8b44a0253d49868d957e7ea2edd365aae1d0b8639073e1
4
- data.tar.gz: 2f5a5df0b10c3344b563abede725f50b65c3fad6694babb2dd6683569e3d784c
3
+ metadata.gz: e46b14c6f88047e2a91d8b3dee98dc62b77453e8df259af908673471215eaf61
4
+ data.tar.gz: 758b74fc5fe973143b1bf23cb73df23c27fbf97953e51a244cdb5fda41dc6140
5
5
  SHA512:
6
- metadata.gz: b77c1978798dbe4e885b7f9f47b46d18b53bc0e72c8075ad6be054d0ac0a80c4268f945431523158f3534b896858ad32b58fa7153ae3934d87e638fa4df57bf8
7
- data.tar.gz: fe942f554c30ef6eca06a9e99f8d4e0b73381e59da2145ae7dabe233008db30b5a01ca19095d2f48d6168e367de0db62b880eaf54a3bf74618704538de9e1fc7
6
+ metadata.gz: 64525963c48bbc9485cf639e0d09473288df601d3b6b85c3d7077cda7da694395d7d5070de76f3de5c5fc27498d42b8cead943ce670f5f39b3582c8fcc7d8e5f
7
+ data.tar.gz: 6b345b3c1f03158242261cea4e01d95939a3c87e0f4af77efb0b4c129b55ddc284d4d0c7a10d5e45a75e6f6d0ccaca4e9b81d9e8b40135aec81b92a6728fa311
data/Gemfile.lock CHANGED
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- usps-jwt_auth (0.2.6)
4
+ usps-jwt_auth (0.2.7)
5
5
  activesupport (~> 8.0)
6
+ colorize (~> 1.1)
6
7
  fileutils (~> 1.7)
7
8
  jwt (~> 3.1)
8
9
 
@@ -26,6 +27,7 @@ GEM
26
27
  base64 (0.3.0)
27
28
  benchmark (0.4.1)
28
29
  bigdecimal (3.3.1)
30
+ colorize (1.1.0)
29
31
  concurrent-ruby (1.3.5)
30
32
  connection_pool (2.5.4)
31
33
  date (3.4.1)
data/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  Add the gem to your Gemfile:
8
8
 
9
9
  ```ruby
10
- gem 'usps-jwt_auth', '>= 0.2.6'
10
+ gem 'usps-jwt_auth', '>= 0.2.7'
11
11
  ```
12
12
 
13
13
  Then run the install task:
@@ -18,17 +18,26 @@ bundle exec rails usps:jwt:install
18
18
 
19
19
  ## Configuration
20
20
 
21
+ Config options `audience`, `is_admin`, and `find_member` are required.
22
+
21
23
  ```ruby
22
24
  Usps::JwtAuth.configure do |config|
23
- # config.environment = Rails.env
24
- # config.keys_path = 'config/keys'
25
- # config.public_keys_path = 'config/public_keys'
26
- # config.key_size = 4096
27
- # config.algorithm = 'RS512'
28
-
29
- config.audience = ENV.fetch('JWT_AUDIENCE')
30
- # config.issuer_base = ENV.fetch('JWT_ISSUER_BASE', 'usps:1')
31
- config.issuers = ENV.fetch('JWT_ISSUERS', 'admin:1').split(',')
25
+ # This will default to `Rails.env` if available.
26
+ config.environment = 'development'
27
+
28
+ # These will append to `Rails.root` if available.
29
+ config.keys_path = 'config/keys'
30
+ config.public_keys_path = 'config/public_keys'
31
+
32
+ # These options will default to the listed `ENV` variable if available.
33
+ #
34
+ # The ultimate defaults are listed to the right.
35
+ #
36
+ config.audience = 'example' # ENV['JWT_AUDIENCE'] # nil
37
+ config.algorithm = 'RS512' # ENV['JWT_ALGORITHM'] # 'RS512'
38
+ config.key_size = 4096 # ENV['JWT_KEY_SIZE'] # 4096
39
+ config.issuer_base = 'usps:1' # ENV['JWT_ISSUER_BASE'] # 'usps:1'
40
+ config.issuers = ['admin:1'] # ENV['JWT_ISSUERS'] # []
32
41
 
33
42
  config.is_admin = ->(user) { Pundit.policy(user, :admin).admin? }
34
43
  config.find_member = ->(certificate) { Members::Member.find(certificate) }
@@ -0,0 +1,2 @@
1
+ Metrics/BlockLength:
2
+ Enabled: false
@@ -0,0 +1,2 @@
1
+ Style/FrozenStringLiteralComment:
2
+ Enabled: false
@@ -0,0 +1,7 @@
1
+ Usps::JwtAuth.configure do |config|
2
+ config.audience = ENV.fetch('JWT_AUDIENCE', 'example')
3
+ config.issuers = ENV.fetch('JWT_ISSUERS', ['admin:1'])
4
+
5
+ config.is_admin = ->(user) { Pundit.policy(user, :admin).admin? }
6
+ config.find_member = ->(certificate) { Members::Member.find(certificate) }
7
+ end
@@ -1,24 +1,110 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'colorize'
4
+
3
5
  namespace :usps do
4
6
  namespace :jwt do
5
7
  desc 'Setup JWT Authentication'
6
8
  task install: :environment do
7
- # Ensure keys directories exist
9
+ Rake::Task['usps:jwt:keys'].invoke
10
+ Rake::Task['usps:jwt:ignore_keys'].invoke
11
+ Rake::Task['usps:jwt:initializer'].invoke
12
+ Rake::Task['usps:jwt:controller'].invoke
13
+ end
14
+
15
+ desc 'Ensure key directories exist'
16
+ task keys: :environment do
17
+ print 'Ensuring configured key directories exist... '
18
+
8
19
  FileUtils.mkdir_p(Usps::JwtAuth.config.keys_path)
9
20
  FileUtils.touch(Usps::JwtAuth.config.keys_path.join('.keep'))
10
21
  FileUtils.mkdir_p(Usps::JwtAuth.config.public_keys_path)
11
22
  FileUtils.touch(Usps::JwtAuth.config.public_keys_path.join('.keep'))
12
23
 
13
- # Ignore keys directories from git
14
- File.open('.gitignore', 'a') do |file|
15
- file.puts <<~IGNORE
16
- /#{Usps::JwtAuth.config.keys_path}
17
- /!#{Usps::JwtAuth.config.keys_path.join('.keep')}
18
- /#{Usps::JwtAuth.config.public_keys_path}
19
- /!#{Usps::JwtAuth.config.public_keys_path.join('.keep')}
20
- IGNORE
24
+ puts 'Done.'.green
25
+ end
26
+
27
+ desc 'Create configuration initializer'
28
+ task initializer: :environment do
29
+ print 'Creating configuration initializer... '
30
+
31
+ initializer_path = defined?(Rails) ? Rails.root.join('config/initializers/jwt_auth.rb') : 'jwt_auth.rb'
32
+
33
+ initializer = File.read(File.join(__dir__, 'default/initializer.rb'))
34
+
35
+ if File.exist?(initializer_path)
36
+ if File.read(initializer_path).include?('Usps::JwtAuth.configure')
37
+ puts 'Existing detected: Skipping.'.yellow
38
+ next
39
+ end
40
+
41
+ puts "Conflict!\n".red
42
+ warn "File #{initializer_path} already exists!".yellow
43
+ puts "\nPlease choose a different location for your configuration initializer:\n".yellow
44
+ puts "#{initializer}\n".blue
45
+ abort
46
+ else
47
+ File.open(initializer_path, 'w') { |f| f.puts(initializer) }
48
+
49
+ puts 'Done.'.green
50
+ end
51
+ end
52
+
53
+ desc 'Add key directories to git ignore'
54
+ task ignore_keys: :environment do
55
+ print 'Git ignoring key directories... '
56
+
57
+ ignores = [
58
+ "/#{Usps::JwtAuth.config.raw_keys_path}/*",
59
+ "!/#{Usps::JwtAuth.config.raw_keys_path.join('.keep')}",
60
+ "/#{Usps::JwtAuth.config.raw_public_keys_path}/*",
61
+ "!/#{Usps::JwtAuth.config.raw_public_keys_path.join('.keep')}"
62
+ ]
63
+
64
+ ignores.each do |ignore|
65
+ next if File.foreach('.gitignore').find { |line| line.chomp == ignore }
66
+
67
+ File.open('.gitignore', 'a') { |f| f.puts ignore }
68
+ end
69
+
70
+ puts 'Done.'.green
71
+ end
72
+
73
+ desc 'Add include and callback to ApplicationController'
74
+ task controller: :environment do
75
+ next unless defined?(Rails)
76
+
77
+ application_controller = Rails.root.join('app/controllers/application_controller.rb')
78
+ next unless File.exist?(application_controller)
79
+
80
+ existing = File.foreach(application_controller).find do |line|
81
+ line.chomp.match?(/^\s+include Usps::JwtAuth::Concern$/)
82
+ end
83
+
84
+ print 'Including in ApplicationController... '
85
+
86
+ if existing
87
+ puts 'Existing detected: Skipping.'.yellow
88
+ next
89
+ end
90
+
91
+ lines = []
92
+ File.foreach(application_controller) do |line|
93
+ lines << line
94
+ next unless line.chomp =~ /^(\s*)class ApplicationController < ActionController::Base$/
95
+
96
+ indent = Regexp.last_match(1)
97
+
98
+ lines << "#{indent} include Usps::JwtAuth::Concern"
99
+ lines << ''
100
+ lines << "#{indent} before_action :authorize_user_from_jwt!"
101
+ lines << "#{indent} # skip_before_action :authorize_user_from_jwt!, only: %i[]"
102
+ lines << ''
21
103
  end
104
+
105
+ File.open(application_controller, 'w').puts(lines)
106
+
107
+ puts 'Done.'.green
22
108
  end
23
109
  end
24
110
  end
@@ -14,11 +14,11 @@ module Usps
14
14
  @environment = defined?(Rails) ? Rails.env : ActiveSupport::StringInquirer.new('development')
15
15
  @keys_path = Pathname.new('config/keys')
16
16
  @public_keys_path = Pathname.new('config/public_keys')
17
- @key_size = 4096
18
- @algorithm = 'RS512'
19
- @issuer_base = 'usps:1'
20
- @issuers = []
21
- @audience = nil
17
+ @key_size = ENV.fetch('JWT_KEY_SIZE', '4096').to_i
18
+ @algorithm = ENV.fetch('JWT_ALGORITHM', 'RS512')
19
+ @issuer_base = ENV.fetch('JWT_ISSUER_BASE', 'usps:1')
20
+ @issuers = ENV.fetch('JWT_ISSUERS', [])
21
+ @audience = ENV.fetch('JWT_AUDIENCE', nil)
22
22
 
23
23
  yield self if block_given? # Also support setting options on initialize
24
24
  end
@@ -31,6 +31,10 @@ module Usps
31
31
  defined?(Rails) ? Rails.root.join(@keys_path) : @keys_path
32
32
  end
33
33
 
34
+ def raw_keys_path
35
+ @keys_path
36
+ end
37
+
34
38
  def keys_path=(path)
35
39
  @keys_path = path.is_a?(Pathname) ? path : Pathname.new(path)
36
40
  end
@@ -39,6 +43,10 @@ module Usps
39
43
  defined?(Rails) ? Rails.root.join(@public_keys_path) : @public_keys_path
40
44
  end
41
45
 
46
+ def raw_public_keys_path
47
+ @public_keys_path
48
+ end
49
+
42
50
  def public_keys_path=(path)
43
51
  @public_keys_path = path.is_a?(Pathname) ? path : Pathname.new(path)
44
52
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Usps
4
4
  module JwtAuth
5
- VERSION = '0.2.6'
5
+ VERSION = '0.2.7'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usps-jwt_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Fiander
@@ -23,6 +23,20 @@ dependencies:
23
23
  - - "~>"
24
24
  - !ruby/object:Gem::Version
25
25
  version: '8.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: colorize
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '1.1'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.1'
26
40
  - !ruby/object:Gem::Dependency
27
41
  name: fileutils
28
42
  requirement: !ruby/object:Gem::Requirement
@@ -63,6 +77,9 @@ files:
63
77
  - Gemfile.lock
64
78
  - README.md
65
79
  - Rakefile
80
+ - lib/tasks/.rubocop.yml
81
+ - lib/tasks/default/.rubocop.yml
82
+ - lib/tasks/default/initializer.rb
66
83
  - lib/tasks/jwt_auth.rake
67
84
  - lib/usps/jwt_auth.rb
68
85
  - lib/usps/jwt_auth/concern.rb