-
Notifications
You must be signed in to change notification settings - Fork 0
validating jwt signature from alma #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| require 'jwt' | ||
| require 'net/http' | ||
| require 'json' | ||
|
|
||
| module AlmaJwtValidator | ||
| JWKS_URL = 'https://api-na.hosted.exlibrisgroup.com/auth/01UCS_BER/jwks.json'.freeze | ||
| EXPECTED_ISS = 'Prima'.freeze | ||
|
|
||
| module_function | ||
|
|
||
| def jwk_set | ||
| Rails.cache.fetch('jwks_set', expires_in: 4.hour) do | ||
| jwks_raw = Net::HTTP.get(URI(JWKS_URL)) | ||
| jwks_keys = JSON.parse(jwks_raw)['keys'] | ||
| JWT::JWK::Set.new(jwks_keys) | ||
| end | ||
| end | ||
|
|
||
| def decode_and_verify_jwt(token) | ||
| options = { | ||
| algorithm: 'RS256', | ||
| verify_expiration: true, | ||
| verify_aud: false, | ||
|
davezuckerman marked this conversation as resolved.
|
||
| verify_iss: true, | ||
| iss: EXPECTED_ISS, | ||
| jwks: jwk_set | ||
| } | ||
|
|
||
| JWT.decode(token, nil, true, options) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose this wasn't clear from my earlier comment, but I'm curious about why we're using |
||
| end | ||
|
anarchivist marked this conversation as resolved.
|
||
| end | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, there's a lot in this file that seems to duplicate what's in |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| require 'rails_helper' | ||
| require 'jwt' | ||
| require 'json' | ||
| require 'openssl' | ||
|
|
||
| describe AlmaJwtValidator do | ||
| let(:alma_institution_code) { '01UCS_BER' } | ||
| let(:jwks_url) { "https://api-na.hosted.exlibrisgroup.com/auth/#{alma_institution_code}/jwks.json" } | ||
| let(:expected_iss) { 'Prima' } | ||
|
|
||
| # Generate an RSA key pair for testing | ||
| let(:rsa_key) { OpenSSL::PKey::RSA.new(2048) } | ||
| let(:kid) { 'test-key-id' } | ||
| let(:test_payload) { { 'userName' => '10335026', 'iss' => expected_iss } } | ||
|
|
||
| # Helper to create JWK hash from RSA key using JWT::JWK | ||
| def create_jwk_hash(key, kid) | ||
| jwk = JWT::JWK.new(key, kid: kid) | ||
| jwk.export | ||
| end | ||
|
|
||
| # Helper to generate a valid JWT | ||
| def generate_jwt(payload, key, kid, algorithm = 'RS256') | ||
| header = { 'kid' => kid, 'alg' => algorithm } | ||
| JWT.encode(payload, key, algorithm, header) | ||
| end | ||
|
|
||
| before do | ||
| jwk = create_jwk_hash(rsa_key, kid) | ||
|
|
||
| stub_request(:get, jwks_url) | ||
| .to_return( | ||
| status: 200, | ||
| body: { 'keys' => [jwk] }.to_json, | ||
| headers: { 'Content-Type' => 'application/json' } | ||
| ) | ||
| end | ||
|
|
||
| describe '.decode_and_verify_jwt' do | ||
| context 'with a valid JWT' do | ||
| it 'returns the decoded payload' do | ||
| token = generate_jwt(test_payload, rsa_key, kid) | ||
| result = AlmaJwtValidator.decode_and_verify_jwt(token) | ||
|
|
||
| expect(result).to be_an(Array) | ||
| expect(result[0]['userName']).to eq('10335026') | ||
| expect(result[1]['kid']).to eq(kid) | ||
| end | ||
| end | ||
|
|
||
| context 'with an invalid signature' do | ||
| it 'raises JWT::DecodeError' do | ||
| # Generate a token with a different key | ||
| different_key = OpenSSL::PKey::RSA.new(2048) | ||
| token = generate_jwt(test_payload, different_key, kid) | ||
|
|
||
| expect do | ||
| AlmaJwtValidator.decode_and_verify_jwt(token) | ||
| end.to raise_error(JWT::DecodeError) | ||
| end | ||
| end | ||
|
|
||
| context 'with an unknown key id' do | ||
| it 'raises JWT::DecodeError' do | ||
| token = generate_jwt(test_payload, rsa_key, 'unknown-kid') | ||
|
|
||
| expect do | ||
| AlmaJwtValidator.decode_and_verify_jwt(token) | ||
| end.to raise_error(JWT::DecodeError) | ||
| end | ||
| end | ||
|
|
||
| context 'with a malformed JWT' do | ||
| it 'raises JWT::DecodeError' do | ||
| expect do | ||
| AlmaJwtValidator.decode_and_verify_jwt('not.a.jwt') | ||
| end.to raise_error(JWT::DecodeError) | ||
| end | ||
| end | ||
|
|
||
| context 'when JWKS endpoint is unreachable' do | ||
| it 'raises an error' do | ||
| stub_request(:get, jwks_url).to_return(status: 500) | ||
| token = generate_jwt(test_payload, rsa_key, kid) | ||
|
|
||
| expect do | ||
| AlmaJwtValidator.decode_and_verify_jwt(token) | ||
| end.to raise_error(StandardError) | ||
| end | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the sandbox publish a different URL? If so we'd need to be able to inject this (e.g. via ENV).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we noticed when pairing, the actual issuer is Prima. From our testing: