Class: Usps::JwtAuth::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/usps/jwt_auth/config.rb

Overview

Configure JWT Authentication

Constant Summary collapse

REQUIRED_OPTIONS =
%i[audience is_admin find_member].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Config

Returns a new instance of Config.

Yields:

  • (_self)

Yield Parameters:



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/usps/jwt_auth/config.rb', line 13

def initialize
  @environment = defined?(Rails) ? Rails.env : ActiveSupport::StringInquirer.new('development')
  @keys_path = Pathname.new('config/keys')
  @public_keys_path = Pathname.new('config/public_keys')
  @key_size = ENV.fetch('JWT_KEY_SIZE', '4096').to_i
  @algorithm = ENV.fetch('JWT_ALGORITHM', 'RS512')
  @issuer_base = ENV.fetch('JWT_ISSUER_BASE', 'usps:1')
  @issuers = ENV.fetch('JWT_ISSUERS', [])
  @audience = ENV.fetch('JWT_AUDIENCE', nil)

  yield self if block_given? # Also support setting options on initialize
end

Instance Attribute Details

#algorithmObject

Returns the value of attribute algorithm.



10
11
12
# File 'lib/usps/jwt_auth/config.rb', line 10

def algorithm
  @algorithm
end

#audienceObject

Returns the value of attribute audience.



10
11
12
# File 'lib/usps/jwt_auth/config.rb', line 10

def audience
  @audience
end

#environmentObject

Returns the value of attribute environment.



11
12
13
# File 'lib/usps/jwt_auth/config.rb', line 11

def environment
  @environment
end

#find_memberObject

Returns the value of attribute find_member.



10
11
12
# File 'lib/usps/jwt_auth/config.rb', line 10

def find_member
  @find_member
end

#is_adminObject

Returns the value of attribute is_admin.



10
11
12
# File 'lib/usps/jwt_auth/config.rb', line 10

def is_admin
  @is_admin
end

#issuer_baseObject

Returns the value of attribute issuer_base.



10
11
12
# File 'lib/usps/jwt_auth/config.rb', line 10

def issuer_base
  @issuer_base
end

#issuersObject

Returns the value of attribute issuers.



10
11
12
# File 'lib/usps/jwt_auth/config.rb', line 10

def issuers
  @issuers
end

#key_sizeObject

Returns the value of attribute key_size.



10
11
12
# File 'lib/usps/jwt_auth/config.rb', line 10

def key_size
  @key_size
end

Instance Method Details

#keys_pathObject



30
31
32
# File 'lib/usps/jwt_auth/config.rb', line 30

def keys_path
  defined?(Rails) ? Rails.root.join(@keys_path) : @keys_path
end

#keys_path=(path) ⇒ Object



38
39
40
# File 'lib/usps/jwt_auth/config.rb', line 38

def keys_path=(path)
  @keys_path = path.is_a?(Pathname) ? path : Pathname.new(path)
end

#public_keys_pathObject



42
43
44
# File 'lib/usps/jwt_auth/config.rb', line 42

def public_keys_path
  defined?(Rails) ? Rails.root.join(@public_keys_path) : @public_keys_path
end

#public_keys_path=(path) ⇒ Object



50
51
52
# File 'lib/usps/jwt_auth/config.rb', line 50

def public_keys_path=(path)
  @public_keys_path = path.is_a?(Pathname) ? path : Pathname.new(path)
end

#raw_keys_pathObject



34
35
36
# File 'lib/usps/jwt_auth/config.rb', line 34

def raw_keys_path
  @keys_path
end

#raw_public_keys_pathObject



46
47
48
# File 'lib/usps/jwt_auth/config.rb', line 46

def raw_public_keys_path
  @public_keys_path
end

#validate!Object



54
55
56
57
58
59
# File 'lib/usps/jwt_auth/config.rb', line 54

def validate!
  missing_required_options = REQUIRED_OPTIONS.select { public_send(it).nil? }
  return unless missing_required_options.any?

  raise "Missing required options: #{missing_required_options.join(', ')}"
end