Module: OmniAuth::Identity::Models::Rom

Defined in:
lib/omniauth/identity/models/rom.rb

Overview

ROM adapter for OmniAuth::Identity
This provides a reusable adapter that makes ROM entities compatible with OmniAuth::Identity

Usage:
class Identity
include OmniAuth::Identity::Models::Rom

# Configure the ROM container and relation using the DSL (no `self.`):
rom_container -> { MyDatabase.rom } # accepts a proc or a container object
rom_relation_name :identities # optional, defaults to :identities
owner_relation_name :owners  # optional, for loading associated owner
# Uses OmniAuth::Identity::Model.auth_key to set the auth key (defaults to :email)
auth_key :email
# Optional: override the password digest field name (defaults to :password_digest)
password_field :password_digest   end

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#emailObject (readonly)

Instance shape matches other adapters for downstream usage



111
112
113
# File 'lib/omniauth/identity/models/rom.rb', line 111

def email
  @email
end

#infoObject (readonly)

Instance shape matches other adapters for downstream usage



111
112
113
# File 'lib/omniauth/identity/models/rom.rb', line 111

def info
  @info
end

#nameObject (readonly)

Instance shape matches other adapters for downstream usage



111
112
113
# File 'lib/omniauth/identity/models/rom.rb', line 111

def name
  @name
end

#ownerObject (readonly)

Instance shape matches other adapters for downstream usage



111
112
113
# File 'lib/omniauth/identity/models/rom.rb', line 111

def owner
  @owner
end

#uidObject (readonly)

Instance shape matches other adapters for downstream usage



111
112
113
# File 'lib/omniauth/identity/models/rom.rb', line 111

def uid
  @uid
end

Class Method Details

.included(base) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/omniauth/identity/models/rom.rb', line 28

def self.included(base)
  # Align with other adapters: rely on OmniAuth::Identity::Model for API
  base.include(::OmniAuth::Identity::Model)
  base.extend(ClassMethods)

  base.class_eval do
    # OmniAuth::Identity required instance API
    # Authenticates the instance with the provided password
    # Returns self on success, false otherwise (to match Model.authenticate contract)
    def authenticate(password)
      digest_key = self.class.password_field
      password_digest = @identity_data[digest_key]
      return false unless password_digest

      begin
        BCrypt::Password.new(password_digest) == password && self
      rescue BCrypt::Errors::InvalidHash
        false
      end
    end
  end
end

Instance Method Details

#[](key) ⇒ Object

Hash-like access to underlying ROM tuple



135
136
137
# File 'lib/omniauth/identity/models/rom.rb', line 135

def [](key)
  @identity_data[key]
end

#initialize(identity_data, owner_data = nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/omniauth/identity/models/rom.rb', line 113

def initialize(identity_data, owner_data = nil)
  @identity_data = identity_data
  @owner_data = owner_data
  @uid = identity_data[:id]
  @email = identity_data[:email]

  # Prefer owner name if available, else fall back to email
  @name = if owner_data && owner_data[:name]
    owner_data[:name]
  else
    identity_data[:email]
  end

  @info = {
    "email" => @email,
    "name" => @name,
  }

  @owner = owner_data
end

#owner_idObject

Convenience accessor for owner id



140
141
142
# File 'lib/omniauth/identity/models/rom.rb', line 140

def owner_id
  @identity_data[:owner_id]
end

#to_hashObject

OmniAuth info hash



145
146
147
# File 'lib/omniauth/identity/models/rom.rb', line 145

def to_hash
  @info
end