@restorecommerce/facade 0.1.44 → 0.2.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/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [0.2.0](https://github.com/restorecommerce/libs/compare/@restorecommerce/facade@0.1.44...@restorecommerce/facade@0.2.0) (2022-02-09)
7
+
8
+
9
+ ### Features
10
+
11
+ * add oauth ([3e7798e](https://github.com/restorecommerce/libs/commit/3e7798e3aa10ef092872928f5254cd5fbb125f3b))
12
+
13
+
14
+
15
+
16
+
6
17
  ## [0.1.44](https://github.com/restorecommerce/libs/compare/@restorecommerce/facade@0.1.43...@restorecommerce/facade@0.1.44) (2022-02-02)
7
18
 
8
19
 
package/debug-run.ts CHANGED
@@ -51,6 +51,7 @@ function createTestFacade() {
51
51
  ],
52
52
  jwks,
53
53
  },
54
+ oauth: true,
54
55
  apiKey: 'api_key'
55
56
  }))
56
57
  .useModule(resourceModule({config: serviceConfig.get('resource')}))
@@ -194,13 +194,20 @@ const getGQLResolverFunctions = (service, key, serviceKey, grpcClientConfig) =>
194
194
  });
195
195
  return { details: aggregatedResponse, operationStatus };
196
196
  }
197
- let items = utils_1.decodeBufferFields(result.items, bufferFields);
198
- return {
199
- details: {
200
- items: items,
201
- operationStatus: result.operationStatus // overall status
202
- },
203
- };
197
+ if ('items' in result) {
198
+ let items = utils_1.decodeBufferFields(result.items, bufferFields);
199
+ return {
200
+ details: {
201
+ items: items,
202
+ operationStatus: result.operationStatus // overall status
203
+ },
204
+ };
205
+ }
206
+ else {
207
+ return {
208
+ details: utils_1.decodeBufferFields([result], bufferFields)[0]
209
+ };
210
+ }
204
211
  }
