CommunitySolidServer/test/deploy/createAccountCredentials.ts
Jasper Vaneessen 7c6eae0ee6
chore: Use conventional changelog and automate config version bumps
* chore: add the reference change to npm version

* chore: remove unused require

* chore: add conventional-changelog

* chore: add git to pre-release config changes

* style: formatting

* chore: fix commit message

* chore: add no-verify to commit of configs

* chore: no more shellJs

* chore: fixing async

* chore: committing restored

* refactor: move and rename

* chore: remove shelljs devdep and old script

* chore: change npm script ref after refactor

* chore: upgrade-config code improvements

* chore: edit package.json (not package-lock)

* chore(changelog): use conventionalcommits preset

* chore: add conventional changelog config

* chore: use .versionrc directly

* chore: update changelog config

* chore: update .versionrc.json

* chore: use standard-version

* chore: change to standard version

* styling(changelog): remove a tags + formatting

* styling: conventiontal-changelog styling

* chore: postformatting of changelog

* chore: remove unnecessary dependencies

* chore: add upgrade-config to version as backup

* docs: update release.md

* styling: order scripts alphabetically

* docs: requested changes + dry-run explanation

* chore: release script to TS

* chore: use ts-node to execute the TS scripts

* docs: add some documentation comments to script

* docs: remove unnecessary newline

* docs: fix comment linting

* chore: add test/integration and templates configs

* chore: correct automated commit message

* chore: remove fdir dependency

* chore: remove manual-git-changelog dependency

* chore: impl requested changes

* docs: update script comments

* chore: ensure full cov

* chore: review comments
2022-05-17 11:36:12 +02:00

96 lines
2.5 KiB
TypeScript

#!/usr/bin/env ts-node
/* eslint-disable no-console */
import fetch from 'cross-fetch';
import urljoin from 'url-join';
if (process.argv.length !== 3) {
throw new Error('Exactly 1 parameter is needed: the server URL.');
}
const baseUrl = process.argv[2];
type User = {
email: string;
password: string;
podName: string;
};
const alice: User = {
email: 'alice@example.com',
password: 'alice-secret',
podName: 'alice',
};
const bob: User = {
email: 'bob@example.com',
password: 'bob-secret',
podName: 'bob',
};
/**
* Registers a user with the server.
* @param user - The user settings necessary to register a user.
*/
async function register(user: User): Promise<void> {
const body = JSON.stringify({
...user,
confirmPassword: user.password,
createWebId: true,
register: true,
createPod: true,
});
const res = await fetch(urljoin(baseUrl, '/idp/register/'), {
method: 'POST',
headers: { 'content-type': 'application/json' },
body,
});
if (res.status !== 200) {
throw new Error(`Registration failed: ${await res.text()}`);
}
}
/**
* Requests a client credentials API token.
* @param user - User for which the token needs to be generated.
* @returns The id/secret for the client credentials request.
*/
async function createCredentials(user: User): Promise<{ id: string; secret: string }> {
const res = await fetch(urljoin(baseUrl, '/idp/credentials/'), {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ email: user.email, password: user.password, name: 'token' }),
});
if (res.status !== 200) {
throw new Error(`Token generation failed: ${await res.text()}`);
}
return res.json();
}
/**
* Generates all the necessary data and outputs the necessary lines
* that need to be added to the CTH environment file
* so it can use client credentials.
* @param user - User for which data needs to be generated.
*/
async function outputCredentials(user: User): Promise<void> {
await register(user);
const { id, secret } = await createCredentials(user);
const name = user.podName.toUpperCase();
console.log(`USERS_${name}_CLIENTID=${id}`);
console.log(`USERS_${name}_CLIENTSECRET=${secret}`);
}
/**
* Ends the process and writes out an error in case something goes wrong.
*/
function endProcess(error: Error): never {
console.error(error);
process.exit(1);
}
// Create tokens for Alice and Bob
outputCredentials(alice).catch(endProcess);
outputCredentials(bob).catch(endProcess);