@restorecommerce/facade 1.2.0 → 1.3.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.
- package/.eslintignore +3 -0
- package/.eslintrc.cjs +13 -0
- package/CHANGELOG.md +11 -0
- package/codegen/index.d.ts +1 -1
- package/codegen/index.js +18 -44
- package/debug-run.ts +7 -6
- package/dist/gql/index.d.ts +1 -1
- package/dist/gql/index.js +1 -17
- package/dist/gql/protos/federation.d.ts +3 -3
- package/dist/gql/protos/federation.js +16 -24
- package/dist/gql/protos/graphql.d.ts +4 -4
- package/dist/gql/protos/graphql.js +64 -42
- package/dist/gql/protos/index.d.ts +6 -6
- package/dist/gql/protos/index.js +6 -22
- package/dist/gql/protos/registry.d.ts +6 -6
- package/dist/gql/protos/registry.js +157 -176
- package/dist/gql/protos/resolvers.d.ts +3 -3
- package/dist/gql/protos/resolvers.js +78 -104
- package/dist/gql/protos/schema.d.ts +4 -4
- package/dist/gql/protos/schema.js +40 -53
- package/dist/gql/protos/types.d.ts +3 -3
- package/dist/gql/protos/types.js +1 -4
- package/dist/gql/protos/utils.d.ts +3 -3
- package/dist/gql/protos/utils.js +23 -62
- package/dist/gql/types/AccessControlTypes.js +13 -16
- package/dist/gql/types/DateType.js +4 -7
- package/dist/gql/types/FilterType.js +7 -10
- package/dist/gql/types/MetaType.js +8 -11
- package/dist/gql/types/ScopeType.js +4 -7
- package/dist/gql/types/SortType.js +5 -8
- package/dist/gql/types/StatusType.js +4 -7
- package/dist/gql/types/index.d.ts +7 -7
- package/dist/gql/types/index.js +7 -23
- package/dist/index.d.ts +14 -14
- package/dist/index.js +195 -207
- package/dist/interfaces.d.ts +8 -8
- package/dist/interfaces.js +1 -2
- package/dist/middlewares/index.d.ts +1 -1
- package/dist/middlewares/index.js +1 -17
- package/dist/middlewares/req-res-logger/index.d.ts +2 -2
- package/dist/middlewares/req-res-logger/index.js +33 -31
- package/dist/modules/access-control/gql/federation.d.ts +1 -1
- package/dist/modules/access-control/gql/federation.js +10 -17
- package/dist/modules/access-control/gql/schema.d.ts +1 -1
- package/dist/modules/access-control/gql/schema.generated.d.ts +136 -110
- package/dist/modules/access-control/gql/schema.generated.js +22 -25
- package/dist/modules/access-control/gql/schema.js +5 -9
- package/dist/modules/access-control/gql/types.d.ts +2 -2
- package/dist/modules/access-control/gql/types.js +13 -17
- package/dist/modules/access-control/grpc/index.d.ts +5 -5
- package/dist/modules/access-control/grpc/index.js +10 -14
- package/dist/modules/access-control/index.d.ts +2 -2
- package/dist/modules/access-control/index.js +20 -17
- package/dist/modules/access-control/interfaces.d.ts +3 -3
- package/dist/modules/access-control/interfaces.js +1 -4
- package/dist/modules/catalog/gql/federation.d.ts +1 -1
- package/dist/modules/catalog/gql/federation.js +10 -17
- package/dist/modules/catalog/gql/schema.d.ts +1 -1
- package/dist/modules/catalog/gql/schema.generated.d.ts +252 -226
- package/dist/modules/catalog/gql/schema.generated.js +20 -23
- package/dist/modules/catalog/gql/schema.js +5 -9
- package/dist/modules/catalog/gql/types.d.ts +2 -2
- package/dist/modules/catalog/gql/types.js +15 -19
- package/dist/modules/catalog/grpc/index.d.ts +6 -6
- package/dist/modules/catalog/grpc/index.js +12 -16
- package/dist/modules/catalog/index.d.ts +2 -2
- package/dist/modules/catalog/index.js +20 -17
- package/dist/modules/catalog/interfaces.d.ts +3 -3
- package/dist/modules/catalog/interfaces.js +1 -4
- package/dist/modules/facade-status/gql/index.js +8 -14
- package/dist/modules/facade-status/gql/resolvers.d.ts +1 -1
- package/dist/modules/facade-status/gql/resolvers.js +14 -8
- package/dist/modules/facade-status/gql/schema.d.ts +1 -1
- package/dist/modules/facade-status/gql/schema.generated.d.ts +34 -11
- package/dist/modules/facade-status/gql/schema.generated.js +1 -2
- package/dist/modules/facade-status/gql/schema.js +6 -9
- package/dist/modules/facade-status/gql/types.js +3 -6
- package/dist/modules/facade-status/index.d.ts +1 -1
- package/dist/modules/facade-status/index.js +4 -7
- package/dist/modules/facade-status/interfaces.d.ts +1 -1
- package/dist/modules/facade-status/interfaces.js +1 -2
- package/dist/modules/fulfillment/gql/federation.d.ts +1 -1
- package/dist/modules/fulfillment/gql/federation.js +10 -17
- package/dist/modules/fulfillment/gql/schema.d.ts +1 -1
- package/dist/modules/fulfillment/gql/schema.generated.d.ts +249 -223
- package/dist/modules/fulfillment/gql/schema.generated.js +20 -23
- package/dist/modules/fulfillment/gql/schema.js +5 -9
- package/dist/modules/fulfillment/gql/types.d.ts +2 -2
- package/dist/modules/fulfillment/gql/types.js +11 -15
- package/dist/modules/fulfillment/grpc/index.d.ts +3 -3
- package/dist/modules/fulfillment/grpc/index.js +6 -10
- package/dist/modules/fulfillment/index.d.ts +2 -2
- package/dist/modules/fulfillment/index.js +20 -17
- package/dist/modules/fulfillment/interfaces.d.ts +3 -3
- package/dist/modules/fulfillment/interfaces.js +1 -4
- package/dist/modules/identity/api-key/api-key.d.ts +2 -2
- package/dist/modules/identity/api-key/api-key.js +45 -65
- package/dist/modules/identity/gql/federation.d.ts +2 -2
- package/dist/modules/identity/gql/federation.js +5 -9
- package/dist/modules/identity/gql/schema.d.ts +1 -1
- package/dist/modules/identity/gql/schema.generated.d.ts +284 -255
- package/dist/modules/identity/gql/schema.generated.js +20 -23
- package/dist/modules/identity/gql/schema.js +5 -9
- package/dist/modules/identity/gql/types.d.ts +2 -2
- package/dist/modules/identity/gql/types.js +17 -21
- package/dist/modules/identity/grpc/index.d.ts +6 -6
- package/dist/modules/identity/grpc/index.js +12 -16
- package/dist/modules/identity/index.d.ts +4 -4
- package/dist/modules/identity/index.js +24 -21
- package/dist/modules/identity/interfaces.d.ts +5 -5
- package/dist/modules/identity/interfaces.js +1 -4
- package/dist/modules/identity/oauth/oauth.d.ts +4 -4
- package/dist/modules/identity/oauth/oauth.js +109 -135
- package/dist/modules/identity/oidc/adapter.d.ts +3 -3
- package/dist/modules/identity/oidc/adapter.js +111 -92
- package/dist/modules/identity/oidc/in-memory-adapter.d.ts +1 -1
- package/dist/modules/identity/oidc/in-memory-adapter.js +64 -50
- package/dist/modules/identity/oidc/index.d.ts +6 -6
- package/dist/modules/identity/oidc/index.js +29 -30
- package/dist/modules/identity/oidc/interfaces.d.ts +6 -6
- package/dist/modules/identity/oidc/interfaces.js +2 -6
- package/dist/modules/identity/oidc/password-grant.d.ts +1 -1
- package/dist/modules/identity/oidc/password-grant.js +43 -66
- package/dist/modules/identity/oidc/router.d.ts +6 -6
- package/dist/modules/identity/oidc/router.js +31 -26
- package/dist/modules/identity/oidc/templates.d.ts +1 -1
- package/dist/modules/identity/oidc/templates.js +45 -41
- package/dist/modules/identity/oidc/user.d.ts +4 -4
- package/dist/modules/identity/oidc/user.js +31 -29
- package/dist/modules/identity/oidc/utils.d.ts +2 -2
- package/dist/modules/identity/oidc/utils.js +6 -13
- package/dist/modules/index.d.ts +13 -13
- package/dist/modules/index.js +13 -29
- package/dist/modules/indexing/gql/federation.d.ts +1 -1
- package/dist/modules/indexing/gql/federation.js +10 -17
- package/dist/modules/indexing/gql/schema.d.ts +1 -1
- package/dist/modules/indexing/gql/schema.generated.d.ts +45 -19
- package/dist/modules/indexing/gql/schema.generated.js +2 -5
- package/dist/modules/indexing/gql/schema.js +5 -9
- package/dist/modules/indexing/gql/types.d.ts +2 -2
- package/dist/modules/indexing/gql/types.js +7 -11
- package/dist/modules/indexing/grpc/index.d.ts +2 -2
- package/dist/modules/indexing/grpc/index.js +4 -8
- package/dist/modules/indexing/index.d.ts +2 -2
- package/dist/modules/indexing/index.js +20 -17
- package/dist/modules/indexing/interfaces.d.ts +3 -3
- package/dist/modules/indexing/interfaces.js +1 -4
- package/dist/modules/invoicing/gql/federation.d.ts +1 -1
- package/dist/modules/invoicing/gql/federation.js +10 -17
- package/dist/modules/invoicing/gql/schema.d.ts +1 -1
- package/dist/modules/invoicing/gql/schema.generated.d.ts +209 -183
- package/dist/modules/invoicing/gql/schema.generated.js +20 -23
- package/dist/modules/invoicing/gql/schema.js +5 -9
- package/dist/modules/invoicing/gql/types.d.ts +2 -2
- package/dist/modules/invoicing/gql/types.js +7 -11
- package/dist/modules/invoicing/grpc/index.d.ts +2 -2
- package/dist/modules/invoicing/grpc/index.js +4 -8
- package/dist/modules/invoicing/index.d.ts +2 -2
- package/dist/modules/invoicing/index.js +20 -17
- package/dist/modules/invoicing/interfaces.d.ts +3 -3
- package/dist/modules/invoicing/interfaces.js +1 -4
- package/dist/modules/notification/gql/federation.d.ts +1 -1
- package/dist/modules/notification/gql/federation.js +10 -17
- package/dist/modules/notification/gql/schema.d.ts +1 -1
- package/dist/modules/notification/gql/schema.generated.d.ts +100 -74
- package/dist/modules/notification/gql/schema.generated.js +18 -21
- package/dist/modules/notification/gql/schema.js +5 -9
- package/dist/modules/notification/gql/types.d.ts +2 -2
- package/dist/modules/notification/gql/types.js +7 -11
- package/dist/modules/notification/grpc/index.d.ts +2 -2
- package/dist/modules/notification/grpc/index.js +4 -8
- package/dist/modules/notification/index.d.ts +2 -2
- package/dist/modules/notification/index.js +20 -17
- package/dist/modules/notification/interfaces.d.ts +3 -3
- package/dist/modules/notification/interfaces.js +1 -4
- package/dist/modules/ordering/gql/federation.d.ts +1 -1
- package/dist/modules/ordering/gql/federation.js +10 -17
- package/dist/modules/ordering/gql/schema.d.ts +1 -1
- package/dist/modules/ordering/gql/schema.generated.d.ts +221 -195
- package/dist/modules/ordering/gql/schema.generated.js +22 -25
- package/dist/modules/ordering/gql/schema.js +5 -9
- package/dist/modules/ordering/gql/types.d.ts +2 -2
- package/dist/modules/ordering/gql/types.js +7 -11
- package/dist/modules/ordering/grpc/index.d.ts +2 -2
- package/dist/modules/ordering/grpc/index.js +4 -8
- package/dist/modules/ordering/index.d.ts +2 -2
- package/dist/modules/ordering/index.js +20 -17
- package/dist/modules/ordering/interfaces.d.ts +3 -3
- package/dist/modules/ordering/interfaces.js +1 -4
- package/dist/modules/ostorage/gql/federation.d.ts +1 -1
- package/dist/modules/ostorage/gql/federation.js +10 -17
- package/dist/modules/ostorage/gql/schema.d.ts +1 -1
- package/dist/modules/ostorage/gql/schema.generated.d.ts +126 -94
- package/dist/modules/ostorage/gql/schema.generated.js +8 -11
- package/dist/modules/ostorage/gql/schema.js +5 -9
- package/dist/modules/ostorage/gql/types.d.ts +2 -2
- package/dist/modules/ostorage/gql/types.js +7 -11
- package/dist/modules/ostorage/grpc/index.d.ts +2 -2
- package/dist/modules/ostorage/grpc/index.js +4 -8
- package/dist/modules/ostorage/index.d.ts +2 -2
- package/dist/modules/ostorage/index.js +20 -17
- package/dist/modules/ostorage/interfaces.d.ts +3 -3
- package/dist/modules/ostorage/interfaces.js +1 -4
- package/dist/modules/payment/gql/federation.d.ts +1 -1
- package/dist/modules/payment/gql/federation.js +10 -17
- package/dist/modules/payment/gql/schema.d.ts +1 -1
- package/dist/modules/payment/gql/schema.generated.d.ts +67 -44
- package/dist/modules/payment/gql/schema.generated.js +4 -7
- package/dist/modules/payment/gql/schema.js +5 -9
- package/dist/modules/payment/gql/types.d.ts +2 -2
- package/dist/modules/payment/gql/types.js +7 -11
- package/dist/modules/payment/grpc/index.d.ts +2 -2
- package/dist/modules/payment/grpc/index.js +4 -8
- package/dist/modules/payment/index.d.ts +2 -2
- package/dist/modules/payment/index.js +20 -17
- package/dist/modules/payment/interfaces.d.ts +3 -3
- package/dist/modules/payment/interfaces.js +1 -4
- package/dist/modules/resource/gql/federation.d.ts +1 -1
- package/dist/modules/resource/gql/federation.js +10 -17
- package/dist/modules/resource/gql/schema.d.ts +1 -1
- package/dist/modules/resource/gql/schema.generated.d.ts +348 -322
- package/dist/modules/resource/gql/schema.generated.js +28 -31
- package/dist/modules/resource/gql/schema.js +5 -9
- package/dist/modules/resource/gql/types.d.ts +2 -2
- package/dist/modules/resource/gql/types.js +31 -35
- package/dist/modules/resource/grpc/index.d.ts +13 -13
- package/dist/modules/resource/grpc/index.js +28 -32
- package/dist/modules/resource/index.d.ts +2 -2
- package/dist/modules/resource/index.js +20 -17
- package/dist/modules/resource/interfaces.d.ts +3 -3
- package/dist/modules/resource/interfaces.js +1 -4
- package/dist/modules/scheduling/gql/federation.d.ts +1 -1
- package/dist/modules/scheduling/gql/federation.js +10 -17
- package/dist/modules/scheduling/gql/schema.d.ts +1 -1
- package/dist/modules/scheduling/gql/schema.generated.d.ts +109 -83
- package/dist/modules/scheduling/gql/schema.generated.js +10 -13
- package/dist/modules/scheduling/gql/schema.js +5 -9
- package/dist/modules/scheduling/gql/types.d.ts +2 -2
- package/dist/modules/scheduling/gql/types.js +7 -11
- package/dist/modules/scheduling/grpc/index.d.ts +2 -2
- package/dist/modules/scheduling/grpc/index.js +4 -8
- package/dist/modules/scheduling/index.d.ts +2 -2
- package/dist/modules/scheduling/index.js +20 -17
- package/dist/modules/scheduling/interfaces.d.ts +3 -3
- package/dist/modules/scheduling/interfaces.js +1 -4
- package/dist/utils.d.ts +3 -3
- package/dist/utils.js +4 -9
- package/generate.ts +42 -42
- package/jest.config.cjs +23 -0
- package/package.json +17 -12
- package/tsconfig-base.json +8 -0
- package/tsconfig.debug.json +5 -1
- package/tsconfig.test.json +17 -2
- package/jest.config.js +0 -14
@@ -1,95 +1,142 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
+
});
|
9
|
+
};
|
10
|
+
import { readFile } from 'node:fs';
|
11
|
+
import path, { resolve as resolvePath } from 'node:path';
|
12
|
+
import hbs from 'handlebars';
|
13
|
+
import { marshallProtobufAny } from '../oidc/utils.js';
|
14
|
+
import * as uuid from 'uuid';
|
15
|
+
import { RegisterRequest, UserType } from '@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/user.js';
|
16
|
+
import * as jose from 'jose';
|
17
|
+
import * as url from 'node:url';
|
18
|
+
import Router from 'koa-router';
|
19
|
+
import { koaBody as bodyParser } from 'koa-body';
|
20
|
+
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
|
21
|
+
const upsertUserToken = (ids, accountId) => __awaiter(void 0, void 0, void 0, function* () {
|
22
|
+
const token = new jose.UnsecuredJWT({})
|
23
|
+
.setIssuedAt()
|
24
|
+
.setExpirationTime('30d')
|
25
|
+
.encode();
|
26
|
+
// 1 Month
|
27
|
+
const expiresIn = Date.now() + (1000 * 60 * 60 * 24 * 30);
|
28
|
+
yield ids.token.upsert({
|
29
|
+
id: uuid.v4().replace(/-/g, ''),
|
30
|
+
type: 'access_token',
|
31
|
+
expiresIn,
|
32
|
+
payload: marshallProtobufAny({
|
33
|
+
accountId,
|
34
|
+
exp: expiresIn,
|
35
|
+
jti: token
|
36
|
+
})
|
37
|
+
});
|
38
|
+
return token;
|
39
|
+
});
|
40
|
+
let layoutHbs;
|
41
|
+
export const layout = (context) => __awaiter(void 0, void 0, void 0, function* () {
|
42
|
+
if (!layoutHbs) {
|
43
|
+
const layoutTpl = yield new Promise((resolve, reject) => {
|
44
|
+
readFile(resolvePath(__dirname, 'views/layout.hbs'), (err, data) => err ? reject(err) : resolve(data.toString()));
|
45
|
+
});
|
46
|
+
layoutHbs = hbs.compile(layoutTpl);
|
7
47
|
}
|
8
|
-
|
9
|
-
}) : (function(o, m, k, k2) {
|
10
|
-
if (k2 === undefined) k2 = k;
|
11
|
-
o[k2] = m[k];
|
12
|
-
}));
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
-
}) : function(o, v) {
|
16
|
-
o["default"] = v;
|
48
|
+
return layoutHbs(context);
|
17
49
|
});
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
return (
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
const
|
33
|
-
|
34
|
-
const
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
}
|
42
|
-
|
50
|
+
let registerHbs;
|
51
|
+
export const register = (email) => __awaiter(void 0, void 0, void 0, function* () {
|
52
|
+
if (!registerHbs) {
|
53
|
+
const registerTpl = yield new Promise((resolve, reject) => {
|
54
|
+
readFile(resolvePath(__dirname, 'views/register.hbs'), (err, data) => err ? reject(err) : resolve(data.toString()));
|
55
|
+
});
|
56
|
+
registerHbs = hbs.compile(registerTpl);
|
57
|
+
}
|
58
|
+
return layout({
|
59
|
+
title: 'Register',
|
60
|
+
body: registerHbs({ email })
|
61
|
+
});
|
62
|
+
});
|
63
|
+
let loginHbs;
|
64
|
+
export const login = (links) => __awaiter(void 0, void 0, void 0, function* () {
|
65
|
+
if (!loginHbs) {
|
66
|
+
const loginTpl = yield new Promise((resolve, reject) => {
|
67
|
+
readFile(resolvePath(__dirname, 'views/login.hbs'), (err, data) => err ? reject(err) : resolve(data.toString()));
|
68
|
+
});
|
69
|
+
loginHbs = hbs.compile(loginTpl);
|
70
|
+
}
|
71
|
+
return layout({
|
72
|
+
title: 'Login',
|
73
|
+
body: loginHbs({ links })
|
74
|
+
});
|
75
|
+
});
|
76
|
+
let accountHbs;
|
77
|
+
export const account = (user) => __awaiter(void 0, void 0, void 0, function* () {
|
78
|
+
if (!accountHbs) {
|
79
|
+
const accountTpl = yield new Promise((resolve, reject) => {
|
80
|
+
readFile(resolvePath(__dirname, 'views/account.hbs'), (err, data) => err ? reject(err) : resolve(data.toString()));
|
81
|
+
});
|
82
|
+
accountHbs = hbs.compile(accountTpl);
|
83
|
+
}
|
84
|
+
return layout({
|
85
|
+
title: 'Account',
|
86
|
+
body: accountHbs({ user })
|
87
|
+
});
|
88
|
+
});
|
89
|
+
export const createOAuth = () => {
|
43
90
|
const router = new Router();
|
44
91
|
router.use(bodyParser({ multipart: true }));
|
45
|
-
router.get('/oauth2-login',
|
92
|
+
router.get('/oauth2-login', (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
|
46
93
|
ctx.type = 'html';
|
47
|
-
ctx.body =
|
94
|
+
ctx.body = yield login((yield ctx.identitySrvClient.oauth.GenerateLinks({})).links);
|
48
95
|
return next();
|
49
|
-
});
|
50
|
-
router.get('/oauth2-logout',
|
96
|
+
}));
|
97
|
+
router.get('/oauth2-logout', (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
|
51
98
|
ctx.cookies.set('token', undefined);
|
52
99
|
ctx.status = 303;
|
53
100
|
ctx.redirect('/oauth2-login');
|
54
101
|
ctx.body = 'Redirecting to login page';
|
55
102
|
return next();
|
56
|
-
});
|
57
|
-
router.get('/oauth2-urls',
|
58
|
-
ctx.body = (
|
103
|
+
}));
|
104
|
+
router.get('/oauth2-urls', (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
|
105
|
+
ctx.body = (yield ctx.identitySrvClient.oauth.GenerateLinks({})).links;
|
59
106
|
return next();
|
60
|
-
});
|
61
|
-
router.get('/oauth2-account',
|
107
|
+
}));
|
108
|
+
router.get('/oauth2-account', (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
|
62
109
|
const token = ctx.cookies.get('token');
|
63
110
|
if (!token) {
|
64
111
|
ctx.body = 'user not logged in';
|
65
112
|
return next();
|
66
113
|
}
|
67
114
|
const ids = ctx.identitySrvClient;
|
68
|
-
const user =
|
69
|
-
token
|
115
|
+
const user = yield ids.user.findByToken({
|
116
|
+
token
|
70
117
|
});
|
71
118
|
if (!user || !user.payload) {
|
72
119
|
ctx.body = 'user not logged in';
|
73
120
|
return next();
|
74
121
|
}
|
75
122
|
ctx.type = 'html';
|
76
|
-
ctx.body =
|
123
|
+
ctx.body = yield account(user.payload);
|
77
124
|
return next();
|
78
|
-
});
|
79
|
-
router.post('/oauth2-register',
|
125
|
+
}));
|
126
|
+
router.post('/oauth2-register', (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
|
80
127
|
const ids = ctx.identitySrvClient;
|
81
128
|
const body = ctx.request.body;
|
82
|
-
const user =
|
129
|
+
const user = yield ids.user.register(RegisterRequest.fromPartial({
|
83
130
|
email: body.email,
|
84
131
|
name: body.username,
|
85
132
|
firstName: body.first_name,
|
86
133
|
lastName: body.last_name,
|
87
134
|
password: body.password,
|
88
|
-
userType:
|
135
|
+
userType: UserType.INDIVIDUAL_USER,
|
89
136
|
guest: false,
|
90
137
|
}));
|
91
138
|
if (user.payload) {
|
92
|
-
const token =
|
139
|
+
const token = yield upsertUserToken(ids, user.payload.id);
|
93
140
|
ctx.cookies.set('token', token);
|
94
141
|
ctx.status = 303;
|
95
142
|
ctx.redirect('/oauth2-account');
|
@@ -100,17 +147,17 @@ const createOAuth = () => {
|
|
100
147
|
ctx.body = user.status.message;
|
101
148
|
}
|
102
149
|
return next();
|
103
|
-
});
|
104
|
-
router.get('/oauth2/:service',
|
150
|
+
}));
|
151
|
+
router.get('/oauth2/:service', (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
|
105
152
|
const ids = ctx.identitySrvClient;
|
106
|
-
const user =
|
153
|
+
const user = yield ids.oauth.exchangeCode({
|
107
154
|
service: ctx.params.service,
|
108
155
|
code: ctx.request.query['code'],
|
109
156
|
state: ctx.request.query['state']
|
110
157
|
});
|
111
158
|
if (!user.user || !user.user.payload || !user.token || (user.user.status && user.user.status.code !== 200)) {
|
112
159
|
ctx.type = 'html';
|
113
|
-
ctx.body =
|
160
|
+
ctx.body = yield register(user.email || '');
|
114
161
|
return next();
|
115
162
|
}
|
116
163
|
ctx.cookies.set('token', user.token.token);
|
@@ -118,79 +165,6 @@ const createOAuth = () => {
|
|
118
165
|
ctx.redirect('/oauth2-account');
|
119
166
|
ctx.body = 'Redirecting to account page';
|
120
167
|
return next();
|
121
|
-
});
|
168
|
+
}));
|
122
169
|
return router;
|
123
170
|
};
|
124
|
-
exports.createOAuth = createOAuth;
|
125
|
-
const upsertUserToken = async (ids, accountId) => {
|
126
|
-
const token = new jose.UnsecuredJWT({})
|
127
|
-
.setIssuedAt()
|
128
|
-
.setExpirationTime('30d')
|
129
|
-
.encode();
|
130
|
-
// 1 Month
|
131
|
-
const expiresIn = Date.now() + (1000 * 60 * 60 * 24 * 30);
|
132
|
-
await ids.token.upsert({
|
133
|
-
id: uuid.v4().replace(/-/g, ''),
|
134
|
-
type: 'access_token',
|
135
|
-
expiresIn: expiresIn,
|
136
|
-
payload: (0, utils_1.marshallProtobufAny)({
|
137
|
-
accountId: accountId,
|
138
|
-
exp: expiresIn,
|
139
|
-
jti: token
|
140
|
-
})
|
141
|
-
});
|
142
|
-
return token;
|
143
|
-
};
|
144
|
-
let layoutHbs;
|
145
|
-
const layout = async (context) => {
|
146
|
-
if (!layoutHbs) {
|
147
|
-
const layoutTpl = await new Promise((resolve, reject) => {
|
148
|
-
(0, fs_1.readFile)((0, path_1.resolve)(__dirname, 'views/layout.hbs'), (err, data) => err ? reject(err) : resolve(data.toString()));
|
149
|
-
});
|
150
|
-
layoutHbs = handlebars_1.default.compile(layoutTpl);
|
151
|
-
}
|
152
|
-
return layoutHbs(context);
|
153
|
-
};
|
154
|
-
exports.layout = layout;
|
155
|
-
let registerHbs;
|
156
|
-
const register = async (email) => {
|
157
|
-
if (!registerHbs) {
|
158
|
-
const registerTpl = await new Promise((resolve, reject) => {
|
159
|
-
(0, fs_1.readFile)((0, path_1.resolve)(__dirname, 'views/register.hbs'), (err, data) => err ? reject(err) : resolve(data.toString()));
|
160
|
-
});
|
161
|
-
registerHbs = handlebars_1.default.compile(registerTpl);
|
162
|
-
}
|
163
|
-
return (0, exports.layout)({
|
164
|
-
title: 'Register',
|
165
|
-
body: registerHbs({ email })
|
166
|
-
});
|
167
|
-
};
|
168
|
-
exports.register = register;
|
169
|
-
let loginHbs;
|
170
|
-
const login = async (links) => {
|
171
|
-
if (!loginHbs) {
|
172
|
-
const loginTpl = await new Promise((resolve, reject) => {
|
173
|
-
(0, fs_1.readFile)((0, path_1.resolve)(__dirname, 'views/login.hbs'), (err, data) => err ? reject(err) : resolve(data.toString()));
|
174
|
-
});
|
175
|
-
loginHbs = handlebars_1.default.compile(loginTpl);
|
176
|
-
}
|
177
|
-
return (0, exports.layout)({
|
178
|
-
title: 'Login',
|
179
|
-
body: loginHbs({ links })
|
180
|
-
});
|
181
|
-
};
|
182
|
-
exports.login = login;
|
183
|
-
let accountHbs;
|
184
|
-
const account = async (user) => {
|
185
|
-
if (!accountHbs) {
|
186
|
-
const accountTpl = await new Promise((resolve, reject) => {
|
187
|
-
(0, fs_1.readFile)((0, path_1.resolve)(__dirname, 'views/account.hbs'), (err, data) => err ? reject(err) : resolve(data.toString()));
|
188
|
-
});
|
189
|
-
accountHbs = handlebars_1.default.compile(accountTpl);
|
190
|
-
}
|
191
|
-
return (0, exports.layout)({
|
192
|
-
title: 'Account',
|
193
|
-
body: accountHbs({ user })
|
194
|
-
});
|
195
|
-
};
|
196
|
-
exports.account = account;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { Logger } from 'winston';
|
2
|
-
import { Adapter, AdapterConstructor } from 'oidc-provider';
|
3
|
-
import { TokenServiceClient as tokenService } from
|
1
|
+
import { type Logger } from 'winston';
|
2
|
+
import { type Adapter, type AdapterConstructor } from 'oidc-provider';
|
3
|
+
import { type TokenServiceClient as tokenService } from '@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/token.js';
|
4
4
|
export declare function createIdentityServiceAdapterClass(remoteTokenService: tokenService, logger: Logger, localTokenServiceFactory?: (type: string) => Adapter): AdapterConstructor;
|
@@ -1,116 +1,135 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
+
});
|
9
|
+
};
|
10
|
+
import { marshallProtobufAny, unmarshallProtobufAny } from './utils.js';
|
11
|
+
import { Subject } from '@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/auth.js';
|
12
|
+
import { InMemoryAdapter } from './in-memory-adapter.js';
|
7
13
|
const delegateToRemoteService = (type) => ['AccessToken', 'RefreshToken'].includes(type);
|
8
|
-
function createIdentityServiceAdapterClass(remoteTokenService, logger, localTokenServiceFactory) {
|
14
|
+
export function createIdentityServiceAdapterClass(remoteTokenService, logger, localTokenServiceFactory) {
|
9
15
|
return class IdentityServiceAdapter {
|
10
16
|
constructor(type) {
|
11
17
|
this.type = type;
|
12
|
-
this.localTokenService = localTokenServiceFactory ? localTokenServiceFactory(type) : new
|
18
|
+
this.localTokenService = localTokenServiceFactory ? localTokenServiceFactory(type) : new InMemoryAdapter(type);
|
13
19
|
}
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
consume(id) {
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
22
|
+
logger.verbose(`Consuming ${this.type} token ${id}`);
|
23
|
+
if (delegateToRemoteService(this.type)) {
|
24
|
+
try {
|
25
|
+
yield remoteTokenService.consume({
|
26
|
+
id,
|
27
|
+
type: this.type,
|
28
|
+
subject: undefined
|
29
|
+
});
|
30
|
+
}
|
31
|
+
catch (error) {
|
32
|
+
logger.error(`Error consuming ${this.type} token ${id}`, error);
|
33
|
+
}
|
23
34
|
}
|
24
|
-
|
25
|
-
|
35
|
+
else {
|
36
|
+
yield this.localTokenService.consume(id);
|
26
37
|
}
|
27
|
-
}
|
28
|
-
else {
|
29
|
-
await this.localTokenService.consume(id);
|
30
|
-
}
|
38
|
+
});
|
31
39
|
}
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
40
|
+
destroy(id) {
|
41
|
+
return __awaiter(this, void 0, void 0, function* () {
|
42
|
+
logger.verbose(`Destroying ${this.type} token ${id}`);
|
43
|
+
if (delegateToRemoteService(this.type)) {
|
44
|
+
try {
|
45
|
+
yield remoteTokenService.destroy({
|
46
|
+
id,
|
47
|
+
type: this.type,
|
48
|
+
subject: Subject.fromPartial({ token: id })
|
49
|
+
});
|
50
|
+
}
|
51
|
+
catch (error) {
|
52
|
+
logger.error(`Error destroying ${this.type} token ${id}`, error);
|
53
|
+
}
|
41
54
|
}
|
42
|
-
|
43
|
-
|
55
|
+
else {
|
56
|
+
yield this.localTokenService.destroy(id);
|
44
57
|
}
|
45
|
-
}
|
46
|
-
else {
|
47
|
-
await this.localTokenService.destroy(id);
|
48
|
-
}
|
58
|
+
});
|
49
59
|
}
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
+
find(id) {
|
61
|
+
return __awaiter(this, void 0, void 0, function* () {
|
62
|
+
logger.verbose(`Finding ${this.type} token ${id}`);
|
63
|
+
if (delegateToRemoteService(this.type)) {
|
64
|
+
try {
|
65
|
+
let result = yield remoteTokenService.find({
|
66
|
+
id,
|
67
|
+
type: this.type,
|
68
|
+
subject: Subject.fromPartial({ token: id })
|
69
|
+
});
|
70
|
+
return result ? unmarshallProtobufAny(result) : undefined;
|
71
|
+
}
|
72
|
+
catch (error) {
|
73
|
+
logger.error(`Error finding ${this.type} token ${id}`, error);
|
74
|
+
return undefined;
|
75
|
+
}
|
60
76
|
}
|
61
|
-
|
62
|
-
|
63
|
-
return undefined;
|
77
|
+
else {
|
78
|
+
return yield this.localTokenService.find(id);
|
64
79
|
}
|
65
|
-
}
|
66
|
-
else {
|
67
|
-
return await this.localTokenService.find(id);
|
68
|
-
}
|
80
|
+
});
|
69
81
|
}
|
70
|
-
|
71
|
-
|
72
|
-
|
82
|
+
findByUid(uid) {
|
83
|
+
return __awaiter(this, void 0, void 0, function* () {
|
84
|
+
logger.verbose(`Finding by Uid ${uid}`);
|
85
|
+
return yield this.localTokenService.findByUid(uid);
|
86
|
+
});
|
73
87
|
}
|
74
88
|
// Not needed. DeviceFlow only
|
75
|
-
|
76
|
-
|
77
|
-
|
89
|
+
findByUserCode(userCode) {
|
90
|
+
return __awaiter(this, arguments, void 0, function* () {
|
91
|
+
logger.error('Finding br UserCode is unsupported', this.type, ...arguments);
|
92
|
+
throw new Error('UNSUPPORTED');
|
93
|
+
});
|
78
94
|
}
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
95
|
+
upsert(id, payload, expiresIn) {
|
96
|
+
return __awaiter(this, void 0, void 0, function* () {
|
97
|
+
logger.error(`Upserting ${this.type} token ${id}`, payload);
|
98
|
+
if (delegateToRemoteService(this.type)) {
|
99
|
+
try {
|
100
|
+
yield remoteTokenService.upsert({
|
101
|
+
expiresIn,
|
102
|
+
id,
|
103
|
+
payload: marshallProtobufAny(payload),
|
104
|
+
type: this.type,
|
105
|
+
subject: undefined
|
106
|
+
});
|
107
|
+
}
|
108
|
+
catch (error) {
|
109
|
+
logger.error(`Error upserting ${this.type} token ${id}`, error);
|
110
|
+
}
|
90
111
|
}
|
91
|
-
|
92
|
-
|
112
|
+
else {
|
113
|
+
return this.localTokenService.upsert(id, payload, expiresIn * 1000);
|
93
114
|
}
|
94
|
-
}
|
95
|
-
else {
|
96
|
-
return this.localTokenService.upsert(id, payload, expiresIn * 1000);
|
97
|
-
}
|
115
|
+
});
|
98
116
|
}
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
117
|
+
revokeByGrantId(grantId) {
|
118
|
+
return __awaiter(this, void 0, void 0, function* () {
|
119
|
+
logger.error(`Revoking grant id ${this.type} token ${grantId}`);
|
120
|
+
yield this.localTokenService.revokeByGrantId(grantId);
|
121
|
+
if (delegateToRemoteService(this.type)) {
|
122
|
+
try {
|
123
|
+
yield remoteTokenService.revokeByGrantId({
|
124
|
+
grantId,
|
125
|
+
subject: Subject.fromPartial({ token: grantId })
|
126
|
+
});
|
127
|
+
}
|
128
|
+
catch (error) {
|
129
|
+
logger.error(`Error revoking grant id ${grantId}`, error);
|
130
|
+
}
|
108
131
|
}
|
109
|
-
|
110
|
-
logger.error(`Error revoking grant id ${grantId}`, error);
|
111
|
-
}
|
112
|
-
}
|
132
|
+
});
|
113
133
|
}
|
114
134
|
};
|
115
135
|
}
|
116
|
-
exports.createIdentityServiceAdapterClass = createIdentityServiceAdapterClass;
|