Sha256: cd74c6f5ebfe0a1d871668bb220534247a22c401239021590b5d29aa50f2372a

Contents?: true

Size: 934 Bytes

Versions: 23

Compression:

Stored size: 934 Bytes

Contents

# frozen_string_literal: true

module SolidusNexio
  EncryptionService = Struct.new(:salt) do
    delegate :encrypt_and_sign, :decrypt_and_verify, to: :encryptor

    def self.encrypt(salt, value)
      new(salt).encrypt_and_sign(value)
    end

    def self.decrypt(salt, value)
      new(salt).decrypt_and_verify(value)
    end

    private

    def encryptor
      ActiveSupport::MessageEncryptor.new(key)
    end


    NEXIO_SECRET = Rails.application.secrets.nexio_secret_key ||
                    ENV['NEXIO_SECRET_KEY'] ||
                    Rails.application.secrets.secret_key_base ||
                    ENV['SECRET_KEY_BASE']

    raise ArgumentError, 'Please setup nexio secret key. Rails.application.secrets.nexio_secret_key or ENV["NEXIO_SECRET_KEY"]' if NEXIO_SECRET.blank?

    def key
      ActiveSupport::KeyGenerator.new(NEXIO_SECRET).generate_key(salt, ActiveSupport::MessageEncryptor.key_len)
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
solidus_nexio-0.6.3 app/services/solidus_nexio/encryption_service.rb
solidus_nexio-0.6.2 app/services/solidus_nexio/encryption_service.rb
solidus_nexio-0.6.1 app/services/solidus_nexio/encryption_service.rb