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.



78
79
80
81
# File 'lib/paseto/public.rb', line 78

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

Class Method Details

.decode64(encoded_key) ⇒ Object



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

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

.decode_hex(encoded_key) ⇒ Object



74
75
76
# File 'lib/paseto/public.rb', line 74

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

Instance Method Details

#encode64Object



83
84
85
# File 'lib/paseto/public.rb', line 83

def encode64
  Paseto.encode64(@key)
end

#encode_hexObject



87
88
89
# File 'lib/paseto/public.rb', line 87

def encode_hex
  Paseto.encode_hex(@key)
end

#verify(token, footer = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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