module Garage::Jwt::Utils

Public Instance Methods

decode_token(token, token_type) click to toggle source
# File lib/garage/jwt/utils.rb, line 14
def decode_token(token, token_type)
  payload, _ = JWT.decode(token, public_key, verify?, decoding_options)
  { token: token,
    token_type: token_type,
    scope: payload["scope"],
    application_id: payload["aud"],
    resource_owner_id: payload["sub"],
    expired_at: payload["exp"],
    revoked_at: nil
  }
rescue JWT::DecodeError => e
  nil
end
encode_token(resource_owner_id:, application_id:, expired_at:, scope:) click to toggle source
# File lib/garage/jwt/utils.rb, line 4
def encode_token(resource_owner_id:, application_id:, expired_at:, scope:)
  payload = {
    sub: resource_owner_id,
    aud: application_id,
    exp: expired_at.to_i,
    scope: (scope.is_a?(Array) ? scope.join(" ") : scope)
  }
  JWT.encode(payload, private_key, algorithm.type)
end

Private Instance Methods

algorithm() click to toggle source
# File lib/garage/jwt/utils.rb, line 34
def algorithm
  configuration.algorithm
end
common_key() click to toggle source
# File lib/garage/jwt/utils.rb, line 46
def common_key
  algorithm.need_common_key? ? configuration.common_key : nil
end
configuration() click to toggle source
# File lib/garage/jwt/utils.rb, line 30
def configuration
  Garage::Jwt.configuration
end
decoding_options() click to toggle source
# File lib/garage/jwt/utils.rb, line 50
def decoding_options
  { algorithm: algorithm.type, verify_expiration: false }
end
private_key() click to toggle source
# File lib/garage/jwt/utils.rb, line 42
def private_key
  algorithm.need_public_key? ? configuration.private_key : common_key
end
public_key() click to toggle source
# File lib/garage/jwt/utils.rb, line 38
def public_key
  algorithm.need_public_key? ? configuration.public_key : common_key
end
verify?() click to toggle source
# File lib/garage/jwt/utils.rb, line 54
def verify?
  !algorithm.none?
end