-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathauth.utils.ts
More file actions
49 lines (39 loc) · 1.49 KB
/
auth.utils.ts
File metadata and controls
49 lines (39 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import {nonNullish} from '@dfinity/utils';
import type {JsonnableEd25519KeyIdentity} from '@icp-sdk/core/identity';
import {Ed25519KeyIdentity} from '@icp-sdk/core/identity';
import type {PrincipalText} from '@junobuild/schema';
import {REDIRECT_URL} from '../constants/constants';
import {ENV} from '../env';
export const generateToken = (): {principal: PrincipalText; token: JsonnableEd25519KeyIdentity} => {
const key = Ed25519KeyIdentity.generate();
const principal = key.getPrincipal().toText();
const token = key.toJSON();
return {principal, token};
};
export const authUrl = ({
port,
nonce,
principal
}: {
port: number;
nonce: string;
principal: string;
}): string => {
const callbackUrl = authCallbackUrl({port, nonce});
const authUrl = new URL(ENV.console.urls.auth);
authUrl.searchParams.set('redirect_uri', encodeURIComponent(callbackUrl));
authUrl.searchParams.set('principal', principal);
if (nonNullish(ENV.profile)) {
authUrl.searchParams.set('profile', encodeURIComponent(ENV.profile));
}
return authUrl.toString();
};
export const requestUrl = ({port, reqUrl}: {port: number; reqUrl: string | undefined}): string => {
const requestUrl = REDIRECT_URL.replace('{port}', `${port}`);
return `${requestUrl}${reqUrl}`;
};
const authCallbackUrl = ({port, nonce}: {port: number; nonce: string}): string => {
const redirectUrl = new URL(REDIRECT_URL.replace('{port}', `${port}`));
redirectUrl.searchParams.set('state', nonce);
return redirectUrl.toString();
};