712ba617de
* Inline subtlecrypto shim The presence of this thing just makes code more confusing. * Remove pre-node-20 webcrypto hack Until node 20.0, the webcrypto API lived at `crypto.webCrypto`. It's now available at the same place as in web -- `globalThis.crypto`. See: https://nodejs.org/docs/latest-v20.x/api/webcrypto.html#web-crypto-api * oidc auth test: Clean up mocking THe previous reset code wasn't really resetting the right thing. Let's just re-init `window.crypto` on each test. * Remove `crypto` shim This isn't very useful any more.
52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
/*
|
|
Copyright 2018 New Vector Ltd
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
import { encodeUnpaddedBase64Url } from "./base64";
|
|
|
|
const LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
|
|
const UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
const DIGITS = "0123456789";
|
|
|
|
export function secureRandomBase64Url(len: number): string {
|
|
const key = new Uint8Array(len);
|
|
globalThis.crypto.getRandomValues(key);
|
|
|
|
return encodeUnpaddedBase64Url(key);
|
|
}
|
|
|
|
export function randomString(len: number): string {
|
|
return randomStringFrom(len, UPPERCASE + LOWERCASE + DIGITS);
|
|
}
|
|
|
|
export function randomLowercaseString(len: number): string {
|
|
return randomStringFrom(len, LOWERCASE);
|
|
}
|
|
|
|
export function randomUppercaseString(len: number): string {
|
|
return randomStringFrom(len, UPPERCASE);
|
|
}
|
|
|
|
function randomStringFrom(len: number, chars: string): string {
|
|
let ret = "";
|
|
|
|
for (let i = 0; i < len; ++i) {
|
|
ret += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
}
|
|
|
|
return ret;
|
|
}
|