Sha256: dabbefd5b0267b53be2c5db662b2dd1915ee9701ae40353bde668680ac624ee1

Contents?: true

Size: 795 Bytes

Versions: 9

Compression:

Stored size: 795 Bytes

Contents

module Ctws
  class JsonWebToken
    require 'jwt'
    
    # secret to encode and decode token
    HMAC_SECRET = Rails.application.secrets.secret_key_base

    def self.encode(payload, exp = Ctws.jwt_expiration_time)
      # set expiry to 24 hours from creation time
      payload[:exp] = exp.to_i
      # sign token with application secret
      JWT.encode(payload, HMAC_SECRET)
    end

    def self.decode(token)
      # get payload; first index in decoded Array
      body = JWT.decode(token, HMAC_SECRET)[0]
      HashWithIndifferentAccess.new body
      # rescue from expiry exception
    rescue JWT::ExpiredSignature, JWT::VerificationError => e
      # raise custom error to be handled by custom handler
      raise Ctws::ExceptionHandler::ExpiredSignature, e.message
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ctws-0.1.13.alpha app/lib/ctws/json_web_token.rb
ctws-0.1.12.alpha app/lib/ctws/json_web_token.rb
ctws-0.1.11.alpha app/lib/ctws/json_web_token.rb
ctws-0.1.10.alpha app/lib/ctws/json_web_token.rb
ctws-0.1.9.alpha app/lib/ctws/json_web_token.rb
ctws-0.1.8.alpha app/lib/ctws/json_web_token.rb
ctws-0.1.7.alpha app/lib/ctws/json_web_token.rb
ctws-0.1.6.alpha app/lib/ctws/json_web_token.rb
ctws-0.1.5.alpha app/lib/ctws/json_web_token.rb