@@ -32,21 +32,60 @@ declare namespace KeycloakConnect {
32
32
cookies ?: boolean
33
33
}
34
34
35
- interface GrantProperties {
36
- access_token ?: string
37
- refresh_token ?: string
38
- id_token ?: string
39
- expires_in ?: string
40
- token_type ?: string
35
+ interface Claims {
36
+ // In the future it may make sense to populate this with some known claims
37
+ [ key : string ] : any
38
+ }
39
+
40
+ interface EnforcerOptions {
41
+ response_mode ?: 'permissions' | 'token'
42
+ resource_server_id ?: string
43
+ claims ?: Claims
44
+ }
45
+
46
+ interface Permission {
47
+ id : string
48
+ scopes ?: string [ ]
49
+ }
50
+
51
+ interface AuthzRequest {
52
+ audience ?: string
53
+ claim_token ?: string
54
+ claim_token_format ?: string
55
+ permissions : Permission [ ]
56
+ }
57
+
58
+ interface AuthzRequestGrant extends AuthzRequest {
59
+ response_mode : undefined
60
+ }
61
+
62
+ interface AuthzRequestOther extends AuthzRequest {
63
+ response_mode : 'decision' | 'permissions'
64
+ }
65
+
66
+ interface TokenContent {
67
+ exp : number
41
68
}
42
69
43
70
interface Token {
71
+ clientId ?: string
72
+ token ?: string
73
+ content ?: TokenContent
44
74
isExpired ( ) : boolean
45
75
hasRole ( roleName : string ) : boolean
46
76
hasApplicationRole ( appName : string , roleName : string ) : boolean
47
77
hasRealmRole ( roleName : string ) : boolean
48
78
}
49
79
80
+ interface GrantProperties {
81
+ access_token ?: Token
82
+ refresh_token ?: Token
83
+ id_token ?: Token
84
+ expires_in ?: number
85
+ token_type ?: string
86
+ __raw ?: string
87
+ }
88
+
50
89
interface GrantManager {
51
90
/**
52
91
* Use the direct grant API to obtain a grant from Keycloak.
@@ -280,7 +319,67 @@ declare namespace KeycloakConnect {
280
319
*
281
320
* @param {String } spec The protection spec (optional)
282
321
*/
283
- protect ( spec : GaurdFn | string ) : express . RequestHandler
322
+ protect ( spec ?: GaurdFn | string ) : express . RequestHandler
323
+
324
+ /**
325
+ * Enforce access based on the given permissions. This method operates in two modes, depending on the `response_mode`
326
+ * defined for this policy enforcer.
327
+ *
328
+ * If `response_mode` is set to `token`, permissions are obtained using an specific grant type. As a consequence, the
329
+ * token with the permissions granted by the server is updated and made available to the application via `request.kauth.grant.access_token`.
330
+ * Use this mode when your application is using sessions and you want to cache previous decisions from the server, as well automatically handle
331
+ * refresh tokens. This mode is especially useful for applications acting as client and resource server.
332
+ *
333
+ * If `response_mode` is set to `permissions`, the server only returns the list of granted permissions (no oauth2 response).
334
+ * Previous decisions are not cached and the policy enforcer will query the server every time to get a decision.
335
+ * This is the default `response_mode`.
336
+ *
337
+ * You can set `response_mode` as follows:
338
+ *
339
+ * keycloak.enforcer('item:read', {response_mode: 'token'})
340
+ *
341
+ * In all cases, if the request is already populated with a valid access token (for instance, bearer tokens sent by clients to the application),
342
+ * the policy enforcer will first try to resolve permissions from the current token before querying the server.
343
+ *
344
+ * By default, the policy enforcer will use the `client_id` defined to the application (for instance, via `keycloak.json`) to
345
+ * reference a client in Keycloak that supports Keycloak Authorization Services. In this case, the client can not be public given
346
+ * that it is actually a resource server.
347
+ *
348
+ * If your application is acting as a client and resource server, you can use the following configuration to specify the client
349
+ * in Keycloak with the authorization settings:
350
+ *
351
+ * keycloak.enforcer('item:read', {resource_server_id: 'nodejs-apiserver'})
352
+ *
353
+ * It is recommended to use separated clients in Keycloak to represent your frontend and backend.
354
+ *
355
+ * If the application you are protecting is enabled with Keycloak authorization services and you have defined client credentials
356
+ * in `keycloak.json`, you can push additional claims to the server and make them available to your policies in order to make decisions.
357
+ * For that, you can define a `claims` configuration option which expects a `function` that returns a JSON with the claims you want to push:
358
+ *
359
+ * app.get('/protected/resource', keycloak.enforcer(['resource:view', 'resource:write'], {
360
+ claims: function(request) {
361
+ return {
362
+ "http.uri": ["/protected/resource"],
363
+ "user.agent": // get user agent from request
364
+ }
365
+ }
366
+ }), function (req, res) {
367
+ // access granted
368
+ });
369
+ *
370
+ * @param {String[] } expectedPermissions A single string representing a permission or an arrat of strings representing the permissions. For instance, 'item:read' or ['item:read', 'item:write'].
371
+ */
372
+ enforcer ( expectedPermissions : string | string [ ] , options ?: EnforcerOptions ) : express . RequestHandler
373
+
374
+ /**
375
+ * Apply check SSO middleware to an application or specific URL.
376
+ *
377
+ * Check SSO will only authenticate the client if the user is already logged-in,
378
+ * if the user is not logged-in the browser will be redirected back
379
+ * to the originally-requested URL and remain unauthenticated.
380
+ *
381
+ */
382
+ checkSso ( ) : express . RequestHandler
284
383
285
384
/**
286
385
* Callback made upon successful authentication of a user.
@@ -336,6 +435,9 @@ declare namespace KeycloakConnect {
336
435
337
436
getGrantFromCode ( code : string , req : express . Request , res : express . Response ) : Promise < Grant >
338
437
438
+ checkPermissions ( authz_req : AuthzRequestGrant , req : express . Request , cb ?: ( grant : Grant ) => void ) : Promise < Grant >
439
+ checkPermissions ( authz_req : AuthzRequestOther , req : express . Request , cb ?: ( grant : Object ) => void ) : Promise < Object >
440
+
339
441
loginUrl ( uuid : string , redirectUrl : string ) : string
340
442
341
443
logoutUrl ( redirectUrl : string ) : string
0 commit comments