205
212
  catch (error) {
206
213
  console.error(error);
@@ -5,9 +5,10 @@ const user_1 = require("@restorecommerce/rc-grpc-clients/dist/generated/io/resto
5
5
  const role_1 = require("@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/role");
6
6
  const authentication_log_1 = require("@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/authentication_log");
7
7
  const token_1 = require("@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/token");
8
+ const oauth_1 = require("@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/oauth");
8
9
  const protos_1 = require("../../../gql/protos");
9
10
  function registerTypings() {
10
- protos_1.registerPackagesRecursive(user_1.protoMetadata, role_1.protoMetadata, authentication_log_1.protoMetadata, token_1.protoMetadata);
11
+ protos_1.registerPackagesRecursive(user_1.protoMetadata, role_1.protoMetadata, authentication_log_1.protoMetadata, token_1.protoMetadata, oauth_1.protoMetadata);
11
12
  }
12
13
  exports.registerTypings = registerTypings;
13
14
  exports.subServices = [
@@ -3,6 +3,7 @@ import { Service as userService } from "@restorecommerce/rc-grpc-clients/dist/ge
3
3
  import { Service as roleService } from "@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/role";
4
4
  import { Service as authentication_logService } from "@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/authentication_log";
5
5
  import { Service as tokenService } from "@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/token";
6
+ import { Service as oauthService } from "@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/oauth";
6
7
  import { GrpcClientConfig } from "@restorecommerce/grpc-client";
7
8
  import { Logger } from "winston";
8
9
  export declare class IdentitySrvGrpcClient extends RestoreCommerceGrpcClient {
@@ -11,4 +12,5 @@ export declare class IdentitySrvGrpcClient extends RestoreCommerceGrpcClient {
11
12
  role: roleService;
12
13
  authentication_log: authentication_logService;
13
14
  token: tokenService;
15
+ oauth: oauthService;
14
16
  }
@@ -6,6 +6,7 @@ const user_1 = require("@restorecommerce/rc-grpc-clients/dist/generated/io/resto
6
6
  const role_1 = require("@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/role");
7
7
  const authentication_log_1 = require("@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/authentication_log");
8
8
  const token_1 = require("@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/token");
9
+ const oauth_1 = require("@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/oauth");
9
10
  const protos_1 = require("../../../gql/protos");
10
11
  class IdentitySrvGrpcClient extends rc_grpc_clients_1.RestoreCommerceGrpcClient {
11
12
  constructor(cfg, logger) {
@@ -14,6 +15,7 @@ class IdentitySrvGrpcClient extends rc_grpc_clients_1.RestoreCommerceGrpcClient
14
15
  this.role = protos_1.getGRPCService(this, role_1.protobufPackage, role_1.protoMetadata.fileDescriptor.service[0]);
15
16
  this.authentication_log = protos_1.getGRPCService(this, authentication_log_1.protobufPackage, authentication_log_1.protoMetadata.fileDescriptor.service[0]);
16
17
  this.token = protos_1.getGRPCService(this, token_1.protobufPackage, token_1.protoMetadata.fileDescriptor.service[0]);
18
+ this.oauth = protos_1.getGRPCService(this, oauth_1.protobufPackage, oauth_1.protoMetadata.fileDescriptor.service[0]);
17
19
  }
18
20
  }
19
21
  exports.IdentitySrvGrpcClient = IdentitySrvGrpcClient;
@@ -7,6 +7,7 @@ const federation_1 = require("./gql/federation");
7
7
  const oidc_1 = require("./oidc");
8
8
  const api_key_1 = require("./api-key/api-key");
9
9
  const grpc_1 = require("./grpc");
10
+ const oauth_1 = require("./oauth/oauth");
10
11
  exports.identityModule = utils_1.createFacadeModuleFactory('identity', (facade, config) => {
11
12
  const identitySrvClient = new grpc_1.IdentitySrvGrpcClient(config.identitySrvClientConfig, facade.logger);
12
13
  if (!!config.apiKey) {
@@ -29,6 +30,9 @@ exports.identityModule = utils_1.createFacadeModuleFactory('identity', (facade,
29
30
  facade.koa.use(router.routes());
30
31
  facade.koa.use(koa_mount_1.default(provider.app));
31
32
  }
33
+ if (config.oauth) {
34
+ facade.koa.use(oauth_1.createOAuth().routes());
35
+ }
32
36
  const identity = {
33
37
  client: identitySrvClient
34
38
  };
@@ -11,6 +11,7 @@ export interface IdentityConfig {
11
11
  identitySrvClientConfig: GrpcClientConfig;
12
12
  oidc?: OIDCConfig;
13
13
  apiKey?: boolean | string;
14
+ oauth?: boolean;
14
15
  }
15
16
  export interface IdentityContext extends FacadeContext {
16
17
  identity: {
@@ -0,0 +1,11 @@
1
+ import Router from "koa-router";
2
+ import { IdentityContext } from "../interfaces";
3
+ import { User } from "@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/user";
4
+ export declare const createOAuth: () => Router<{}, IdentityContext>;
5
+ export declare const layout: (context: {
6
+ body: string;
7
+ title: string;
8
+ }) => Promise<string>;
9
+ export declare const register: (email: string) => Promise<string>;
10
+ export declare const login: (links: any) => Promise<string>;
11
+ export declare const account: (user: User) => Promise<string>;
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.account = exports.login = exports.register = exports.layout = exports.createOAuth = void 0;
4
+ const koa_router_1 = require("koa-router");
5
+ const fs_1 = require("fs");
6
+ const path_1 = require("path");
7
+ const handlebars_1 = require("handlebars");
8
+ const utils_1 = require("../oidc/utils");
9
+ const uuid = require("uuid");
10
+ const user_1 = require("@restorecommerce/rc-grpc-clients/dist/generated/io/restorecommerce/user");
11
+ const koa_body_1 = require("koa-body");
12
+ const createOAuth = () => {
13
+ const router = new koa_router_1.default();
14
+ router.use(koa_body_1.default({ multipart: true }));
15
+ router.get('/oauth2-login', async (ctx, next) => {
16
+ ctx.type = 'html';
17
+ ctx.body = await exports.login((await ctx.identitySrvClient.oauth.GenerateLinks({})).links);
18
+ return next();
19
+ });
20
+ router.get('/oauth2-urls', async (ctx, next) => {
21
+ ctx.body = (await ctx.identitySrvClient.oauth.GenerateLinks({})).links;
22
+ return next();
23
+ });
24
+ router.post('/oauth2-register', async (ctx, next) => {
25
+ const ids = ctx.identitySrvClient;
26
+ const body = ctx.request.body;
27
+ const user = await ids.user.Register(user_1.RegisterRequest.fromPartial({
28
+ email: body.email,
29
+ name: body.username,
30
+ firstName: body.first_name,
31
+ lastName: body.last_name,
32
+ password: body.password,
33
+ userType: user_1.UserType.INDIVIDUAL_USER,
34
+ guest: false,
35
+ }));
36
+ if (user.payload) {
37
+ ctx.type = 'html';
38
+ ctx.body = await exports.account(user.payload);
39
+ return next();
40
+ }
41
+ if (user.status) {
42
+ ctx.body = user.status.message;
43
+ }
44
+ return next();
45
+ });
46
+ router.get('/oauth2/:service', async (ctx, next) => {
47
+ const ids = ctx.identitySrvClient;
48
+ const user = await ids.oauth.ExchangeCode({
49
+ service: ctx.params.service,
50
+ code: ctx.request.query['code'],
51
+ state: ctx.request.query['state']
52
+ });
53
+ console.log(user);
54
+ if (!user.user || !user.user.payload || (user.user.status && user.user.status.code !== 200)) {
55
+ if (user.email) {
56
+ ctx.type = 'html';
57
+ ctx.body = await exports.register(user.email);
58
+ return next();
59
+ }
60
+ else {
61
+ ctx.body = 'Internal Error';
62
+ return next();
63
+ }
64
+ }
65
+ const token = utils_1.nanoid();
66
+ // 1 Month
67
+ const expiresIn = Date.now() + (1000 * 60 * 60 * 24 * 30);
68
+ await ids.token.upsert({
69
+ id: uuid.v4().replace(/-/g, ''),
70
+ type: 'access_token',
71
+ expiresIn: expiresIn,
72
+ payload: utils_1.marshallProtobufAny({
73
+ accountId: user.user.payload.id,
74
+ exp: expiresIn,
75
+ jti: token
76
+ })
77
+ });
78
+ ctx.type = 'html';
79
+ ctx.body = await exports.account(user.user.payload);
80
+ return next();
81
+ });
82
+ return router;
83
+ };
84
+ exports.createOAuth = createOAuth;
85
+ let layoutHbs;
86
+ const layout = async (context) => {
87
+ if (!layoutHbs) {
88
+ const layoutTpl = await new Promise((resolve, reject) => {
89
+ fs_1.default.readFile(path_1.default.resolve(__dirname, 'views/layout.hbs'), (err, data) => err ? reject(err) : resolve(data.toString()));
90
+ });
91
+ layoutHbs = handlebars_1.default.compile(layoutTpl);
92
+ }
93
+ return layoutHbs(context);
94
+ };
95
+ exports.layout = layout;
96
+ let registerHbs;
97
+ const register = async (email) => {
98
+ if (!registerHbs) {
99
+ const registerTpl = await new Promise((resolve, reject) => {
100
+ fs_1.default.readFile(path_1.default.resolve(__dirname, 'views/register.hbs'), (err, data) => err ? reject(err) : resolve(data.toString()));
101
+ });
102
+ registerHbs = handlebars_1.default.compile(registerTpl);
103
+ }
104
+ return exports.layout({
105
+ title: 'Register',
106
+ body: registerHbs({ email })
107
+ });
108
+ };
109
+ exports.register = register;
110
+ let loginHbs;
111
+ const login = async (links) => {
112
+ if (!loginHbs) {
113
+ const loginTpl = await new Promise((resolve, reject) => {
114
+ fs_1.default.readFile(path_1.default.resolve(__dirname, 'views/login.hbs'), (err, data) => err ? reject(err) : resolve(data.toString()));
115
+ });
116
+ loginHbs = handlebars_1.default.compile(loginTpl);
117
+ }
118
+ return exports.layout({
119
+ title: 'Login',
120
+ body: loginHbs({ links })
121
+ });
122
+ };
123
+ exports.login = login;
124
+ let accountHbs;
125
+ const account = async (user) => {
126
+ if (!accountHbs) {
127
+ const accountTpl = await new Promise((resolve, reject) => {
128
+ fs_1.default.readFile(path_1.default.resolve(__dirname, 'views/account.hbs'), (err, data) => err ? reject(err) : resolve(data.toString()));
129
+ });
130
+ accountHbs = handlebars_1.default.compile(accountTpl);
131
+ }
132
+ return exports.layout({
133
+ title: 'Account',
134
+ body: accountHbs({ user })
135
+ });
136
+ };
137
+ exports.account = account;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@restorecommerce/facade",
3
- "version": "0.1.44",
3
+ "version": "0.2.0",
4
4
  "description": "Facade for Restorecommerce microservices",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -24,13 +24,14 @@
24
24
  "@cloudnative/health": "^2.1.2",
25
25
  "@grpc/grpc-js": "^1.2.12",
26
26
  "@grpc/proto-loader": "^0.6.0",
27
- "@restorecommerce/grpc-client": "^0.2.14",
28
- "@restorecommerce/kafka-client": "^0.2.29",
29
- "@restorecommerce/logger": "^0.11.3",
30
- "@restorecommerce/rc-grpc-clients": "^0.3.0",
31
- "@restorecommerce/service-config": "^0.4.23",
27
+ "@restorecommerce/grpc-client": "^0.3.0",
28
+ "@restorecommerce/kafka-client": "^0.3.0",
29
+ "@restorecommerce/logger": "^0.12.0",
30
+ "@restorecommerce/rc-grpc-clients": "^0.4.0",
31
+ "@restorecommerce/service-config": "^0.4.24",
32
32
  "apollo-server-koa": "^2.21.2",
33
33
  "debug": "^4.3.1",
34
+ "googleapis": "^92.0.0",
34
35
  "handlebars": "^4.7.7",
35
36
  "koa": "^2.13.1",
36
37
  "koa-body": "^4.2.0",
@@ -39,6 +40,8 @@
39
40
  "koa-router": "^10.0.0",
40
41
  "lodash": "^4.17.21",
41
42
  "lru-cache": "^6.0.0",
43
+ "node-fetch": "^2.6.7",
44
+ "oauth": "^0.9.15",
42
45
  "oidc-provider": "^7.2.0",
43
46
  "querystring": "^0.2.1",
44
47
  "request-ip": "^2.1.3",
@@ -46,7 +49,7 @@
46
49
  "ts-proto-descriptors": "^1.2.0",
47
50
  "useragent": "^2.3.0",
48
51
  "uuid": "^8.3.2",
49
- "winston": "^3.3.3"
52
+ "winston": "^3.4.0"
50
53
  },
51
54
  "devDependencies": {
52
55
  "@graphql-codegen/core": "^1.17.9",
@@ -63,6 +66,7 @@
63
66
  "@types/lodash": "^4.14.177",
64
67
  "@types/lru-cache": "^5.1.0",
65
68
  "@types/node": "^14.14.35",
69
+ "@types/oauth": "^0.9.1",
66
70
  "@types/oidc-provider": "^7.1.1",
67
71
  "@types/request-ip": "^0.0.35",
68
72
  "@types/supertest": "^2.0.10",
@@ -101,5 +105,5 @@
101
105
  "publishConfig": {
102
106
  "access": "public"
103
107
  },
104
- "gitHead": "dd8b26367482e61ada3120acf5e96b23569580f0"
108
+ "gitHead": "97966555c4e421699a8a7c054f41ccc4385f618f"
105
109
  }