@simulacrum/auth0-simulator 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.md +13 -9
  3. package/bin/index.js +2 -0
  4. package/dist/auth/date.js +1 -1
  5. package/dist/auth/date.js.map +1 -1
  6. package/dist/auth/jwt.d.ts +1 -1
  7. package/dist/auth/jwt.d.ts.map +1 -1
  8. package/dist/auth/jwt.js +5 -4
  9. package/dist/auth/jwt.js.map +1 -1
  10. package/dist/handlers/auth0-handlers.d.ts +1 -1
  11. package/dist/handlers/auth0-handlers.d.ts.map +1 -1
  12. package/dist/handlers/auth0-handlers.js +82 -31
  13. package/dist/handlers/auth0-handlers.js.map +1 -1
  14. package/dist/handlers/get-service-url.js +1 -1
  15. package/dist/handlers/get-service-url.js.map +1 -1
  16. package/dist/handlers/login-redirect.js +1 -1
  17. package/dist/handlers/login-redirect.js.map +1 -1
  18. package/dist/handlers/openid-handlers.js +2 -2
  19. package/dist/handlers/openid-handlers.js.map +1 -1
  20. package/dist/handlers/web-message.js +4 -4
  21. package/dist/handlers/web-message.js.map +1 -1
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +33 -28
  24. package/dist/index.js.map +1 -1
  25. package/dist/middleware/create-cors.js +1 -1
  26. package/dist/middleware/create-cors.js.map +1 -1
  27. package/dist/middleware/session.js +1 -1
  28. package/dist/middleware/session.js.map +1 -1
  29. package/dist/rules/parse-rules-files.js +2 -2
  30. package/dist/rules/parse-rules-files.js.map +1 -1
  31. package/dist/rules/rules-runner.js +3 -3
  32. package/dist/rules/rules-runner.js.map +1 -1
  33. package/dist/start.js +35 -3
  34. package/dist/start.js.map +1 -1
  35. package/dist/types.d.ts +21 -0
  36. package/dist/types.d.ts.map +1 -1
  37. package/dist/views/login.js +1 -1
  38. package/dist/views/username-password.js +1 -1
  39. package/dist/views/username-password.js.map +1 -1
  40. package/dist/views/web-message.js +1 -1
  41. package/dist/views/web-message.js.map +1 -1
  42. package/package.json +19 -13
  43. package/docs/create-simulation.png +0 -0
  44. package/docs/person.png +0 -0
  45. package/src/auth/constants.ts +0 -16
  46. package/src/auth/date.ts +0 -4
  47. package/src/auth/jwt.ts +0 -23
  48. package/src/handlers/auth0-handlers.ts +0 -219
  49. package/src/handlers/get-service-url.ts +0 -10
  50. package/src/handlers/login-redirect.ts +0 -37
  51. package/src/handlers/openid-handlers.ts +0 -39
  52. package/src/handlers/url.ts +0 -1
  53. package/src/handlers/web-message.ts +0 -31
  54. package/src/index.ts +0 -70
  55. package/src/middleware/create-cors.ts +0 -14
  56. package/src/middleware/no-cache.ts +0 -7
  57. package/src/middleware/session.ts +0 -14
  58. package/src/rules/extensionless-file-name.ts +0 -4
  59. package/src/rules/parse-rules-files.ts +0 -40
  60. package/src/rules/rules-runner.ts +0 -72
  61. package/src/rules/types.ts +0 -25
  62. package/src/start.ts +0 -19
  63. package/src/types.ts +0 -29
  64. package/src/views/login.ts +0 -107
  65. package/src/views/public/img/frontside-logo.png +0 -0
  66. package/src/views/username-password.ts +0 -54
  67. package/src/views/web-message.ts +0 -72
  68. package/test/auth0.test.ts +0 -351
  69. package/test/helpers.ts +0 -23
  70. package/test/openid-handlers.test.ts +0 -57
  71. package/test/rules/avatar.js +0 -13
  72. package/test/rules/avatar.json +0 -5
  73. package/tsconfig.dist.json +0 -12
  74. package/tsconfig.dist.tsbuildinfo +0 -2236
  75. package/tsconfig.json +0 -11
  76. package/tsconfig.watch.json +0 -9
  77. package/watch.ts +0 -59
