#!/usr/bin/env node
/**
 * A test NeoPass server! This is a very lean, hacky implementation, designed
 * to just see the basic OAuth interactions Work At All.
 *
 * This server is an `oauth2-mock-server` instance that's easy to spin up and
 * have perform OAuth for us. We give it a hardcoded development-only key, and
 * it just auto-grants permissions!
 *
 * It slightly differs from the NeoPass spec, in that it uses different paths
 * for its endpoints, but that's okay: DTI will use OpenID's "discovery"
 * feature to discover those endpoints via a single well-known path, without
 * needing them hardcoded.
 */

const fs = require("node:fs/promises");
const pathLib = require("node:path");
const { spawn } = require("node:child_process");
const urlLib = require("node:url");

const { OAuth2Server } = require("oauth2-mock-server");
const express = require("express");

// This is the Neopets username and email we'll report back to DTI when you
// authenticate through here.
const USERNAME = "test";
const EMAIL = "theneopetsteam@neopets.com";

const certPath = pathLib.join(__dirname, "..", "tmp", "localhost.pem");
const keyPath = pathLib.join(__dirname, "..", "tmp", "localhost-key.pem");

async function fileExists(path) {
	try {
		await fs.stat(path);
	} catch (error) {
		if (error.code === "ENOENT") {
			return false;
		}
		throw error;
	}
	return true;
}

async function ensureCertsExist() {
	if (!(await fileExists(certPath)) || !(await fileExists(keyPath))) {
		console.log(
			"Using mkcert to create localhost.pem and localhost-key.pem in " +
				"the Rails tmp dir, to serve over HTTPS.",
		);

		const mkcertProc = spawn(
			"mkcert",
			["-cert-file", certPath, "-key-file", keyPath, "localhost"],
			{ stdio: ["ignore", process.stdout, process.stderr] },
		);

		// Wait for the process to finish, raising an error if it fails.
		await new Promise((resolve, reject) => {
			mkcertProc.on("close", (code) => {
				if (code === 0) {
					resolve();
				} else {
					reject(new Error(`mkcert returned status ${code}`));
				}
			});
			mkcertProc.on("error", (error) => {
				reject(error);
			});
		});
	}
}

async function startServer(port) {
	const server = new OAuth2Server(keyPath, certPath);
	await server.issuer.keys.add({
		// A key we generated for the NeoPass test server. It's okay for its
		// "secret" info to be here, because it's for development only!
		kid: "neopass-server-DEVLOPMENT-ONLY-NOT-FOR-PRODUCTION",
		p: "50btwsJlPbGLUFnCSBZ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		dq: "purLCHKKKM7NRfYRsFiI_H2wPwfroHX8uqokz2rKk_Kc5NX9CNYOEmokBfO9BtenCIxIhX5k2G8NeD5BQrSAenIEdy5g-5FVVtevH1s023vDMyU29hOs_eHnh4d1poiwTUk8q_T3d1S7CZnr5r_drRSN2m1C7biLLwVHrLTceVE",
		n: "svVfGU4NGcfBCmQiIOW5uzg5SAN2CWSIQSstnhqZoCdjy5OoKpKVR8O9TbDvxixrvkFyAav90Q0Xse8iFTcjfCKuqINYiuYMXhCvfBlc_DVVOQca9pMpN03LaDofd5Ll4_BFTtt1nSPahwWU7xDM-Bkkh_TcS2qS4N2xbpEGi0q0ZkrJN4WyiDBC2k9WbK-YHr4Rj4JKypFVSeBIrjxVPmlPzgfqlLGGIB0l92SnJDXDMlkWcCCTyLgqSBM04nkxGDSykq_ei76qCdRd7b10wMBaoS9DeBThAyHpur2LoPdH3gxbcwoWExi-jPlNP1LdKVZD8b95OY3CRyMAAMGdKQ",
	});

	server.service.on("beforeTokenSigning", (token) => {
		token.payload.sub = USERNAME;
	});

	server.service.on("beforeUserinfo", (userInfoResponse) => {
		userInfoResponse.body.email = EMAIL;
	});

	await server.start(port, "localhost");
	console.log(`Started NeoPass development server at: ${server.issuer.url}`);
}

async function main() {
	await ensureCertsExist();
	await startServer(8585);
}

main().catch((error) => {
	console.error(error);
});