Class: BsJwt::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/bs_jwt/authentication.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Authentication

Returns a new instance of Authentication.



19
20
21
22
23
24
25
26
27
28
# File 'lib/bs_jwt/authentication.rb', line 19

def initialize(attributes = {})
  attributes = attributes.with_indifferent_access
  @roles = attributes[:roles] || []
  @display_name = attributes[:display_name]
  @token = attributes[:token]
  @expires_at = attributes[:expires_at]
  @email = attributes[:email]
  @user_id = attributes[:user_id]
  @issued_at = attributes[:issued_at]
end

Instance Attribute Details

#display_nameObject

Returns the value of attribute display_name.



5
6
7
# File 'lib/bs_jwt/authentication.rb', line 5

def display_name
  @display_name
end

#emailObject

Returns the value of attribute email.



5
6
7
# File 'lib/bs_jwt/authentication.rb', line 5

def email
  @email
end

#expires_atObject

Returns the value of attribute expires_at.



5
6
7
# File 'lib/bs_jwt/authentication.rb', line 5

def expires_at
  @expires_at
end

#issued_atObject

Returns the value of attribute issued_at.



5
6
7
# File 'lib/bs_jwt/authentication.rb', line 5

def issued_at
  @issued_at
end

#rolesObject

Returns the value of attribute roles.



5
6
7
# File 'lib/bs_jwt/authentication.rb', line 5

def roles
  @roles
end

#tokenObject

Returns the value of attribute token.



5
6
7
# File 'lib/bs_jwt/authentication.rb', line 5

def token
  @token
end

#user_idObject

Returns the value of attribute user_id.



5
6
7
# File 'lib/bs_jwt/authentication.rb', line 5

def user_id
  @user_id
end

Class Method Details

.from_jwt_payload(payload, jwt_token) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/bs_jwt/authentication.rb', line 7

def self.from_jwt_payload(payload, jwt_token)
  new(
    roles: payload['https://buddy.buddyandselly.com/roles'],
    display_name: payload['nickname'],
    token: jwt_token,
    expires_at: Time.at(payload['exp']),
    email: payload['name'],
    user_id: payload['sub'],
    issued_at: Time.at(payload['iat'])
  )
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/bs_jwt/authentication.rb', line 34

def expired?
  Time.now >= expires_at
end

#has_role?(role) ⇒ Boolean

rubocop:disable Naming/PredicateName

Returns:

  • (Boolean)


30
31
32
# File 'lib/bs_jwt/authentication.rb', line 30

def has_role?(role) # rubocop:disable Naming/PredicateName
  roles.include?(role)
end

#to_hObject



38
39
40
# File 'lib/bs_jwt/authentication.rb', line 38

def to_h
  instance_values
end