Class: YiffSpace::Auth::AuthInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/yiffspace/auth/auth_info.rb,
lib/yiffspace/auth/auth_info/anonymous.rb

Defined Under Namespace

Classes: Anonymous

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, entitlements:, roles:, token:) ⇒ AuthInfo

Returns a new instance of AuthInfo.

Parameters:

  • id

    String

  • roles

    Array(String)

  • entitlements

    Array(String)

  • token

    OpenIDConnect::AccessToken

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
# File 'lib/yiffspace/auth/auth_info.rb', line 12

def initialize(id:, entitlements:, roles:, token:)
  raise(ArgumentError, "no id provided") if id.blank?
  raise(ArgumentError, "no token provided") if token.blank?

  @id           = id
  @token        = token
  @entitlements = Array(entitlements)
  @roles        = Array(roles)
  @permissions  = Permissions.new(@entitlements)
end

Instance Attribute Details

#entitlementsObject (readonly)

Returns the value of attribute entitlements.



6
7
8
# File 'lib/yiffspace/auth/auth_info.rb', line 6

def entitlements
  @entitlements
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/yiffspace/auth/auth_info.rb', line 6

def id
  @id
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



6
7
8
# File 'lib/yiffspace/auth/auth_info.rb', line 6

def permissions
  @permissions
end

#rolesObject (readonly)

Returns the value of attribute roles.



6
7
8
# File 'lib/yiffspace/auth/auth_info.rb', line 6

def roles
  @roles
end

#tokenObject (readonly)

Returns the value of attribute token.



6
7
8
# File 'lib/yiffspace/auth/auth_info.rb', line 6

def token
  @token
end

Class Method Details

.from_json(data) ⇒ Object

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/yiffspace/auth/auth_info.rb', line 53

def self.from_json(data)
  raise(ArgumentError, "invalid data") if data.blank?

  data = JSON.parse(data) if data.is_a?(String)
  data = ::YiffSpace::Utils::OpenHash.from(data)

  new(
    id:           data.id,
    token:        ::YiffSpace::Auth.unserialize_token(data.token),
    entitlements: data.entitlements,
    roles:        data.roles,
  )
end

.from_session(data) ⇒ Object



67
68
69
70
71
# File 'lib/yiffspace/auth/auth_info.rb', line 67

def self.from_session(data)
  return nil if data.blank?

  from_json(data)
end

Instance Method Details

#anonymous?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/yiffspace/auth/auth_info.rb', line 23

def anonymous?
  false
end

#blank?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/yiffspace/auth/auth_info.rb', line 32

def blank?
  false
end

#has_permission?(name) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/yiffspace/auth/auth_info.rb', line 36

def has_permission?(name)
  permissions.include?(name.to_s)
end

#present?Boolean

this feels wrong, but it hopefully shouldn’t break anything

Returns:

  • (Boolean)


28
29
30
# File 'lib/yiffspace/auth/auth_info.rb', line 28

def present?
  true
end

#serializable_hashObject



40
41
42
43
44
45
46
47
# File 'lib/yiffspace/auth/auth_info.rb', line 40

def serializable_hash(*)
  {
    "id"           => id,
    "token"        => ::YiffSpace::Auth.serialize_token(token),
    "entitlements" => entitlements,
    "roles"        => roles,
  }
end

#to_sessionObject



49
50
51
# File 'lib/yiffspace/auth/auth_info.rb', line 49

def to_session
  serializable_hash
end