Sha256: ae85c11619831d6b9aa7ec2c7233fd7a89937002f96fd738a7a914df7a4ad74b

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

# A public/private key pair.
class KeyPair < Sequel::Model
  plugin :authable, container: true
  plugin :belongs_to_user

  role :encrypt, :decrypt

  def public_json
    super.except(:public_keyid, :private_keyid)
  end

  def encrypt(value)
    public_key.encrypt_message(value)
  end
  
  def decrypt(value)
    private_key.decrypt_message(value)
  end
  
  def public_key
    @public_key ||= Slosilo::Key.new authz_api.variable(public_keyid).value
  end

  def private_key
    @private_key ||= Slosilo::Key.new authz_api.variable(private_keyid).value
  end

  # @private
  def after_initialize
    super
    
    unless self.public_keyid
      @key = Slosilo::Key.new
      
      private_key = authz_api.create_variable 'application/x-pem-file', 'rsa-private-key', owner_option
      private_key.add_value @key.key.to_pem

      public_key = authz_api.create_variable 'application/x-pem-file', 'rsa-public-key', owner_option
      public_key.add_value @key.key.public_key.to_pem
  
      self.private_keyid = private_key.id
      self.public_keyid = public_key.id
    end
  end

  # @private
  def after_create
    super
    
    # Encrypt role can execute the public key variable
    # Decrypt role can execute the private key variable
    authz_api.variable(public_keyid).resource.permit :execute, role('encrypt')
    authz_api.variable(private_keyid).resource.permit :execute, role('decrypt')
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
conjur-asset-key-pair-0.2.1 app/models/key_pair.rb