module LpTokenAuth

Constants

VERSION

Current version of LpTokenAuth

Public Class Methods

check_id!(id) click to toggle source

Determines if the `id` provided is either a `String` or an `Integer` @param [Integer, String] id the identifier of the resource @raise [LpTokenAuth::Error] if the `id` is not a `String` or `Integer` @return [nil]

# File lib/lp_token_auth/core.rb, line 54
def check_id!(id)
  unless id.is_a?(String) || id.is_a?(Integer)
    raise LpTokenAuth::Error, "id must be a string or integer, you provided #{id}"
  end
end
config() { |config| ... } click to toggle source
# File lib/lp_token_auth.rb, line 2
def self.config
  @config ||= LpTokenAuth::Config.new
  if block_given?
    yield @config
  else
    @config
  end
end
decode!(token) click to toggle source

Decodes the JWT token @param [String] token the token to decode @raise [LpTokenAuth::Error] if the token is expired, or if any errors occur during decoding @return [Array] decoded token

# File lib/lp_token_auth/core.rb, line 35
def decode!(token)
  begin
    JWT.decode(
      token,
      LpTokenAuth.config.get_option(:secret),
      true,
      algorithm: LpTokenAuth.config.get_option(:algorithm)
    ).first
  rescue JWT::ExpiredSignature => msg
    raise LpTokenAuth::Error, msg
  rescue StandardError => msg
    raise LpTokenAuth::Error, msg
  end
end
issue_token(id, **payload) click to toggle source

Encodes the JWT token with the payload @param [Integer, String] id the identifier of the resource @param [Symbol=>String] payload keyword arguments required to create the token @raise [LpTokenAuth::Error] if the `id` is not a `String` or `Integer` @return [String] encoded token

# File lib/lp_token_auth/core.rb, line 14
def issue_token(id, **payload)

  check_id!(id)

  payload[:id] = id

  unless payload.has_key? :exp
    payload[:exp] = (Time.now + LpTokenAuth.config.get_option(:expires) * 60 * 60).to_i
  end

  JWT.encode(
    payload,
    LpTokenAuth.config.get_option(:secret),
    LpTokenAuth.config.get_option(:algorithm)
  )
end