|
| 1 | +require 'rails_helper' |
| 2 | +require 'jwt' |
| 3 | +require 'json' |
| 4 | +require 'openssl' |
| 5 | + |
| 6 | +describe AlmaJwtValidator do |
| 7 | + let(:alma_institution_code) { '01UCS_BER' } |
| 8 | + let(:jwks_url) { "https://api-na.hosted.exlibrisgroup.com/auth/#{alma_institution_code}/jwks.json" } |
| 9 | + let(:expected_iss) { "https://api-na.hosted.exlibrisgroup.com/auth/#{alma_institution_code}" } |
| 10 | + |
| 11 | + # Generate an RSA key pair for testing |
| 12 | + let(:rsa_key) { OpenSSL::PKey::RSA.new(2048) } |
| 13 | + let(:kid) { 'test-key-id' } |
| 14 | + let(:test_payload) { { 'userName' => '10335026', 'iss' => expected_iss } } |
| 15 | + |
| 16 | + # Helper to create JWK from RSA public key |
| 17 | + def create_jwk(public_key, kid) |
| 18 | + n = Base64.urlsafe_encode64(public_key.n.to_s(2)).gsub(/=+$/, '') |
| 19 | + e = Base64.urlsafe_encode64(public_key.e.to_s(2)).gsub(/=+$/, '') |
| 20 | + { |
| 21 | + 'kty' => 'RSA', |
| 22 | + 'kid' => kid, |
| 23 | + 'use' => 'sig', |
| 24 | + 'alg' => 'RS256', |
| 25 | + 'n' => n, |
| 26 | + 'e' => e |
| 27 | + } |
| 28 | + end |
| 29 | + |
| 30 | + # Helper to generate a valid JWT |
| 31 | + def generate_jwt(payload, key, kid, algorithm = 'RS256') |
| 32 | + header = { 'kid' => kid, 'alg' => algorithm } |
| 33 | + JWT.encode(payload, key, algorithm, header) |
| 34 | + end |
| 35 | + |
| 36 | + before do |
| 37 | + jwk = create_jwk(rsa_key.public_key, kid) |
| 38 | + |
| 39 | + stub_request(:get, jwks_url) |
| 40 | + .to_return( |
| 41 | + status: 200, |
| 42 | + body: { 'keys' => [jwk] }.to_json, |
| 43 | + headers: { 'Content-Type' => 'application/json' } |
| 44 | + ) |
| 45 | + end |
| 46 | + |
| 47 | + describe '.decode_and_verify_jwt' do |
| 48 | + context 'with a valid JWT' do |
| 49 | + it 'returns the decoded payload' do |
| 50 | + token = generate_jwt(test_payload, rsa_key, kid) |
| 51 | + result = AlmaJwtValidator.decode_and_verify_jwt(token) |
| 52 | + |
| 53 | + expect(result).to be_an(Array) |
| 54 | + expect(result[0]['userName']).to eq('10335026') |
| 55 | + expect(result[1]['kid']).to eq(kid) |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + context 'with an invalid signature' do |
| 60 | + it 'raises JWT::VerificationError' do |
| 61 | + # Generate a token with a different key |
| 62 | + different_key = OpenSSL::PKey::RSA.new(2048) |
| 63 | + token = generate_jwt(test_payload, different_key, kid) |
| 64 | + |
| 65 | + expect do |
| 66 | + AlmaJwtValidator.decode_and_verify_jwt(token) |
| 67 | + end.to raise_error(JWT::VerificationError) |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + context 'with an unknown key id' do |
| 72 | + it 'raises JWT::VerificationError' do |
| 73 | + token = generate_jwt(test_payload, rsa_key, 'unknown-kid') |
| 74 | + |
| 75 | + expect do |
| 76 | + AlmaJwtValidator.decode_and_verify_jwt(token) |
| 77 | + end.to raise_error(JWT::VerificationError) |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + context 'with a malformed JWT' do |
| 82 | + it 'raises JWT::DecodeError' do |
| 83 | + expect do |
| 84 | + AlmaJwtValidator.decode_and_verify_jwt('not.a.jwt') |
| 85 | + end.to raise_error(JWT::DecodeError) |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + context 'when JWKS endpoint is unreachable' do |
| 90 | + it 'raises an error' do |
| 91 | + stub_request(:get, jwks_url).to_return(status: 500) |
| 92 | + token = generate_jwt(test_payload, rsa_key, kid) |
| 93 | + |
| 94 | + expect do |
| 95 | + AlmaJwtValidator.decode_and_verify_jwt(token) |
| 96 | + end.to raise_error(StandardError) |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | +end |
0 commit comments