Class: Paseto::V2::Public::PublicKey

Inherits:
Object
  • Object
show all
Includes:
Encoder
Defined in:
lib/paseto/public.rb

Overview

public-key used for signing and verifing

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ PublicKey

Returns a new instance of PublicKey.



66
67
68
69
# File 'lib/paseto/public.rb', line 66

def initialize(key)
  @key = key
  @nacl = RbNaCl::VerifyKey.new(key)
end

Class Method Details

.decode64(encoded_key) ⇒ Object



62
63
64
# File 'lib/paseto/public.rb', line 62

def self.decode64(encoded_key)
  new(Paseto.decode64(encoded_key))
end

Instance Method Details

#encode64Object



71
72
73
# File 'lib/paseto/public.rb', line 71

def encode64
  Paseto.encode64(@key)
end

#verify(token, footer = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/paseto/public.rb', line 75

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