1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
require 'spec_helper'
describe JSON::JWS do
let(:alg) { :none }
let(:jwt) do
_jwt_ = JSON::JWT.new claims
_jwt_.alg = alg
_jwt_
end
let(:jws) { JSON::JWS.new jwt }
let(:signed) { jws.sign! private_key_or_secret }
let(:decoded) { JSON::JWT.decode signed.to_s, public_key_or_secret }
let(:claims) do
{
iss: 'joe',
exp: 1300819380,
:'http://example.com/is_root' => true
}
end
let(:expected_signature) do
{
:HS256 => 'DyuTgO2Ggb5nrhkkhI-RjVYIBe3o8oL4ijkAn94YPxQ',
:HS384 => 'a5-7rr61TG8Snv9xxJ7l064ky-SCq1Mswe9t8HEorvoc_nnfIeUy9WQCLMIli34R',
:HS512 => 'ce-GlHDaNwaHfmAFRGp3QPP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H7f6A8KZdcOm8AnEjullcZs8_OWGnW43p1qrxoBRSivb9pqQ'
}
end
shared_examples_for :jwt_with_alg do
it { should == jwt }
its(:header) { should == jwt.header }
end
context 'before sign' do
subject { jws }
it_behaves_like :jwt_with_alg
its(:signature) { should be_nil }
end
describe '#content_type' do
it do
jws.content_type.should == 'application/jose'
end
end
describe '#sign!' do
shared_examples_for :generate_expected_signature do
it do
UrlSafeBase64.encode64(signed.signature).should == expected_signature[alg]
end
end
subject { signed }
[:HS256, :HS384, :HS512].each do |algorithm|
describe algorithm do
let(:alg) { algorithm }
context 'when String key given' do
let(:private_key_or_secret) { shared_secret }
it_behaves_like :jwt_with_alg
it_behaves_like :generate_expected_signature
end
context 'when JSON::JWK key given' do
let(:private_key_or_secret) { JSON::JWK.new shared_secret }
it_behaves_like :jwt_with_alg
it_behaves_like :generate_expected_signature
end
end
end
[:RS256, :RS384, :RS512].each do |algorithm|
describe algorithm do
let(:alg) { algorithm }
context 'when OpenSSL::PKey::RSA key given' do
let(:private_key_or_secret) { private_key }
it_behaves_like :jwt_with_alg
it_behaves_like :generate_expected_signature
end
context 'when JSON::JWK key given' do
let(:private_key_or_secret) { JSON::JWK.new private_key }
it_behaves_like :jwt_with_alg
it_behaves_like :generate_expected_signature
end
end
end
[:ES256, :ES384, :ES512].each do |algorithm|
describe algorithm do
let(:alg) { algorithm }
shared_examples_for :self_verifiable do
it 'should be self-verifiable' do
expect do
JSON::JWT.decode(
JSON::JWT.new(claims).sign(
private_key_or_secret, algorithm
).to_s, public_key_or_secret
)
end.not_to raise_error
end
end
context 'when OpenSSL::PKey::EC key given' do
let(:private_key_or_secret) { private_key :ecdsa, digest_length: algorithm.to_s[2,3].to_i }
let(:public_key_or_secret) { public_key :ecdsa, digest_length: algorithm.to_s[2,3].to_i }
it_behaves_like :jwt_with_alg
it_behaves_like :self_verifiable
end
context 'when JSON::JWK key given' do
let(:private_key_or_secret) { JSON::JWK.new(private_key :ecdsa, digest_length: algorithm.to_s[2,3].to_i) }
let(:public_key_or_secret) { JSON::JWK.new(public_key :ecdsa, digest_length: algorithm.to_s[2,3].to_i) }
it_behaves_like :jwt_with_alg
it_behaves_like :self_verifiable
end
end
end
context 'when JSON::JWK::Set key given' do
let(:alg) { :HS256 }
let(:kid) { 'kid' }
let(:jwks) do
jwk = JSON::JWK.new shared_secret, kid: kid
JSON::JWK::Set.new jwk, JSON::JWK.new('another')
end
let(:signed) { jws.sign!(jwks) }
context 'when jwk is found by given kid' do
before { jws.kid = kid }
it { should == jws.sign!('secret') }
end
context 'otherwise' do
it do
expect do
subject
end.to raise_error JSON::JWK::Set::KidNotFound
end
end
end
describe 'unknown algorithm' do
let(:alg) { :unknown }
it do
expect do
jws.sign! 'key'
end.to raise_error JSON::JWS::UnexpectedAlgorithm
end
end
end
describe '#verify!' do
shared_examples_for :success_signature_verification do
it do
expect { decoded }.not_to raise_error
decoded.should be_a JSON::JWT
end
describe 'header' do
let(:header) { decoded.header }
it 'should be parsed successfully' do
header[:typ].should == 'JWT'
header[:alg].should == alg.to_s
end
end
describe 'claims' do
it 'should be parsed successfully' do
decoded[:iss].should == 'joe'
decoded[:exp].should == 1300819380
decoded[:'http://example.com/is_root'] == true
end
end
end
subject { decoded }
[:HS256, :HS384, :HS512].each do |algorithm|
describe algorithm do
let(:alg) { algorithm }
let(:private_key_or_secret) { shared_secret }
context 'when String key given' do
let(:public_key_or_secret) { shared_secret }
it_behaves_like :success_signature_verification
end
context 'when JSON::JWK key given' do
let(:public_key_or_secret) { JSON::JWK.new shared_secret }
it_behaves_like :success_signature_verification
end
end
end
[:RS256, :RS384, :RS512].each do |algorithm|
describe algorithm do
let(:alg) { algorithm }
let(:private_key_or_secret) { private_key }
context 'when OpenSSL::PKey::RSA key given' do
let(:public_key_or_secret) { public_key }
it_behaves_like :success_signature_verification
end
context 'when JSON::JWK key given' do
let(:public_key_or_secret) { JSON::JWK.new public_key }
it_behaves_like :success_signature_verification
end
end
end
[:ES256, :ES384, :ES512].each do |algorithm|
describe algorithm do
let(:alg) { algorithm }
let(:private_key_or_secret) { private_key :ecdsa, digest_length: algorithm.to_s[2,3].to_i }
context 'when OpenSSL::PKey::EC key given' do
let(:public_key_or_secret) { public_key :ecdsa, digest_length: algorithm.to_s[2,3].to_i }
it_behaves_like :success_signature_verification
end
context 'when JSON::JWK key given' do
let(:public_key_or_secret) { JSON::JWK.new public_key(:ecdsa, digest_length: algorithm.to_s[2,3].to_i) }
it_behaves_like :success_signature_verification
end
end
end
context 'when JSON::JWK::Set key given' do
subject { JSON::JWT.decode signed.to_s, jwks }
let(:alg) { :HS256 }
let(:kid) { 'kid' }
let(:jwks) do
jwk = JSON::JWK.new shared_secret, kid: kid
JSON::JWK::Set.new jwk, JSON::JWK.new('another')
end
let(:signed) { jws.sign!(jwks) }
context 'when jwk is found by given kid' do
before { jws.kid = kid }
it { should == signed }
end
context 'otherwise' do
it do
expect do
subject
end.to raise_error JSON::JWK::Set::KidNotFound
end
end
end
describe 'unknown algorithm' do
let(:alg) { :unknown }
it do
expect do
jws.verify! 'key'
end.to raise_error JSON::JWS::UnexpectedAlgorithm
end
end
end
describe 'to_json' do
let(:alg) { :RS256 }
let(:private_key_or_secret) { private_key }
context 'as default' do
it 'should JSONize payload' do
jws.to_json.should == claims.to_json
end
end
context 'when syntax option given' do
context 'when general' do
it 'should return General JWS JSON Serialization' do
signed.to_json(syntax: :general).should == {
payload: UrlSafeBase64.encode64(claims.to_json),
signatures: [{
protected: UrlSafeBase64.encode64(signed.header.to_json),
signature: UrlSafeBase64.encode64(signed.signature)
}]
}.to_json
end
context 'when not signed yet' do
it 'should not fail' do
jws.to_json(syntax: :general).should == {
payload: UrlSafeBase64.encode64(claims.to_json),
signatures: [{
protected: UrlSafeBase64.encode64(jws.header.to_json),
signature: UrlSafeBase64.encode64('')
}]
}.to_json
end
end
end
context 'when flattened' do
it 'should return Flattened JWS JSON Serialization' do
signed.to_json(syntax: :flattened).should == {
protected: UrlSafeBase64.encode64(signed.header.to_json),
payload: UrlSafeBase64.encode64(claims.to_json),
signature: UrlSafeBase64.encode64(signed.signature)
}.to_json
end
context 'when not signed yet' do
it 'should not fail' do
jws.to_json(syntax: :flattened).should == {
protected: UrlSafeBase64.encode64(jws.header.to_json),
payload: UrlSafeBase64.encode64(claims.to_json),
signature: UrlSafeBase64.encode64('')
}.to_json
end
end
end
end
end
end
|