Class: Identizer::Identity

Inherits:
Object
  • Object
show all
Defined in:
lib/identizer/identity.rb

Overview

A signed-in identity: a subject id, an email, and an arbitrary bag of additional claims (given_name, family_name, groups, …). ‘to_h` is what the provider encodes into id_tokens and returns from /userinfo.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email:, sub: nil, claims: {}) ⇒ Identity

Returns a new instance of Identity.



10
11
12
13
14
# File 'lib/identizer/identity.rb', line 10

def initialize(email:, sub: nil, claims: {})
  @email = email.to_s
  @sub = (sub || "identizer|#{@email}").to_s
  @claims = stringify(claims)
end

Instance Attribute Details

#claimsObject (readonly)

Returns the value of attribute claims.



8
9
10
# File 'lib/identizer/identity.rb', line 8

def claims
  @claims
end

#emailObject (readonly)

Returns the value of attribute email.



8
9
10
# File 'lib/identizer/identity.rb', line 8

def email
  @email
end

#subObject (readonly)

Returns the value of attribute sub.



8
9
10
# File 'lib/identizer/identity.rb', line 8

def sub
  @sub
end

Class Method Details

.from(value) ⇒ Object

Coerce a Hash, String (email) or Identity into an Identity.



17
18
19
20
21
22
23
# File 'lib/identizer/identity.rb', line 17

def self.from(value)
  return value if value.is_a?(Identity)

  attrs = value.is_a?(Hash) ? value.transform_keys(&:to_sym) : { email: value }
  claims = attrs[:claims] || attrs.except(:email, :sub)
  new(email: attrs[:email], sub: attrs[:sub], claims: claims)
end

Instance Method Details

#==(other) ⇒ Object



29
30
31
# File 'lib/identizer/identity.rb', line 29

def ==(other)
  other.is_a?(Identity) && other.to_h == to_h
end

#to_hObject



25
26
27
# File 'lib/identizer/identity.rb', line 25

def to_h
  { "sub" => sub, "email" => email }.merge(claims)
end