Skip to content
Snippets Groups Projects
Commit b322534f authored by Steinwender, Tamara's avatar Steinwender, Tamara
Browse files

Fix unit test

parent 1855a1aa
No related branches found
No related tags found
No related merge requests found
Pipeline #61380 passed
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
"@open-wc/scoped-elements": "^1.3.3", "@open-wc/scoped-elements": "^1.3.3",
"file-saver": "^2.0.2", "file-saver": "^2.0.2",
"i18next": "^20.0.0", "i18next": "^20.0.0",
"jose": "^3.16.1", "jose": "^4.0.0",
"jszip": "^3.5.0", "jszip": "^3.5.0",
"lit-element": "^2.1.0", "lit-element": "^2.1.0",
"lit-html": "^1.3.0", "lit-html": "^1.3.0",
......
...@@ -7,6 +7,7 @@ import json from '@rollup/plugin-json'; ...@@ -7,6 +7,7 @@ import json from '@rollup/plugin-json';
import serve from 'rollup-plugin-serve'; import serve from 'rollup-plugin-serve';
import del from 'rollup-plugin-delete'; import del from 'rollup-plugin-delete';
import {getPackagePath, getDistPath} from '../../rollup.utils.js'; import {getPackagePath, getDistPath} from '../../rollup.utils.js';
import path from "path";
const pkg = require('./package.json'); const pkg = require('./package.json');
const build = (typeof process.env.BUILD !== 'undefined') ? process.env.BUILD : 'local'; const build = (typeof process.env.BUILD !== 'undefined') ? process.env.BUILD : 'local';
...@@ -29,7 +30,7 @@ export default (async () => { ...@@ -29,7 +30,7 @@ export default (async () => {
del({ del({
targets: 'dist/*' targets: 'dist/*'
}), }),
resolve(), resolve({browser: true}),
commonjs(), commonjs(),
json(), json(),
(build !== 'local' && build !== 'test') ? terser() : false, (build !== 'local' && build !== 'test') ? terser() : false,
......
import { CompactEncrypt } from 'jose/jwe/compact/encrypt'; import { CompactEncrypt, compactDecrypt, importJWK, base64url } from 'jose';
import { compactDecrypt } from 'jose/jwe/compact/decrypt';
import { parseJwk } from 'jose/jwk/parse';
import {encode} from 'jose/util/base64url';
/** /**
* This "encrypts" the additional information string using the current oauth2 * This encrypts the payload using the token,
* token, using A256GCM and PBES2-HS256+A128KW. * using A256GCM and PBES2-HS256+A128KW.
*
* Since we can't do any server side validation the user needs to confirm in the
* UI that he/she won't abuse the system.
*
* By using the token we make replaying an older requests harder and by using
* JOSE which needs crypto APIs, abusing the system can't reasonably be done by
* accident but only deliberately.
*
* This doesn't make things more secure, it just makes the intent of the user
* more clear in case the API isn't used through our UI flow.
* *
* @param {string} token * @param {string} token
* @param {string} payload * @param {string} payload
...@@ -23,40 +10,36 @@ import {encode} from 'jose/util/base64url'; ...@@ -23,40 +10,36 @@ import {encode} from 'jose/util/base64url';
*/ */
export async function encrypt(token, payload) { export async function encrypt(token, payload) {
const encoder = new TextEncoder(); const encoder = new TextEncoder();
const key = await parseJwk({kty: 'oct', k: encode(token)}, 'PBES2-HS256+A128KW'); const key = await importJWK({kty: 'oct', k: base64url.encode(token)}, 'PBES2-HS256+A128KW');
const jwe = await new CompactEncrypt(encoder.encode(payload)) const jwe = await new CompactEncrypt(encoder.encode(payload))
.setProtectedHeader({alg: 'PBES2-HS256+A128KW', enc: 'A256GCM'}) .setProtectedHeader({alg: 'PBES2-HS256+A128KW', enc: 'A256GCM'})
.encrypt(key); .encrypt(key);
return jwe; return jwe;
} }
/** /**
* This "encrypts" the additional information string using the current oauth2 * This creates a key from the given token and
* token, using A256GCM and PBES2-HS256+A128KW. * decrypts the payload using the token,
* * using A256GCM and PBES2-HS256+A128KW.
* Since we can't do any server side validation the user needs to confirm in the
* UI that he/she won't abuse the system.
*
* By using the token we make replaying an older requests harder and by using
* JOSE which needs crypto APIs, abusing the system can't reasonably be done by
* accident but only deliberately.
*
* This doesn't make things more secure, it just makes the intent of the user
* more clear in case the API isn't used through our UI flow.
* *
* @param {string} token * @param {string} token
* @param {string} payload * @param {string} payload
* @returns {string} * @returns {string}
*/ */
export async function decrypt(token, payload) { export async function decrypt(token, payload) {
const key = await parseJwk({kty: 'oct', k: encode(token)}, 'PBES2-HS256+A128KW'); const key = await importJWK({kty: 'oct', k: base64url.encode(token)}, 'PBES2-HS256+A128KW');
const decryption = await compactDecrypt(payload, key, {alg: 'PBES2-HS256+A128KW', enc: 'A256GCM'}); const decryption = await compactDecrypt(payload, key, {alg: 'PBES2-HS256+A128KW', enc: 'A256GCM'});
const secret = new TextDecoder().decode(decryption.plaintext); const secret = new TextDecoder().decode(decryption.plaintext);
return secret; return secret;
} }
/*
/**
* This parses a given json webtoken to its different parts
*
* @param {string} token
* @returns {string}
*/
export function parseJwt (token) { export function parseJwt (token) {
if (!token) if (!token)
return null; return null;
......
...@@ -5155,6 +5155,11 @@ jest-worker@^26.2.1: ...@@ -5155,6 +5155,11 @@ jest-worker@^26.2.1:
merge-stream "^2.0.0" merge-stream "^2.0.0"
supports-color "^7.0.0" supports-color "^7.0.0"
jose@^4.0.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/jose/-/jose-4.2.0.tgz#eb3dfe4926514a99f325ba604d32e41589394f6d"
integrity sha512-7nlU7qankWiES1WmZXAJl0jiGusoouXhjiGR12yc+0/SIDi+4uhEGzqcfONtDI7g66K4IyqA44botXGpi9EBWA==
jquery@>=1.7, jquery@^3.4.1: jquery@>=1.7, jquery@^3.4.1:
version "3.6.0" version "3.6.0"
resolved "https://registry.yarnpkg.com/jquery/-/jquery-3.6.0.tgz#c72a09f15c1bdce142f49dbf1170bdf8adac2470" resolved "https://registry.yarnpkg.com/jquery/-/jquery-3.6.0.tgz#c72a09f15c1bdce142f49dbf1170bdf8adac2470"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment