@fraym/auth 0.5.3 → 0.6.1

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/README.md CHANGED
@@ -15,34 +15,12 @@ There is a sandbox available at `http://auth:3000/management/graphql/sandbox`.
15
15
 
16
16
  You need to add the `Tenant-Id` header in order to use the graphQL Endpoint and the sandbox.
17
17
 
18
- ## CLI command
19
-
20
- Use the `auth` cli command to automatically apply your permissions to the auth service.
21
-
22
- You can specify the address (and port) of the auth service instance you use in the `AUTH_SERVER_ADDRESS` env variable (default: `127.0.0.1:9000`).
23
-
24
- In case of scopes api you need to configure the HTTP api:
25
-
26
- - `AUTH_HTTP_SERVER_ADDRESS`: Http api url of the auth service (default: `http://127.0.0.1`)
27
- - `AUTH_HTTP_API_TOKEN`: The value of that token has to match the token configured in the auth service
28
-
29
- The needed schema for auth is a simple enum containing all your permissions. Example:
30
-
31
- ```graphql
32
- enum Permission {
33
- USER_READ
34
- USER_WRITE
35
- }
36
- ```
37
-
38
18
  ### Config
39
19
 
40
20
  Use a `.env` file or env variables to configure cte clients and the command:
41
21
 
42
22
  ```env
43
23
  AUTH_SERVER_ADDRESS=127.0.0.1:9000
44
- AUTH_HTTP_SERVER_ADDRESS=http://127.0.0.1
45
- AUTH_HTTP_API_TOKEN=
46
24
  ```
47
25
 
48
26
  ## JWT functions
@@ -93,42 +71,10 @@ Parameters:
93
71
 
94
72
  ### Create the client
95
73
 
96
- management client:
97
-
98
74
  ```typescript
99
75
  const managementClient = await newManagementClient();
100
76
  ```
101
77
 
102
- ## Get all scopes (permissions)
103
-
104
- The `clientId` paramenter is optional. If none is given the default client will be used.
105
-
106
- ```typescript
107
- const scopes = await managementClient.getScopes();
108
- ```
109
-
110
- Note: you need to configure `AUTH_HTTP_SERVER_ADDRESS` and `AUTH_HTTP_API_TOKEN` to use this function.
111
-
112
- ## Create a scope (permission)
113
-
114
- The `clientId` paramenter is optional. If none is given the default client will be used.
115
-
116
- ```typescript
117
- await managementClient.createScope("PERMISSION_NAME");
118
- ```
119
-
120
- Note: you need to configure `AUTH_HTTP_SERVER_ADDRESS` and `AUTH_HTTP_API_TOKEN` to use this function.
121
-
122
- ## Delete a scope (permission)
123
-
124
- The `clientId` paramenter is optional. If none is given the default client will be used.
125
-
126
- ```typescript
127
- await managementClient.deleteScope("PERMISSION_NAME");
128
- ```
129
-
130
- Note: you need to configure `AUTH_HTTP_SERVER_ADDRESS` and `AUTH_HTTP_API_TOKEN` to use this function.
131
-
132
78
  ## Get all roles
133
79
 
