13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/opp/base64_url.rb', line 13
def decode(value, length: nil)
unless value.is_a?(String) && ALPHABET.match?(value)
raise InvalidEncodingError, "invalid unpadded Base64url"
end
bytes = Base64.urlsafe_decode64(value.ljust((value.length + 3) / 4 * 4, "="))
raise InvalidEncodingError, "non-canonical Base64url" unless encode(bytes) == value
raise InvalidEncodingError, "expected #{length} bytes" if length && bytes.bytesize != length
bytes
rescue ArgumentError => error
raise InvalidEncodingError, error.message
end
|