-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.ts
More file actions
253 lines (235 loc) · 8.45 KB
/
index.ts
File metadata and controls
253 lines (235 loc) · 8.45 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// For Default export
import pino from 'pino';
import { ApiClient, IPermitApi } from './api/api-client';
import { ElementsClient, IPermitElementsApi } from './api/elements';
import { ConfigFactory, IPermitConfig } from './config';
import { Enforcer, IEnforcer } from './enforcement/enforcer';
import {
ICheckQuery,
IResource,
IUser,
IUserPermissions,
TenantDetails,
} from './enforcement/interfaces';
import { LoggerFactory } from './logger';
import { CheckConfig, Context } from './utils/context';
import { AxiosLoggingInterceptor } from './utils/http-logger';
import { resolveRetryConfig } from './utils/retry';
import { AxiosRetryInterceptor } from './utils/retry-interceptor';
import { RecursivePartial } from './utils/types';
// exported interfaces
export * from './api';
export { IPermitConfig } from './config';
export { IUser, IAction, IResource } from './enforcement/interfaces';
export { PermitConnectionError, PermitError, PermitPDPStatusError } from './enforcement/enforcer';
export { Context, ContextTransform } from './utils/context';
export { ApiContext, PermitContextError, ApiKeyLevel } from './api/context';
export { PermitApiError } from './api/base';
export {
IRetryConfig,
RetryConditionFn,
RETRYABLE_STATUS_CODES,
NON_RETRYABLE_STATUS_CODES,
} from './utils/retry';
export interface IPermitClient extends IEnforcer {
/**
* Access the SDK configuration using this property.
* Once the SDK is initialized, the configuration is read-only.
*/
config: IPermitConfig;
/**
* Access the Permit REST API using this property.
*/
api: IPermitApi;
/**
* Access the Permit Elements API using this property.
*/
elements: IPermitElementsApi;
}
/**
* The `Permit` class represents the main entry point for interacting with the Permit.io SDK.
* The SDK constructor expects an object implementing the {@link IPermitConfig} interface.
*
* Example usage:
*
* ```typescript
* import { Permit } from 'permitio';
*
* const permit = new Permit({
* // this is typically the same API Key you would use for the PDP container
* token: "[YOUR_API_KEY]",
* // in production, you might need to change this url to fit your deployment
* pdp: "http://localhost:7766",
* ...
* });
*
* // creates (or updates) a user on that can be assigned roles and permissions
* const { user } = await permit.api.users.sync({
* // the user key must be a unique id of the user
* key: 'auth0|elon',
* // optional params
* email: 'elonmusk@tesla.com',
* first_name: 'Elon',
* last_name: 'Musk',
* // user attributes can be used in attribute-based access-control policies
* attributes: {
* age: 50,
* favoriteColor: 'red',
* },
* });
*
* // 'document' is the protected resource we are enforcing access to
* const resource = 'document';
* // the action the user is trying to do on the resource
* const action = 'read';
*
* const permitted = await permit.check(user, action, resource);
* if (permitted) {
* console.log('User is authorized to read a document.');
* } else {
* console.log('User is not authorized to read a document.');
* }
* ```
*/
export class Permit implements IPermitClient {
private logger: pino.Logger;
private enforcer: IEnforcer;
/**
* Access the SDK configuration using this property.
* Once the SDK is initialized, the configuration is read-only.
*
* Usage example:
*
* ```typescript
* const permit = new Permit(config);
* const pdpUrl = permit.config.pdp;
* ```
*/
public readonly config: IPermitConfig;
/**
* Access the Permit REST API using this property.
*
* Usage example:
*
* ```typescript
* const permit = new Permit(config);
* permit.api.roles.create(...);
* ```
*/
public readonly api: IPermitApi;
/**
* Access the Permit Elements API using this property.
*
* Usage example:
*
* ```typescript
* const permit = new Permit(config);
* permit.elements.loginAs(user, tenant);
* ```
*/
public readonly elements: IPermitElementsApi;
/**
* Constructs a new instance of the {@link Permit} class with the specified configuration.
*
* @param config - The configuration for the Permit SDK.
*/
constructor(config: RecursivePartial<IPermitConfig>) {
this.config = ConfigFactory.build(config);
this.logger = LoggerFactory.createLogger(this.config);
AxiosLoggingInterceptor.setupInterceptor(this.config.axiosInstance, this.logger);
// Setup retry interceptor for REST API calls
const resolvedRetryConfig = resolveRetryConfig(this.config.retry);
if (resolvedRetryConfig.enabled) {
AxiosRetryInterceptor.setupInterceptor(
this.config.axiosInstance,
resolvedRetryConfig,
this.logger,
'API',
);
}
this.api = new ApiClient(this.config, this.logger);
this.enforcer = new Enforcer(this.config, this.logger);
this.elements = new ElementsClient(this.config, this.logger);
this.logger.debug(
`Permit.io SDK initialized with config:\n${JSON.stringify(this.config, undefined, 2)}`,
);
}
/**
* Checks if a `user` is authorized to perform an `action` on a `resource` within the specified context.
*
* @param user - The user object representing the user.
* @param action - The action to be performed on the resource.
* @param resource - The resource object representing the resource.
* @param context - The context object representing the context in which the action is performed.
* @returns `true` if the user is authorized, `false` otherwise.
* @throws {@link PermitConnectionError} if an error occurs while sending the authorization request to the PDP.
* @throws {@link PermitPDPStatusError} if received a response with unexpected status code from the PDP.
*/
public async check(
user: string | IUser,
action: string,
resource: string | IResource,
context?: Context | undefined,
config?: CheckConfig | undefined,
): Promise<boolean> {
return await this.enforcer.check(user, action, resource, context, config);
}
/**
* Checks multiple requests within the specified context.
*
* @param checks - The check requests.
* @param context - The context object representing the context in which the action is performed.
* @returns array containing `true` if the user is authorized, `false` otherwise for each check request.
* @throws {@link PermitConnectionError} if an error occurs while sending the authorization request to the PDP.
* @throws {@link PermitPDPStatusError} if received a response with unexpected status code from the PDP.
*/
public async bulkCheck(
checks: Array<ICheckQuery>,
context?: Context | undefined,
config?: CheckConfig | undefined,
): Promise<Array<boolean>> {
return await this.enforcer.bulkCheck(checks, context, config);
}
/**
* Get all tenants available in the system.
* @returns An array of TenantDetails representing all tenants.
*/
/**
* Get all tenants available in the system.
* @returns An array of TenantDetails representing all tenants.
*/
public async checkAllTenants(
user: IUser | string,
action: string,
resource: IResource | string,
context?: Context | undefined,
sdk?: string | undefined,
): Promise<TenantDetails[]> {
try {
return await this.enforcer.checkAllTenants(user, action, resource, context, sdk);
} catch (error) {
this.logger.error('Error fetching all tenants:', error);
throw error;
}
}
/**
* Get all permissions for the specified user.
*
* @param user - The user object representing the user.
* @param tenants - The list of tenants to filter the permissions on ( given by roles ).
* @param resources - The list of resources to filter the permissions on ( given by resource roles ).
* @param resource_types - The list of resource types to filter the permissions on ( given by resource roles ).
* @returns object with key as the resource identifier and value as the resource details and permissions.
* @throws {@link PermitConnectionError} if an error occurs while sending the authorization request to the PDP.
* @throws {@link PermitPDPStatusError} if received a response with unexpected status code from the PDP.
*/
public async getUserPermissions(
user: IUser | string,
tenants?: string[],
resources?: string[],
resource_types?: string[],
config?: CheckConfig,
): Promise<IUserPermissions> {
return await this.enforcer.getUserPermissions(user, tenants, resources, resource_types, config);
}
}