134
80
  ```typescript
@@ -1,7 +1,5 @@
1
1
  export interface ClientConfig {
2
2
  serverAddress: string;
3
- httpServerAddress: string;
4
- httpApiToken: string;
5
3
  keepaliveInterval?: number;
6
4
  keepaliveTimeout?: number;
7
5
  }
@@ -3,11 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useConfigDefaults = exports.getEnvConfig = void 0;
4
4
  const dotenv_1 = require("dotenv");
5
5
  const getEnvConfig = () => {
6
- var _a, _b, _c;
6
+ var _a;
7
7
  (0, dotenv_1.config)();
8
8
  const serverAddress = (_a = process.env.AUTH_SERVER_ADDRESS) !== null && _a !== void 0 ? _a : "";
9
- const httpServerAddress = (_b = process.env.AUTH_HTTP_SERVER_ADDRESS) !== null && _b !== void 0 ? _b : "";
10
- const httpApiToken = (_c = process.env.AUTH_HTTP_API_TOKEN) !== null && _c !== void 0 ? _c : "";
11
9
  let keepaliveInterval;
12
10
  let keepaliveTimeout;
13
11
  const keepaliveIntervalString = process.env.AUTH_CONNECTION_KEEPALIVE_INTERVAL;
@@ -20,8 +18,6 @@ const getEnvConfig = () => {
20
18
  }
21
19
  return {
22
20
  serverAddress,
23
- httpServerAddress,
24
- httpApiToken,
25
21
  keepaliveInterval,
26
22
  keepaliveTimeout,
27
23
  };
@@ -34,8 +30,6 @@ const useConfigDefaults = (config) => {
34
30
  }
35
31
  return {
36
32
  serverAddress: config.serverAddress,
37
- httpServerAddress: config.httpServerAddress,
38
- httpApiToken: config.httpApiToken,
39
33
  keepaliveTimeout: (_a = config.keepaliveTimeout) !== null && _a !== void 0 ? _a : 3 * 1000,
40
34
  keepaliveInterval: (_b = config.keepaliveInterval) !== null && _b !== void 0 ? _b : 40 * 1000,
41
35
  };
@@ -4,9 +4,6 @@ import { Role } from "./getRoles";
4
4
  import { User } from "./getUsers";
5
5
  import { UpsertRoleScope } from "./upsertRole";
6
6
  export interface ManagementClient {
7
- createScope: (name: string, clientId?: string) => Promise<void>;
8
- deleteScope: (name: string, clientId?: string) => Promise<void>;
9
- getScopes: (clientId?: string) => Promise<string[]>;
10
7
  upsertRole: (tenantId: string, allowedScopes: UpsertRoleScope[], id?: string) => Promise<string>;
11
8
  deleteRole: (tenantId: string, id: string) => Promise<void>;
12
9
  getRoles: (tenantId: string) => Promise<Role[]>;
@@ -4,13 +4,10 @@ exports.newManagementClient = void 0;
4
4
  const auth_proto_1 = require("@fraym/auth-proto");
5
5
  const grpc_js_1 = require("@grpc/grpc-js");
6
6
  const config_1 = require("../config/config");
7
- const createScope_1 = require("./createScope");
8
7
  const createUser_1 = require("./createUser");
9
8
  const deleteRole_1 = require("./deleteRole");
10
- const deleteScope_1 = require("./deleteScope");
11
9
  const deleteUser_1 = require("./deleteUser");
12
10
  const getRoles_1 = require("./getRoles");
13
- const getScopes_1 = require("./getScopes");
14
11
  const getUsers_1 = require("./getUsers");
15
12
  const updateUser_1 = require("./updateUser");
16
13
  const upsertRole_1 = require("./upsertRole");
@@ -21,15 +18,6 @@ const newManagementClient = async (config) => {
21
18
  "grpc.keepalive_timeout_ms": currentConfig.keepaliveTimeout,
22
19
  "grpc.keepalive_permit_without_calls": 1,
23
20
  });
24
- const createScope = async (name, clientId = "") => {
25
- await (0, createScope_1.createNewScope)(name, clientId, currentConfig);
26
- };
27
- const deleteScope = async (name, clientId = "") => {
28
- await (0, deleteScope_1.deleteExistingScope)(name, clientId, currentConfig);
29
- };
30
- const getScopes = async (clientId = "") => {
31
- return await (0, getScopes_1.getAllScopes)(clientId, currentConfig);
32
- };
33
21
  const upsertRole = async (tenantId, allowedScopes, id = "") => {
34
22
  return await (0, upsertRole_1.createOrUpdateRole)(tenantId, id, allowedScopes, serviceClient);
35
23
  };
@@ -55,9 +43,6 @@ const newManagementClient = async (config) => {
55
43
  serviceClient.close();
56
44
  };
57
45
  return {
58
- createScope,
59
- deleteScope,
60
- getScopes,
61
46
  upsertRole,
62
47
  deleteRole,
63
48
  getRoles,
@@ -26,7 +26,9 @@ exports.generateJwt = generateJwt;
26
26
  const addDataToJwt = async (appSecret, token, data) => {
27
27
  var _a;
28
28
  const secret = new TextEncoder().encode(appSecret);
29
- const { payload, protectedHeader } = await (0, jose_1.jwtVerify)(token, secret);
29
+ const { payload, protectedHeader } = await (0, jose_1.jwtVerify)(token, secret, {
30
+ clockTolerance: "10 seconds",
31
+ });
30
32
  if (!payload.exp) {
31
33
  throw Error("expiration time is missing in JWT");
32
34
  }
@@ -42,7 +44,9 @@ exports.addDataToJwt = addDataToJwt;
42
44
  const getTokenData = async (appSecret, token, requireUserId = true) => {
43
45
  var _a, _b, _c;
44
46
  const secret = new TextEncoder().encode(appSecret);
45
- const { payload } = await (0, jose_1.jwtVerify)(token, secret);
47
+ const { payload } = await (0, jose_1.jwtVerify)(token, secret, {
48
+ clockTolerance: "10 seconds",
49
+ });
46
50
  if (!payload.exp) {
47
51
  throw Error("expiration time is missing in JWT");
48
52
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fraym/auth",
3
- "version": "0.5.3",
3
+ "version": "0.6.1",
4
4
  "license": "MIT",
5
5
  "homepage": "https://github.com/fraym/auth-nodejs",
6
6
  "repository": {
@@ -12,7 +12,7 @@
12
12
  "test": "echo \"Error: no test specified\" && exit 0",
13
13
  "format": "prettier --write \"**/*.{ts,tsx,json}\"",
14
14
  "lint": "prettier --check \"**/*.{ts,tsx,json}\"",
15
- "build": "npm run clean && tsc && chmod +x dist/cmd/auth.js",
15
+ "build": "npm run clean && tsc",
16
16
  "clean": "rm -rf dist",
17
17
  "prepublishOnly": "npm test && npm run lint && npm run build",
18
18
  "preversion": "npm run lint",
@@ -23,9 +23,6 @@
23
23
  ],
24
24
  "main": "dist/index.js",
25
25
  "types": "dist/index.d.ts",
26
- "bin": {
27
- "auth": "dist/cmd/auth.js"
28
- },
29
26
  "dependencies": {
30
27
  "@fraym/auth-proto": "^0.6.0",
31
28
  "@graphql-tools/graphql-file-loader": "^7.5.11",
@@ -1,2 +0,0 @@
1
- #! /usr/bin/env node
2
- export {};
package/dist/cmd/auth.js DELETED
@@ -1,88 +0,0 @@
1
- #! /usr/bin/env node
2
- "use strict";
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- const yargs_1 = __importDefault(require("yargs/yargs"));
8
- const helpers_1 = require("yargs/helpers");
9
- const dotenv_1 = require("dotenv");
10
- const graphql_file_loader_1 = require("@graphql-tools/graphql-file-loader");
11
- const load_1 = require("@graphql-tools/load");
12
- const graphql_1 = require("graphql");
13
- const client_1 = require("../management/client");
14
- const run = async () => {
15
- (0, dotenv_1.config)();
16
- const argv = await (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
17
- .config({
18
- schemaGlob: "./src/**/*.graphql",
19
- serverAddress: "127.0.0.1:9000",
20
- httpServerAddress: "http://127.0.0.1",
21
- httpApiToken: "",
22
- })
23
- .pkgConf("auth").argv;
24
- let schemaGlob = argv.schemaGlob;
25
- let serverAddress = argv.serverAddress;
26
- let httpServerAddress = argv.httpServerAddress;
27
- let httpApiToken = argv.httpApiToken;
28
- if (process.env.AUTH_SCHEMA_GLOB) {
29
- schemaGlob = process.env.AUTH_SCHEMA_GLOB;
30
- }
31
- if (process.env.AUTH_SERVER_ADDRESS) {
32
- serverAddress = process.env.AUTH_SERVER_ADDRESS;
33
- }
34
- if (process.env.AUTH_HTTP_SERVER_ADDRESS) {
35
- httpServerAddress = process.env.AUTH_HTTP_SERVER_ADDRESS;
36
- }
37
- if (process.env.AUTH_HTTP_API_TOKEN) {
38
- httpApiToken = process.env.AUTH_HTTP_API_TOKEN;
39
- }
40
- const schema = await (0, load_1.loadSchema)(`${schemaGlob}`, {
41
- loaders: [new graphql_file_loader_1.GraphQLFileLoader()],
42
- });
43
- const permissions = getSchemaPermissions(schema);
44
- await migratePermissions(permissions, serverAddress, httpServerAddress, httpApiToken);
45
- };
46
- const getSchemaPermissions = (schema) => {
47
- const permissions = [];
48
- schema.toConfig().types.forEach(t => {
49
- var _a, _b;
50
- if (!(t instanceof graphql_1.GraphQLEnumType)) {
51
- return;
52
- }
53
- const name = t.toString();
54
- if (name !== "Permission") {
55
- return;
56
- }
57
- (_b = (_a = t.astNode) === null || _a === void 0 ? void 0 : _a.values) === null || _b === void 0 ? void 0 : _b.forEach(value => {
58
- permissions.push(value.name.value);
59
- });
60
- });
61
- return permissions;
62
- };
63
- const migratePermissions = async (permissions, serverAddress, httpServerAddress, httpApiToken) => {
64
- const managementClient = await (0, client_1.newManagementClient)({
65
- serverAddress,
66
- httpServerAddress,
67
- httpApiToken,
68
- });
69
- const existingPermissions = (await managementClient.getScopes()).filter(permission => !permission.startsWith("FRAYM_"));
70
- console.log("existingPermissions", existingPermissions);
71
- const permissionsToCreate = permissions.filter(permission => !existingPermissions.includes(permission));
72
- const permissionsToDelete = existingPermissions.filter(permission => !permissions.includes(permission));
73
- if (permissionsToCreate.length > 0) {
74
- console.log(`Creating ${permissionsToCreate.length} permissions: ${permissionsToCreate}...`);
75
- for (let i = 0; i < permissionsToCreate.length; i++) {
76
- await managementClient.createScope(permissionsToCreate[i]);
77
- }
78
- console.log(`Created ${permissionsToCreate.length} permissions`);
79
- }
80
- if (permissionsToDelete.length > 0) {
81
- console.log(`Removing ${permissionsToDelete.length} permissions: ${permissionsToDelete}...`);
82
- for (let i = 0; i < permissionsToDelete.length; i++) {
83
- await managementClient.deleteScope(permissionsToDelete[i]);
84
- }
85
- console.log(`Removed ${permissionsToDelete.length} permissions`);
86
- }
87
- };
88
- run();
@@ -1,2 +0,0 @@
1
- import { ClientConfig } from "config/config";
2
- export declare const createNewScope: (name: string, clientId: string, config: ClientConfig) => Promise<void>;
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createNewScope = void 0;
4
- const createNewScope = async (name, clientId, config) => {
5
- await fetch(`${config.httpServerAddress}/management/scopes`, {
6
- method: "POST",
7
- headers: {
8
- Authorization: `Bearer ${config.httpApiToken}`,
9
- "Content-Type": "application/json",
10
- },
11
- body: JSON.stringify({
12
- clientId,
13
- name,
14
- }),
15
- });
16
- };
17
- exports.createNewScope = createNewScope;
@@ -1,2 +0,0 @@
1
- import { ClientConfig } from "config/config";
2
- export declare const deleteExistingScope: (name: string, clientId: string, config: ClientConfig) => Promise<void>;
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.deleteExistingScope = void 0;
4
- const deleteExistingScope = async (name, clientId, config) => {
5
- await fetch(`${config.httpServerAddress}/management/scopes`, {
6
- method: "DELETE",
7
- headers: {
8
- Authorization: `Bearer ${config.httpApiToken}`,
9
- "Content-Type": "application/json",
10
- },
11
- body: JSON.stringify({
12
- clientId,
13
- name,
14
- }),
15
- });
16
- };
17
- exports.deleteExistingScope = deleteExistingScope;
@@ -1,2 +0,0 @@
1
- import { ClientConfig } from "config/config";
2
- export declare const getAllScopes: (clientId: string, config: ClientConfig) => Promise<string[]>;
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getAllScopes = void 0;
4
- const getAllScopes = async (clientId, config) => {
5
- const response = await fetch(`${config.httpServerAddress}/management/scopes/list`, {
6
- method: "POST",
7
- headers: {
8
- Authorization: `Bearer ${config.httpApiToken}`,
9
- "Content-Type": "application/json",
10
- },
11
- body: JSON.stringify({
12
- clientId,
13
- }),
14
- });
15
- const data = await response.json();
16
- return data.scopes;
17
- };
18
- exports.getAllScopes = getAllScopes;