Sha256: 798705b5c78b98649e9685692a1932f8fa6c539981f39ee8e344ce2934c69ebd

Contents?: true

Size: 1.63 KB

Versions: 5

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require "bundler/inline"
require "stringio"

gemfile do
  source "https://rubygems.org"
  gem "sqlite3"
  gem "sequel"
  gem "pry-meta"
  gem "attr_keyring",
      path: File.expand_path("..", __dir__)
end

Sequel.extension :migration

DB = Sequel.sqlite

Sequel.migration do
  up do
    create_table(:users) do
      primary_key :id
      String :encrypted_email, null: false
      String :email_digest, null: false
      Integer :keyring_id, null: false
    end
  end
end.apply(DB, :up)

class User < Sequel::Model
  include AttrKeyring.sequel
  plugin :validation_helpers

  attr_keyring "1" => "QSXyoiRDPoJmfkJUZ4hJeQ=="
  attr_encrypt :email

  def validate
    super
    validates_unique :email_digest
  end
end

john = User.create(email: "john@example.com")

puts "👱‍ attributes"
puts john.email
puts john.email_digest
puts john.encrypted_email
puts john.keyring_id
puts

puts "🔁 rotate key"
User.keyring["2"] = "r6AfOeilPDJomFsiOXLdfQ=="
puts john.keyring_rotate!
puts

puts "👱‍ attributes (after key rotation)"
puts john.email
puts john.email_digest
puts john.encrypted_email
puts john.keyring_id
puts

puts "👨 assign new email"
puts john.update(email: "jdoe@example.com")
puts john.email
puts john.email_digest
puts john.encrypted_email
puts john.keyring_id
puts

puts "🔎 search by email digest"
user = User.first(email_digest: Digest::SHA1.hexdigest("jdoe@example.com"))
puts user.email
puts user == john
puts

puts "❌ duplicated email address"
copycat = User.new(email: john.email)
puts copycat.valid?
p copycat.errors.to_h
puts

puts "🔑 retrieve latest key from keyring"
puts User.keyring.current_key

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
attr_keyring-0.7.0 examples/sequel_sample.rb
attr_keyring-0.6.1 examples/sequel_sample.rb
attr_keyring-0.6.0 examples/sequel_sample.rb
attr_keyring-0.5.4 examples/sequel_sample.rb
attr_keyring-0.5.3 examples/sequel_sample.rb