Skip to content

Commit 4e4e160

Browse files
committed
[KEYCLOAK-13841] Update typescript declarations for keycloak-nodejs-connect
1 parent 49f313d commit 4e4e160

File tree

1 file changed

+109
-7
lines changed

1 file changed

+109
-7
lines changed

keycloak.d.ts

+109-7
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,60 @@ declare namespace KeycloakConnect {
3232
cookies?: boolean
3333
}
3434

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
4168
}
4269

4370
interface Token {
71+
clientId?: string
72+
token?: string
73+
content?: TokenContent
4474
isExpired(): boolean
4575
hasRole(roleName: string): boolean
4676
hasApplicationRole(appName: string, roleName: string): boolean
4777
hasRealmRole(roleName: string): boolean
4878
}
4979

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+
5089
interface GrantManager {
5190
/**
5291
* Use the direct grant API to obtain a grant from Keycloak.
@@ -280,7 +319,67 @@ declare namespace KeycloakConnect {
280319
*
281320
* @param {String} spec The protection spec (optional)
282321
*/
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
284383

285384
/**
286385
* Callback made upon successful authentication of a user.
@@ -336,6 +435,9 @@ declare namespace KeycloakConnect {
336435

337436
getGrantFromCode(code: string, req: express.Request, res: express.Response): Promise<Grant>
338437

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+
339441
loginUrl(uuid: string, redirectUrl: string): string
340442

341443
logoutUrl(redirectUrl: string): string

0 commit comments

Comments
 (0)