Skip to content

Commit

Permalink
Allow key to be passed as Bytes everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
m-o-e committed Jul 3, 2022
1 parent 689a6e0 commit d6f7673
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 13 deletions.
8 changes: 4 additions & 4 deletions docs/Suzuri.html
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ <h2>
<ul class="list-summary">

<li class="entry-summary">
<a href="#decode%28token%3AString%2Ckey%3AString%2Cttl%3ATime%3A%3ASpan%3F%3Dnil%29%3AToken-class-method" class="signature"><strong>.decode</strong>(token : String, key : String, ttl : Time::Span? = <span class="n">nil</span>) : Token</a>
<a href="#decode%28token%3AString%2Ckey%3AString%7CBytes%2Cttl%3ATime%3A%3ASpan%3F%3Dnil%29%3AToken-class-method" class="signature"><strong>.decode</strong>(token : String, key : String | Bytes, ttl : Time::Span? = <span class="n">nil</span>) : Token</a>

<div class="summary"><p>Decodes a Suzuri token.</p></div>

Expand Down Expand Up @@ -210,12 +210,12 @@ <h2>
Class Method Detail
</h2>

<div class="entry-detail" id="decode(token:String,key:String,ttl:Time::Span?=nil):Token-class-method">
<div class="entry-detail" id="decode(token:String,key:String|Bytes,ttl:Time::Span?=nil):Token-class-method">
<div class="signature">

def self.<strong>decode</strong>(token : String, key : String, ttl : Time::Span? = <span class="n">nil</span>) : <a href="Suzuri/Token.html">Token</a>
def self.<strong>decode</strong>(token : String, key : String | Bytes, ttl : Time::Span? = <span class="n">nil</span>) : <a href="Suzuri/Token.html">Token</a>

<a class="method-permalink" href="#decode%28token%3AString%2Ckey%3AString%2Cttl%3ATime%3A%3ASpan%3F%3Dnil%29%3AToken-class-method">#</a>
<a class="method-permalink" href="#decode%28token%3AString%2Ckey%3AString%7CBytes%2Cttl%3ATime%3A%3ASpan%3F%3Dnil%29%3AToken-class-method">#</a>
</div>

<div class="doc">
Expand Down
2 changes: 1 addition & 1 deletion docs/index.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/search-index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: suzuri
version: 2.0.1
version: 2.0.2

authors:
- moe <[email protected]>
Expand Down
20 changes: 18 additions & 2 deletions spec/suzuri_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,41 @@ describe Suzuri do
Suzuri.decode(token_c, TEST_KEY).to_s.should eq payload
end

it "raises on encode when key is not 32 bytes long" do
it "raises n encode when key is not 32 bytes long" do
expect_raises(ArgumentError, /key size mismatch/) do
Suzuri.encode("hello world", "too short")
end

expect_raises(ArgumentError, /key size mismatch/) do
Suzuri.encode("hello world", TEST_KEY + "too long")
end
end

expect_raises(ArgumentError, /key size mismatch/) do
Suzuri.encode("hello world", "too short".to_slice)
end

expect_raises(ArgumentError, /key size mismatch/) do
Suzuri.encode("hello world", (TEST_KEY + "too long").to_slice)
end
end

it "raises on decode when key is not 32 bytes long" do
token = Suzuri.encode("hello world", TEST_KEY)
expect_raises(ArgumentError, /key size mismatch/) do
Suzuri.decode(token, "too short")
end

expect_raises(ArgumentError, /key size mismatch/) do
Suzuri.decode(token, "too short".to_slice)
end

expect_raises(ArgumentError, /key size mismatch/) do
Suzuri.decode(token, TEST_KEY + "too long")
end

expect_raises(ArgumentError, /key size mismatch/) do
Suzuri.decode(token, (TEST_KEY + "too long").to_slice)
end
end

it "includes a timestamp with encoded tokens" do
Expand Down
2 changes: 1 addition & 1 deletion src/suzuri.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module Suzuri
# # Decode with a ttl constraint
# Suzuri.decode(token, KEY, 5.minutes) # => Suzuri::Error::TokenExpired
# ```
def self.decode(token : String, key : String, ttl : Time::Span? = nil) : Token
def self.decode(token : String, key : String | Bytes, ttl : Time::Span? = nil) : Token
begin
raw = Base64.decode(token)
rescue ex : Exception
Expand Down
6 changes: 3 additions & 3 deletions src/suzuri/json_serializable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ require "../suzuri"
# ```
module JSON::Serializable
# :nodoc:
def to_suzuri(key : String,
def to_suzuri(key : String | Bytes,
timestamp : Time = Time.utc,
compress_level = 3,
compress_threshold : UInt64 = 512)
Expand All @@ -47,13 +47,13 @@ module JSON::Serializable

macro included
# :nodoc:
def self.from_suzuri(token : String, key : String, ttl : Time::Span? = nil)
def self.from_suzuri(token : String, key : String | Bytes, ttl : Time::Span? = nil)
token = Suzuri.decode(token, key, ttl)
from_json(token.to_s)
end

# :nodoc:
def self.from_suzuri_with_timestamp(token : String, key : String, ttl : Time::Span? = nil)
def self.from_suzuri_with_timestamp(token : String, key : String | Bytes, ttl : Time::Span? = nil)
token = Suzuri.decode(token, key, ttl)
{ from_json(token.to_s), token.timestamp }
end
Expand Down

0 comments on commit d6f7673

Please sign in to comment.