-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce pre-/post-auth request hooks for HttpServer (#36690)
* introduce pre-,post-auth stages * cleanup integration_tests. now contracts available in tests * auth per route is configurable * Unify lifecycle results structure * expose api to store auth state and status via http service * update tests * update docs * use full name, auth should not mutate request * move basePath functionality under namespace * regenerate docs * Revert "move basePath functionality under namespace" This reverts commit 9599d32. * Revert "regenerate docs" This reverts commit 1799d3b. * regenerate docs * updated yarn.lock no idea why * extract AuthStateStorage to a separate entity * get rid of nested ifs * describe what is the difference between hooks * re-wording
- Loading branch information
Showing
40 changed files
with
1,001 additions
and
337 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
docs/development/core/server/kibana-plugin-server.kibanarequest.url.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index) > [kibana-plugin-server](./kibana-plugin-server.md) > [KibanaRequest](./kibana-plugin-server.kibanarequest.md) > [url](./kibana-plugin-server.kibanarequest.url.md) | ||
|
||
## KibanaRequest.url property | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
readonly url: Url; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
docs/development/core/server/kibana-plugin-server.onpostauthhandler.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index) > [kibana-plugin-server](./kibana-plugin-server.md) > [OnPostAuthHandler](./kibana-plugin-server.onpostauthhandler.md) | ||
|
||
## OnPostAuthHandler type | ||
|
||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export declare type OnPostAuthHandler<Params = any, Query = any, Body = any> = (request: KibanaRequest<Params, Query, Body>, t: OnPostAuthToolkit) => OnPostAuthResult | Promise<OnPostAuthResult>; | ||
``` |
22 changes: 22 additions & 0 deletions
22
docs/development/core/server/kibana-plugin-server.onpostauthtoolkit.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index) > [kibana-plugin-server](./kibana-plugin-server.md) > [OnPostAuthToolkit](./kibana-plugin-server.onpostauthtoolkit.md) | ||
|
||
## OnPostAuthToolkit interface | ||
|
||
A tool set defining an outcome of OnPostAuth interceptor for incoming request. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export interface OnPostAuthToolkit | ||
``` | ||
|
||
## Properties | ||
|
||
| Property | Type | Description | | ||
| --- | --- | --- | | ||
| [next](./kibana-plugin-server.onpostauthtoolkit.next.md) | <code>() => OnPostAuthResult</code> | To pass request to the next handler | | ||
| [redirected](./kibana-plugin-server.onpostauthtoolkit.redirected.md) | <code>(url: string) => OnPostAuthResult</code> | To interrupt request handling and redirect to a configured url | | ||
| [rejected](./kibana-plugin-server.onpostauthtoolkit.rejected.md) | <code>(error: Error, options?: {`<p/>` statusCode?: number;`<p/>` }) => OnPostAuthResult</code> | Fail the request with specified error. | | ||
|
13 changes: 13 additions & 0 deletions
13
docs/development/core/server/kibana-plugin-server.onpostauthtoolkit.next.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index) > [kibana-plugin-server](./kibana-plugin-server.md) > [OnPostAuthToolkit](./kibana-plugin-server.onpostauthtoolkit.md) > [next](./kibana-plugin-server.onpostauthtoolkit.next.md) | ||
|
||
## OnPostAuthToolkit.next property | ||
|
||
To pass request to the next handler | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
next: () => OnPostAuthResult; | ||
``` |
13 changes: 13 additions & 0 deletions
13
docs/development/core/server/kibana-plugin-server.onpostauthtoolkit.redirected.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index) > [kibana-plugin-server](./kibana-plugin-server.md) > [OnPostAuthToolkit](./kibana-plugin-server.onpostauthtoolkit.md) > [redirected](./kibana-plugin-server.onpostauthtoolkit.redirected.md) | ||
|
||
## OnPostAuthToolkit.redirected property | ||
|
||
To interrupt request handling and redirect to a configured url | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
redirected: (url: string) => OnPostAuthResult; | ||
``` |
15 changes: 15 additions & 0 deletions
15
docs/development/core/server/kibana-plugin-server.onpostauthtoolkit.rejected.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index) > [kibana-plugin-server](./kibana-plugin-server.md) > [OnPostAuthToolkit](./kibana-plugin-server.onpostauthtoolkit.md) > [rejected](./kibana-plugin-server.onpostauthtoolkit.rejected.md) | ||
|
||
## OnPostAuthToolkit.rejected property | ||
|
||
Fail the request with specified error. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
rejected: (error: Error, options?: { | ||
statusCode?: number; | ||
}) => OnPostAuthResult; | ||
``` |
12 changes: 12 additions & 0 deletions
12
docs/development/core/server/kibana-plugin-server.onpreauthhandler.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index) > [kibana-plugin-server](./kibana-plugin-server.md) > [OnPreAuthHandler](./kibana-plugin-server.onpreauthhandler.md) | ||
|
||
## OnPreAuthHandler type | ||
|
||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export declare type OnPreAuthHandler<Params = any, Query = any, Body = any> = (request: KibanaRequest<Params, Query, Body>, t: OnPreAuthToolkit) => OnPreAuthResult | Promise<OnPreAuthResult>; | ||
``` |
22 changes: 22 additions & 0 deletions
22
docs/development/core/server/kibana-plugin-server.onpreauthtoolkit.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index) > [kibana-plugin-server](./kibana-plugin-server.md) > [OnPreAuthToolkit](./kibana-plugin-server.onpreauthtoolkit.md) | ||
|
||
## OnPreAuthToolkit interface | ||
|
||
A tool set defining an outcome of OnPreAuth interceptor for incoming request. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export interface OnPreAuthToolkit | ||
``` | ||
|
||
## Properties | ||
|
||
| Property | Type | Description | | ||
| --- | --- | --- | | ||
| [next](./kibana-plugin-server.onpreauthtoolkit.next.md) | <code>() => OnPreAuthResult</code> | To pass request to the next handler | | ||
| [redirected](./kibana-plugin-server.onpreauthtoolkit.redirected.md) | <code>(url: string, options?: {`<p/>` forward: boolean;`<p/>` }) => OnPreAuthResult</code> | To interrupt request handling and redirect to a configured url. If "options.forwarded" = true, request will be forwarded to another url right on the server. | | ||
| [rejected](./kibana-plugin-server.onpreauthtoolkit.rejected.md) | <code>(error: Error, options?: {`<p/>` statusCode?: number;`<p/>` }) => OnPreAuthResult</code> | Fail the request with specified error. | | ||
|
6 changes: 3 additions & 3 deletions
6
...na-plugin-server.onrequesttoolkit.next.md → ...na-plugin-server.onpreauthtoolkit.next.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index) > [kibana-plugin-server](./kibana-plugin-server.md) > [OnRequestToolkit](./kibana-plugin-server.onrequesttoolkit.md) > [next](./kibana-plugin-server.onrequesttoolkit.next.md) | ||
[Home](./index) > [kibana-plugin-server](./kibana-plugin-server.md) > [OnPreAuthToolkit](./kibana-plugin-server.onpreauthtoolkit.md) > [next](./kibana-plugin-server.onpreauthtoolkit.next.md) | ||
|
||
## OnRequestToolkit.next property | ||
## OnPreAuthToolkit.next property | ||
|
||
To pass request to the next handler | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
next: () => OnRequestResult; | ||
next: () => OnPreAuthResult; | ||
``` |
15 changes: 15 additions & 0 deletions
15
docs/development/core/server/kibana-plugin-server.onpreauthtoolkit.redirected.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index) > [kibana-plugin-server](./kibana-plugin-server.md) > [OnPreAuthToolkit](./kibana-plugin-server.onpreauthtoolkit.md) > [redirected](./kibana-plugin-server.onpreauthtoolkit.redirected.md) | ||
|
||
## OnPreAuthToolkit.redirected property | ||
|
||
To interrupt request handling and redirect to a configured url. If "options.forwarded" = true, request will be forwarded to another url right on the server. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
redirected: (url: string, options?: { | ||
forward: boolean; | ||
}) => OnPreAuthResult; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
docs/development/core/server/kibana-plugin-server.onrequesthandler.md
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
docs/development/core/server/kibana-plugin-server.onrequesttoolkit.md
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
docs/development/core/server/kibana-plugin-server.onrequesttoolkit.redirected.md
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
docs/development/core/server/kibana-plugin-server.onrequesttoolkit.seturl.md
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { Request } from 'hapi'; | ||
import { KibanaRequest } from './router'; | ||
|
||
export enum AuthStatus { | ||
authenticated = 'authenticated', | ||
unauthenticated = 'unauthenticated', | ||
unknown = 'unknown', | ||
} | ||
|
||
const toKey = (request: KibanaRequest | Request) => | ||
request instanceof KibanaRequest ? request.unstable_getIncomingMessage() : request.raw.req; | ||
|
||
export class AuthStateStorage { | ||
private readonly storage = new WeakMap<ReturnType<typeof toKey>, unknown>(); | ||
constructor(private readonly canBeAuthenticated: () => boolean) {} | ||
public set = (request: KibanaRequest | Request, state: unknown) => { | ||
this.storage.set(toKey(request), state); | ||
}; | ||
public get = (request: KibanaRequest | Request) => { | ||
const key = toKey(request); | ||
const state = this.storage.get(key); | ||
const status: AuthStatus = this.storage.has(key) | ||
? AuthStatus.authenticated | ||
: this.canBeAuthenticated() | ||
? AuthStatus.unauthenticated | ||
: AuthStatus.unknown; | ||
|
||
return { status, state }; | ||
}; | ||
public isAuthenticated = (request: KibanaRequest | Request) => { | ||
return this.get(request).status === AuthStatus.authenticated; | ||
}; | ||
} |
Oops, something went wrong.