Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/services/login.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Ed25519KeyIdentity} from '@dfinity/identity';
import type {JsonnableEd25519KeyIdentity} from '@dfinity/identity/lib/cjs/identity/ed25519';
import {nextArg} from '@junobuild/cli-tools';
import {bold, green, underline} from 'kleur';
import {randomBytes} from 'node:crypto';
import fs from 'node:fs';
import type http from 'node:http';
import {createServer} from 'node:http';
Expand All @@ -18,7 +19,7 @@ const __dirname = dirname(__filename);

export const login = async (args?: string[]) => {
const port = await getPort();
const nonce = Math.floor(Math.random() * (2 << 29) + 1);
const nonce = randomBytes(16).toString('hex');

const key = Ed25519KeyIdentity.generate();
const principal = key.getPrincipal().toText();
Expand All @@ -37,7 +38,7 @@ export const login = async (args?: string[]) => {
const orbiters = url.searchParams.get('orbiters');
const missionControl = url.searchParams.get('mission_control');

if (returnedNonce !== `${nonce}`) {
if (returnedNonce !== nonce) {
await respondWithFile(req, res, 400, '../templates/login/failure.html');
reject(new Error('Unexpected error while logging in.'));
server.close();
Expand Down
6 changes: 3 additions & 3 deletions src/utils/auth.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const authUrl = ({
principal
}: {
port: number;
nonce: number;
nonce: string;
principal: string;
}): string => {
const callbackUrl = authCallbackUrl({port, nonce});
Expand All @@ -24,9 +24,9 @@ export const requestUrl = ({port, reqUrl}: {port: number; reqUrl: string | undef
return `${requestUrl}${reqUrl}`;
};

const authCallbackUrl = ({port, nonce}: {port: number; nonce: number}): string => {
const authCallbackUrl = ({port, nonce}: {port: number; nonce: string}): string => {
const redirectUrl = new URL(REDIRECT_URL.replace('{port}', `${port}`));
redirectUrl.searchParams.set('state', `${nonce}`);
redirectUrl.searchParams.set('state', nonce);

return redirectUrl.toString();
};