Skip to content

Commit 7bfe720

Browse files
committed
refactor: populate publicKey with a JWK when one is downloaded
1 parent 6a4dd7e commit 7bfe720

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

src/editor/public-key-download.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { createRemoteJWKSet } from 'jose/jwks/remote'
2-
import { EmbeddedJWK } from 'jose/jwk/embedded'
32
import * as keyExport from 'jose/key/export'
43

54
import { httpGet } from '../utils.js';
@@ -26,7 +25,7 @@ function getKeyFromX5Claims(claims) {
2625
}
2726

2827
function getKeyFromJwkKeySetUrl(header, url) {
29-
return createRemoteJWKSet(new URL(url))(header, {}).then((key) => keyExport.exportSPKI(key))
28+
return createRemoteJWKSet(new URL(url))(header, {}).then(keyExport.exportJWK).then((jwk) => JSON.stringify(jwk, null, 2))
3029
}
3130

3231
export function downloadPublicKeyIfPossible(decodedToken) {
@@ -44,7 +43,7 @@ export function downloadPublicKeyIfPossible(decodedToken) {
4443
} else if(header.jku) {
4544
getKeyFromJwkKeySetUrl(header, header.jku).then(resolve, reject);
4645
} else if(header.jwk) {
47-
EmbeddedJWK(header, {}).then((key) => keyExport.exportSPKI(key)).then(resolve, reject);
46+
resolve(JSON.stringify(header.jwk, null, 2))
4847
} else if(payload.iss) {
4948
const url = payload.iss + (payload.iss.substr(-1) === '/' ? '.well-known/openid-configuration' : '/.well-known/openid-configuration')
5049

test/functional/editor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ describe('Editor', function() {
578578
const publicKey = await this.page.$eval('textarea[name="public-key"]',
579579
publicKeyElement => publicKeyElement.value);
580580

581-
expect(publicKey).to.include(defaultTokens.rs256.publicKey);
581+
expect(jwks.keys[0]).to.contain(JSON.parse(publicKey))
582582
});
583583

584584
it('jku', async function() {
@@ -604,7 +604,7 @@ describe('Editor', function() {
604604
const publicKey = await this.page.$eval('textarea[name="public-key"]',
605605
publicKeyElement => publicKeyElement.value);
606606

607-
expect(publicKey).to.include(defaultTokens.rs256.publicKey);
607+
expect(jwks.keys[0]).to.contain(JSON.parse(publicKey))
608608
});
609609

610610
it('x5c', async function() {

test/unit/editor/public-key-download.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('Public key downloader', function() {
4848
}]
4949
};
5050

51-
const keyAsPEM = `-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1GPz+Er5h7PCk4v3pSln\naLYNYrp4sVc6Tx7FVz9d8m4zIS2qzcTM/6dRbMgZ4hBdD35NpYzU4z+d8lN27+J/\njOzHnCiMdkY+w52dCofAkICh6ftkFlG9bFQyH8Jz5UtpVkZyy1dxCRz/sbRAzUdj\nUYsGvrKXg+3UYCL5SBCnt0ycrvr3iKX9k8IlMrFRB8lBJ6eQVzkzGsuivPaThXjV\nZ/OpY7W+XsDjut7cFgPKIc843tW4CNaDJ6j3afm+RFOok//xLQH5uA7HXS/yqfEc\nhvzXfYfMxJY2d+Eqw4xTurm3TT07RnwJuN9slDJUrTH9EKkJkjZ7dn7fZtGjGTpa\nDQIDAQAB\n-----END PUBLIC KEY-----\n`;
51+
const keyAsJWK = JSON.stringify({ kty: jwks.keys[0].kty, n: jwks.keys[0].n, e: jwks.keys[0].e }, null, 2)
5252

5353
it('Finds keys in iss + .well-known URL', function(done) {
5454
const decodedToken = _.defaultsDeep({}, decodedBaseToken, {
@@ -74,7 +74,7 @@ describe('Public key downloader', function() {
7474
}).downloadPublicKeyIfPossible;
7575

7676
downloadPublicKeyIfPossible(decodedToken)
77-
.should.eventually.include(keyAsPEM)
77+
.should.eventually.include(keyAsJWK)
7878
.then(() => {
7979
httpGetStub.should.have.been
8080
.calledWith(baseUrl + '.well-known/openid-configuration');
@@ -97,7 +97,7 @@ describe('Public key downloader', function() {
9797
}).downloadPublicKeyIfPossible;
9898

9999
downloadPublicKeyIfPossible(decodedToken)
100-
.should.eventually.include(keyAsPEM)
100+
.should.eventually.include(JSON.stringify(jwks.keys[0], null, 2))
101101
.then(() => {
102102
httpGetStub.should.have.callCount(0);
103103
}).should.notify(done);
@@ -123,7 +123,7 @@ describe('Public key downloader', function() {
123123
}).downloadPublicKeyIfPossible;
124124

125125
downloadPublicKeyIfPossible(decodedToken)
126-
.should.eventually.include(keyAsPEM)
126+
.should.eventually.include(keyAsJWK)
127127
.then(() => {
128128
httpGetStub.should.have.callCount(0);
129129
})

0 commit comments

Comments
 (0)