Module: JwtAuthToken

Defined in:
lib/jwt_auth_token.rb

Instance Method Summary collapse

Instance Method Details

#is_jwt_valid_token?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
# File 'lib/jwt_auth_token.rb', line 26

def is_jwt_valid_token?
  begin
    @decoded_token = JWT.decode(jwt_header_token, jwt_hmac_secret, true, { :algorithm => jwt_algorithm })[0]
    return validate_keys
  rescue Exception => e
    return false
  end  
end

#jwt_algorithmObject



9
10
11
# File 'lib/jwt_auth_token.rb', line 9

def jwt_algorithm
  @_jwt_algorithm ||= 'HS512'
end

#jwt_header_nameObject



18
19
20
# File 'lib/jwt_auth_token.rb', line 18

def jwt_header_name
  @_jwt_header_name ||= "embibe-token"
end

#jwt_header_tokenObject



22
23
24
# File 'lib/jwt_auth_token.rb', line 22

def jwt_header_token
  @_jwt_header_token ||= request.cookies[jwt_header_name] || request.headers[jwt_header_name] rescue nil
end

#jwt_hmac_secretObject



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

def jwt_hmac_secret
  @_jwt_hmac_secret ||= Rails.application.secrets[:secret_key_base]
end

#jwt_set_header(data) ⇒ Object



13
14
15
16
# File 'lib/jwt_auth_token.rb', line 13

def jwt_set_header(data)
  encoded_token = JWT.encode(data,jwt_hmac_secret,jwt_algorithm)
  response.set_header(jwt_header_name, encoded_token)
end

#validate_keysObject



35
36
37
# File 'lib/jwt_auth_token.rb', line 35

def validate_keys
  !!@_validate_keys ||= (@decoded_token.keys && ["id", "email"]).any?
end