class Paseto::V2::Public::PublicKey

public-key used for signing and verifing

Public Class Methods

decode64(encoded_key) click to toggle source
# File lib/paseto/public.rb, line 70
def self.decode64(encoded_key)
  new(Paseto.decode64(encoded_key))
end
decode_hex(encoded_key) click to toggle source
# File lib/paseto/public.rb, line 74
def self.decode_hex(encoded_key)
  new(Paseto.decode_hex(encoded_key))
end
new(key) click to toggle source
# File lib/paseto/public.rb, line 78
def initialize(key)
  @key = key
  @nacl = RbNaCl::VerifyKey.new(key)
end

Public Instance Methods

encode64() click to toggle source
# File lib/paseto/public.rb, line 83
def encode64
  Paseto.encode64(@key)
end
encode_hex() click to toggle source
# File lib/paseto/public.rb, line 87
def encode_hex
  Paseto.encode_hex(@key)
end
verify(token, footer = nil) click to toggle source
# File lib/paseto/public.rb, line 91
def verify(token, footer = nil)
  footer ||= token.footer if token.is_a? Paseto::Token
  footer ||= EMPTY_FOOTER

  parsed = Paseto.verify_token(token, HEADER, footer)

  decoded_message = parsed.payload[0..-(SIGNATURE_BYTES + 1)]
  signature = parsed.payload[-SIGNATURE_BYTES..-1]

  if decoded_message.nil? || signature.nil?
    raise BadMessageError, 'Unable to process message'
  end

  begin
    data = encode_message(decoded_message, footer)
    @nacl.verify(signature, data)
    decoded_message
  rescue RbNaCl::BadSignatureError
    raise AuthenticationError, 'Token signature invalid'
  end
end