Module: Darrrr::Provider

Includes:
Constants
Included in:
AccountProvider, RecoveryProvider
Defined in:
lib/darrrr/provider.rb

Constant Summary collapse

RECOVERY_PROVIDER_CACHE_LENGTH =
60.seconds
MAX_RECOVERY_PROVIDER_CACHE_LENGTH =
5.minutes
REQUIRED_CRYPTO_OPS =
[:sign, :verify, :encrypt, :decrypt].freeze

Constants included from Constants

Constants::CLOCK_SKEW, Constants::COUNTERSIGNED_RECOVERY_TOKEN_TYPE, Constants::DIGEST, Constants::GROUP, Constants::PRIME_256_V1, Constants::PROTOCOL_VERSION, Constants::RECOVERY_TOKEN_TYPE, Constants::TOKEN_ID_BYTE_LENGTH, Constants::WELL_KNOWN_CONFIG_PATH

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/darrrr/provider.rb', line 10

def self.included(base)
  base.instance_eval do
    # this represents the account/recovery provider on this web app
    class << self
      attr_accessor :this

      def configure(&block)
        raise ArgumentError, "Block required to configure #{self.name}" unless block_given?
        raise ProviderConfigError, "#{self.name} already configured" if self.this
        self.this = self.new.tap { |provider| provider.instance_eval(&block).freeze }
        self.this.privacy_policy = Darrrr.privacy_policy
        self.this.icon_152px = Darrrr.icon_152px
        self.this.issuer = Darrrr.authority
      end
    end
  end
end

Instance Method Details

#custom_encryptor=(encryptor) ⇒ Object

Overrides the global ‘encryptor` API to use

encryptor: a class/module that responds to all REQUIRED_CRYPTO_OPS.



42
43
44
45
46
47
48
# File 'lib/darrrr/provider.rb', line 42

def custom_encryptor=(encryptor)
  if valid_encryptor?(encryptor)
    @encryptor = encryptor
  else
    raise ArgumentError, "custom encryption class must respond to all of #{REQUIRED_CRYPTO_OPS}"
  end
end

#encryptorObject

Returns the crypto API to be used. A thread local instance overrides the globally configured value which overrides the default encryptor.



35
36
37
# File 'lib/darrrr/provider.rb', line 35

def encryptor
  Thread.current[encryptor_key] || @encryptor || DefaultEncryptor
end

#initialize(provider_origin = nil, attrs: nil) ⇒ Object



28
29
30
31
# File 'lib/darrrr/provider.rb', line 28

def initialize(provider_origin = nil, attrs: nil)
  self.issuer = provider_origin
  load(attrs) if attrs
end

#load(attrs = nil) ⇒ Object

Lazily loads attributes if attrs is nil. It makes an http call to the recovery provider’s well-known config location and caches the response if it’s valid json.

attrs: optional way of building the provider without making an http call.



71
72
73
74
75
# File 'lib/darrrr/provider.rb', line 71

def load(attrs = nil)
  body = attrs || fetch_config!
  set_attrs!(body)
  self
end

#with_encryptor(encryptor) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/darrrr/provider.rb', line 50

def with_encryptor(encryptor)
  raise ArgumentError, "A block must be supplied" unless block_given?
  unless valid_encryptor?(encryptor)
    raise ArgumentError, "custom encryption class must respond to all of #{REQUIRED_CRYPTO_OPS}"
  end

  Thread.current[encryptor_key] = encryptor
  yield
ensure
  Thread.current[encryptor_key] = nil
end