Module: JwtAuthToken

Defined in:
lib/jwt_auth_token.rb

Instance Method Summary collapse

Instance Method Details

#is_jwt_valid_token?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
# File 'lib/jwt_auth_token.rb', line 36

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


22
23
24
25
26
27
28
29
30
# File 'lib/jwt_auth_token.rb', line 22

def jwt_cookie_name
  return @_jwt_cookie_name if @_jwt_cookie_name
  @_jwt_cookie_name = jwt_header_name
  if !Rails.env.production?
    @_jwt_cookie_name = "#{Rails.env}-#{jwt_header_name}"
  end
  @_jwt_cookie_name
  # @_jwt_cookie_name ||= "#{Rails.env}-#{jwt_header_name}"
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



32
33
34
# File 'lib/jwt_auth_token.rb', line 32

def jwt_header_token
  @_jwt_header_token ||= request.cookies[jwt_cookie_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



45
46
47
# File 'lib/jwt_auth_token.rb', line 45

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