@@ -1,39 +0,0 @@
1
- import type { HttpHandler } from '@simulacrum/server';
2
- import { Options } from 'src/types';
3
- import { JWKS } from '../auth/constants';
4
- import { getServiceUrl } from './get-service-url';
5
- import { removeTrailingSlash } from './url';
6
-
7
- type Routes =
8
- | '/jwks.json'
9
- | '/openid-configuration'
10
-
11
- export type OpenIdRoutes = `${`/.well-known`}${Routes}`
12
-
13
- export interface OpenIdConfiguration {
14
- issuer: string;
15
- authorization_endpoint: string;
16
- token_endpoint: string;
17
- userinfo_endpoint: string;
18
- jwks_uri: string;
19
- }
20
-
21
- export const createOpenIdHandlers = (options: Options): Record<OpenIdRoutes, HttpHandler> => {
22
- return {
23
- ['/.well-known/jwks.json']: function* (_, res) {
24
- res.json(JWKS);
25
- },
26
-
27
- ['/.well-known/openid-configuration']: function* (_, res) {
28
- let url = removeTrailingSlash(getServiceUrl(options).toString());
29
-
30
- res.json({
31
- issuer: `${url}/`,
32
- authorization_endpoint: [url, "authorize"].join('/'),
33
- token_endpoint: [url, "oauth", "token"].join('/'),
34
- userinfo_endpoint: [url, "userinfo"].join('/'),
35
- jwks_uri: [url, ".well-known", "jwks.json"].join('/'),
36
- });
37
- }
38
- };
39
- };
@@ -1 +0,0 @@
1
- export const removeTrailingSlash = (url: string): string => url.replace(/\/$/, '');
@@ -1,31 +0,0 @@
1
- import { Middleware } from '@simulacrum/server';
2
- import { assert } from 'assert-ts';
3
- import { encode } from 'base64-url';
4
- import { QueryParams } from 'src/types';
5
- import { webMessage } from '../views/web-message';
6
-
7
- export const createWebMessageHandler = (): Middleware =>
8
- function* (req, res) {
9
- assert(!!req.session, "no session");
10
-
11
- let username = req.session.username;
12
-
13
- assert(!!username, `no username in authorise`);
14
-
15
- let {
16
- redirect_uri,
17
- state,
18
- nonce
19
- } = req.query as QueryParams;
20
-
21
- res.set("Content-Type", "text/html");
22
-
23
- let message = webMessage({
24
- code: encode(`${nonce}:${username}`),
25
- state,
26
- redirect_uri,
27
- nonce,
28
- });
29
-
30
- res.status(200).send(Buffer.from(message));
31
- };
package/src/index.ts DELETED
@@ -1,70 +0,0 @@
1
- import type { Simulator, Service } from '@simulacrum/server';
2
- import { createHttpApp } from '@simulacrum/server';
3
- import { urlencoded, json } from 'express';
4
- import { createAuth0Handlers } from './handlers/auth0-handlers';
5
- import { person } from '@simulacrum/server';
6
- import { createSession } from './middleware/session';
7
- import path from 'path';
8
- import express from 'express';
9
- import { Options } from './types';
10
- import { createCors } from './middleware/create-cors';
11
- import { noCache } from './middleware/no-cache';
12
- import { createOpenIdHandlers } from './handlers/openid-handlers';
13
-
14
-
15
- const publicDir = path.join(__dirname, 'views', 'public');
16
-
17
- const DefaultOptions = {
18
- clientId: '00000000000000000000000000000000',
19
- audience: 'https://thefrontside.auth0.com/api/v1/',
20
- scope: "openid profile email offline_access",
21
- };
22
-
23
- const createAuth0Service = (handlers: ReturnType<typeof createAuth0Handlers> & ReturnType<typeof createOpenIdHandlers>): Service => {
24
- return {
25
- protocol: 'https',
26
- app: createHttpApp()
27
- .use(express.static(publicDir))
28
- .use(createSession())
29
- .use(createCors())
30
- .use(noCache())
31
- .use(json())
32
- .use(urlencoded({ extended: true }))
33
- .get('/heartbeat', handlers['/heartbeat'])
34
- .get('/authorize', handlers['/authorize'])
35
- .get('/login', handlers['/login'])
36
- .get('/u/login', handlers['/usernamepassword/login'])
37
- .post('/usernamepassword/login', handlers['/usernamepassword/login'])
38
- .post('/login/callback', handlers['/login/callback'])
39
- .post('/oauth/token', handlers['/oauth/token'])
40
- .get('/v2/logout', handlers['/v2/logout'])
41
- .get('/.well-known/jwks.json', handlers['/.well-known/jwks.json'])
42
- .get('/.well-known/openid-configuration', handlers['/.well-known/openid-configuration'])
43
-
44
- } as const;
45
- };
46
-
47
- export const auth0: Simulator<Options> = (slice, options) => {
48
- let store = slice.slice('store');
49
- let services = slice.slice('services');
50
-
51
- let handlersOptions = { ...DefaultOptions, ...options, store, services };
52
-
53
- let auth0Handlers = createAuth0Handlers(handlersOptions);
54
- let openIdHandlers = createOpenIdHandlers(handlersOptions);
55
-
56
- return {
57
- services: { auth0: createAuth0Service({ ...auth0Handlers, ...openIdHandlers }) },
58
- scenarios: {
59
- /**
60
- * Here we just wrap the internal `person` scenario to augment
61
- * it with a username and password
62
- * but what we really need to have some way to _react_ to the person
63
- * having been created and augment the record at that point.
64
- */
65
- *person(store, faker) {
66
- return yield person(store, faker);
67
- }
68
- }
69
- };
70
- };
@@ -1,14 +0,0 @@
1
- import { RequestHandler } from 'express';
2
- import cors from "cors";
3
-
4
- export const createCors = (): RequestHandler =>
5
- cors({
6
- origin: (origin, cb) => {
7
- if (typeof origin === "string") {
8
- return cb(null, [origin]);
9
- }
10
-
11
- cb(null, "*");
12
- },
13
- credentials: true,
14
- });
@@ -1,7 +0,0 @@
1
- import type { RequestHandler } from 'express';
2
-
3
- export const noCache: () => RequestHandler = () => (_, res, next) => {
4
- res.set("Pragma", "no-cache");
5
- res.set("Cache-Control", "no-cache, no-store");
6
- next();
7
- };
@@ -1,14 +0,0 @@
1
- import type { RequestHandler } from 'express';
2
- import cookieSession from "cookie-session";
3
- const twentyFourHours = 24 * 60 * 60 * 1000;
4
-
5
- export const createSession = (): RequestHandler => {
6
- return cookieSession({
7
- name: "session",
8
- keys: ["shhh"],
9
- secure: true,
10
- httpOnly: false,
11
- maxAge: twentyFourHours,
12
- sameSite: "none",
13
- });
14
- };
@@ -1,4 +0,0 @@
1
- export const extensionlessFileName = (fileName: string): string =>
2
- fileName.indexOf(".") === -1
3
- ? fileName
4
- : fileName.split(".").slice(0, -1).join(".");
@@ -1,40 +0,0 @@
1
- import { extensionlessFileName } from './extensionless-file-name';
2
- import { assert } from 'assert-ts';
3
- import fs from "fs";
4
- import path from "path";
5
-
6
-
7
- export function parseRulesFiles(rulesPath: string): {code: string, filename: string}[] {
8
- let ruleFiles = fs
9
- .readdirSync(rulesPath)
10
- .filter((f) => path.extname(f) === ".js");
11
-
12
- return ruleFiles
13
- .map((r) => {
14
- let filename = path.join(rulesPath, r);
15
-
16
- let jsonFile = `${extensionlessFileName(filename)}.json`;
17
-
18
- assert(!!jsonFile, `no corresponding rule file for ${r}`);
19
-
20
- let rawRule = fs.readFileSync(jsonFile, 'utf8');
21
-
22
- let {
23
- enabled,
24
- order = 0,
25
- stage = "login_success",
26
- } = JSON.parse(rawRule);
27
-
28
- if (!enabled) {
29
- return undefined;
30
- }
31
-
32
- let code = fs.readFileSync(filename, {
33
- encoding: "utf-8",
34
- });
35
-
36
- return { code, filename, order, stage };
37
- })
38
- .flatMap(x => !!x ? x : [])
39
- .sort((left, right) => left.order - right.order) ?? [];
40
- }
@@ -1,72 +0,0 @@
1
- import path from "path";
2
- import vm from "vm";
3
- import fs from 'fs';
4
- import { assert } from "assert-ts";
5
- import { parseRulesFiles } from './parse-rules-files';
6
- import { RuleContext, RuleUser } from './types';
7
-
8
- export type RulesRunner = <A, I>(user: RuleUser, context: RuleContext<A, I>) => void;
9
-
10
- export function createRulesRunner (rulesPath?: string): RulesRunner {
11
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
12
- let callback = (_user: RuleUser, _context: RuleContext<unknown, unknown>) => {};
13
-
14
- if(typeof rulesPath === 'undefined') {
15
- return callback;
16
- }
17
-
18
- let fullPath = path.join(process.cwd(), rulesPath);
19
-
20
- assert(fs.existsSync(fullPath), `no rules directory at ${fullPath}`);
21
-
22
- let rules = parseRulesFiles(rulesPath);
23
-
24
- if(rules.length === 0) {
25
- return callback;
26
- }
27
-
28
- return <A, I>(user: RuleUser, context: RuleContext<A, I>) => {
29
- console.debug(`applying ${rules.length} rules`);
30
-
31
- let vmContext = vm.createContext({
32
- process,
33
- Buffer,
34
- clearImmediate,
35
- clearInterval,
36
- clearTimeout,
37
- setImmediate,
38
- setInterval,
39
- setTimeout,
40
- console,
41
- require,
42
- module,
43
- __simulator: {
44
- ...{
45
- user,
46
- context: { ...context, },
47
- callback,
48
- },
49
- },
50
- });
51
-
52
- for (let rule of rules) {
53
- assert(typeof rule !== "undefined", "undefined rule");
54
-
55
- let { code, filename } = rule;
56
-
57
- console.debug(`executing rule ${path.basename(filename)}`);
58
-
59
- let script = new vm.Script(
60
- `(function(exports) {
61
- (${code})(__simulator.user, __simulator.context, __simulator.callback)
62
- }
63
- (module.exports));
64
- `
65
- );
66
-
67
- script.runInContext(vmContext, {
68
- filename,
69
- });
70
- }
71
- };
72
- }
@@ -1,25 +0,0 @@
1
- export interface RuleUser {
2
- email?: string | undefined;
3
- username?: string | undefined;
4
- email_verified?: boolean | undefined;
5
- verify_email?: boolean | undefined;
6
- user_id?: string | undefined;
7
- blocked?: boolean | undefined;
8
- nickname?: string | undefined;
9
- picture?: string | undefined;
10
- password?: string | undefined;
11
- phone_number?: string | undefined;
12
- phone_verified?: boolean | undefined;
13
- given_name?: string | undefined;
14
- family_name?: string | undefined;
15
- name?: string | undefined;
16
- }
17
-
18
- export interface RuleContext<A, I> {
19
- clientID: string
20
- accessToken: {
21
- scope: string | string[]
22
- } & A
23
-
24
- idToken: I
25
- }
package/src/start.ts DELETED
@@ -1,19 +0,0 @@
1
- import { main } from 'effection';
2
- import { createSimulationServer, Server } from '@simulacrum/server';
3
- import { auth0 } from '.';
4
-
5
- const port = process.env.PORT ? parseInt(process.env.PORT) : undefined;
6
-
7
- main(function*() {
8
- let server: Server = yield createSimulationServer({
9
- seed: 1,
10
- port,
11
- simulators: { auth0 }
12
- });
13
-
14
- let url = `http://localhost:${server.address.port}`;
15
-
16
- console.log(`simulation server running at ${url}`);
17
-
18
- yield;
19
- });
package/src/types.ts DELETED
@@ -1,29 +0,0 @@
1
- import type { SimulationState, Store } from '@simulacrum/server';
2
- import type { Slice } from '@effection/atom';
3
-
4
- export interface Options {
5
- scope: string;
6
- port?: number;
7
- audience: string;
8
- clientId: string;
9
- store: Store;
10
- services: Slice<SimulationState['services']>;
11
- rulesDirectory?: string;
12
- }
13
-
14
- export type ResponseModes = 'query' | 'web_message';
15
-
16
- export type QueryParams = {
17
- state: string;
18
- code: string;
19
- redirect_uri: string;
20
- code_challenge: string;
21
- scope: string;
22
- client_id: string;
23
- nonce: string;
24
- code_challenge_method: string;
25
- response_type: string;
26
- response_mode: ResponseModes;
27
- auth0Client: string;
28
- audience: string;
29
- };
@@ -1,107 +0,0 @@
1
- const html = String.raw;
2
-
3
- interface LoginViewProps {
4
- domain: string;
5
- scope: string;
6
- redirectUri: string;
7
- clientId: string;
8
- audience: string;
9
- loginFailed: boolean;
10
- }
11
-
12
- export const loginView = ({
13
- domain,
14
- scope,
15
- redirectUri,
16
- clientId,
17
- audience,
18
- loginFailed = false
19
- }: LoginViewProps): string => {
20
- return html`
21
- <html lang="en">
22
- <head>
23
- <meta charset="UTF-8" />
24
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
25
- <link
26
- href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css"
27
- rel="stylesheet"
28
- />
29
- <script src="https://cdn.auth0.com/js/auth0/9.16.0/auth0.min.js"></script>
30
- </head>
31
- <title>login</title>
32
- <body>
33
- <main class="min-h-screen flex items-center justify-center bg-white py-12 px-4 sm:px-6 lg:px-8">
34
- <div class="max-w-md w-full space-y-8">
35
- <div class="flex justify-center">
36
- <img alt="frontside" class="bg-transparent object-contain h-16" src="/img/frontside-logo.png" />
37
- </div>
38
- <h1 class="flex justify-center text-4xl">Welcome</h1>
39
- <h2 class="flex justify-center">Log in to continue to frontside</h2>
40
- <form id="the-form" class="mt-8 space-y-6">
41
- <div class="rounded-md shadow-sm -space-y-px">
42
- <div>
43
- <label for="username" class="sr-only">Email address</label>
44
- <input id="username" name="username" type="email" autocomplete="email" required="" value="" class="${loginFailed ? 'border-red-500' : ''} appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-md focus:outline-none focus:ring-blue-500 focus:border-blue-500 focus:z-10 sm:text-sm" placeholder="Email address">
45
- </div>
46
- <div>
47
- <label for="password" class="sr-only">Password</label>
48
- <input id="password" name="password" type="password" autocomplete="current-password" required="" class="my-4 ${loginFailed ? 'border-red-500' : ''} appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-md focus:outline-none focus:ring-blue-500 focus:border-blue-500 focus:z-10 sm:text-sm" placeholder="Password">
49
- </div>
50
- </div>
51
- <div class="error bg-red-500 text-white p-3 ${loginFailed ? '' : 'hidden'}">Wrong email or password</div>
52
-
53
- <div>
54
- <button id="submit" type="button" class="group relative w-full flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
55
- <span class="absolute left-0 inset-y-0 flex items-center pl-3">
56
- <svg class="h-5 w-5 text-blue-500 group-hover:text-blue-400" x-description="Heroicon name: solid/lock-closed" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
57
- <path fill-rule="evenodd" d="M5 9V7a5 5 0 0110 0v2a2 2 0 012 2v5a2 2 0 01-2 2H5a2 2 0 01-2-2v-5a2 2 0 012-2zm8-2v2H7V7a3 3 0 016 0z" clip-rule="evenodd"></path>
58
- </svg>
59
- </span>
60
- Sign in
61
- </button>
62
- </div>
63
- </form>
64
- </div>
65
- </main>
66
- <script>
67
- document.addEventListener('DOMContentLoaded', function(){
68
- var webAuth = new window.auth0.default.WebAuth({
69
- domain: '${domain}',
70
- clientID: '${clientId}',
71
- redirectUri: '${redirectUri}',
72
- audience: '${audience}',
73
- responseType: 'token id_token',
74
- });
75
- var form = document.querySelector('#the-form');
76
- var button = document.querySelector('#sumbit');
77
-
78
- submit.addEventListener('click', function(e) {
79
- let params = new URLSearchParams(window.location.search);
80
-
81
- var username = document.querySelector('#username');
82
- var password = document.querySelector('#password');
83
-
84
- webAuth.login(
85
- {
86
- username: username.value,
87
- password: password.value,
88
- realm: 'Username-Password-Authentication',
89
- scope: '${scope}',
90
- nonce: params.get('nonce'),
91
- state: params.get('state')
92
- },
93
- function(err, authResult) {
94
- if (err) {
95
- [username, password].forEach(e => e.classList.add('border-red-500'));
96
- document.querySelector('.error').classList.remove('hidden');
97
- }
98
- }
99
- );
100
- });
101
- });
102
-
103
- </script>
104
- </body>
105
- </html>
106
- `;
107
- };
@@ -1,54 +0,0 @@
1
- import { encode } from "html-entities";
2
- import { QueryParams } from '../types';
3
-
4
- export type UserNamePasswordForm = {
5
- auth0Domain?: string;
6
- audience?: string;
7
- connection?: string;
8
- response_type?: string;
9
- tenant: string;
10
- } & Partial<QueryParams>;
11
-
12
- export const userNamePasswordForm = ({
13
- auth0Domain = "/login/callback",
14
- redirect_uri,
15
- state,
16
- nonce,
17
- client_id,
18
- scope,
19
- audience,
20
- connection,
21
- response_type,
22
- tenant,
23
- }: UserNamePasswordForm): string => {
24
- let wctx = encode(
25
- JSON.stringify({
26
- strategy: "auth0",
27
- tenant,
28
- connection,
29
- client_id,
30
- response_type,
31
- scope,
32
- redirect_uri,
33
- state,
34
- nonce,
35
- audience,
36
- realm: connection,
37
- })
38
- );
39
-
40
- return `
41
- <form method="post" name="hiddenform" action="${auth0Domain}">
42
- <input type="hidden" name="wa" value="wsignin1.0">
43
- <input type="hidden"
44
- name="wresult"
45
- value="eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1c2VyX2lkIjoiNjA1MzhjYWQ2YWI5ODQwMDY5OWIxZDZhIiwiZW1haWwiOiJpbXJhbi5zdWxlbWFuamlAcmVzaWRlby5jb20iLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsInNpZCI6Im5zSHZTQ0lYT2NGSUZINUIyRzdVdUFEWDVQTlR4cmRPIiwiaWF0IjoxNjE2MTU0ODA0LCJleHAiOjE2MTYxNTQ4NjQsImF1ZCI6InVybjphdXRoMDpyZXNpZGVvOlVzZXJuYW1lLVBhc3N3b3JkLUF1dGhlbnRpY2F0aW9uIiwiaXNzIjoidXJuOmF1dGgwIn0.CTl0A1hDc4YrErsrFBCCEG0ekIUU3bv0x12p_vUgoyD6zOg_QhaSZjKeZI2elaeYnAi7KUcohgOP9TApj3VlQtm6GlGNuWIiQke4866FtfhufGo2_uLBWyf4nmOgbNcmhpIg2bvVJHUqM-6OCNfnzPWAoFW2_g-DeIo20WBfK2E">
46
- <input type="hidden" name="wctx" value="${wctx}">
47
- <noscript>
48
- <p>
49
- Script is disabled. Click Submit to continue.
50
- </p>
51
- <input type="submit" value="Submit">
52
- </noscript>
53
- </form>`;
54
- };
@@ -1,72 +0,0 @@
1
- import jsesc from "jsesc";
2
- import { QueryParams } from '../types';
3
-
4
- export const webMessage = ({
5
- state,
6
- code,
7
- redirect_uri,
8
- nonce,
9
- }: Pick<
10
- QueryParams,
11
- "state" | "code" | "redirect_uri" | "nonce"
12
- >): string => {
13
- let data = jsesc(
14
- {
15
- redirect_uri,
16
- },
17
- { json: true, isScriptContext: true }
18
- );
19
-
20
- return `
21
- <!DOCTYPE html>
22
- <html lang="en">
23
- <head>
24
- <title>Authorization Response</title>
25
- </head>
26
- <body>
27
- <script ${nonce ? `nonce="${nonce}"` : ""} type="application/javascript">
28
- (function(window, document) {
29
- var data = ${data};
30
- var targetOrigin = data.redirect_uri;
31
- var webMessageRequest = {};
32
-
33
- var authorizationResponse = {
34
- type: "authorization_response",
35
- response: {
36
- "code":"${code}",
37
- "state":"${state}"}
38
- };
39
-
40
- var mainWin = (window.opener) ? window.opener : window.parent;
41
-
42
- if (webMessageRequest["web_message_uri"] && webMessageRequest["web_message_target"]) {
43
- window.addEventListener("message", function(evt) {
44
- if (evt.origin != targetOrigin) {
45
- return;
46
- }
47
-
48
- switch (evt.data.type) {
49
- case "relay_response":
50
- var messageTargetWindow = evt.source.frames[webMessageRequest["web_message_target"]];
51
-
52
- if (messageTargetWindow) {
53
- messageTargetWindow.postMessage(authorizationResponse, webMessageRequest["web_message_uri"]);
54
- window.close();
55
- }
56
- break;
57
- }
58
- }
59
- );
60
-
61
- mainWin.postMessage({
62
- type: "relay_request"
63
- }, targetOrigin);
64
- } else {
65
- mainWin.postMessage(authorizationResponse, targetOrigin);
66
- }
67
- })(this, this.document);
68
- </script>
69
- </body>
70
- </html>
71
- `;
72
- };