-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Workplace Search] Persist OAuth token package during OAuth connect f…
…low (#93210) * Store session data sent from Enterprise Search server This modifies the EnterpriseSearchRequestHandler to remove any data in a response under the _sessionData key and instead persist it on the server side. Ultimately, this data will be persisted in the login session, but for now we'll just store it in a cookie. #92558 Also uses this functionality to persist Workplace Search's OAuth token package. * Only return a modified response body if _sessionData was found The destructuring I'm doing to remove _sessionData from the response is breaking routes that currently expect an empty response body. This change just leaves those response bodies alone. * Refactor from initial feedback & add tests * Decrease levity * Changes from PR feedback Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
0ce3dc7
commit add02f1
Showing
7 changed files
with
206 additions
and
6 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
55 changes: 55 additions & 0 deletions
55
x-pack/plugins/enterprise_search/server/lib/get_oauth_token_package_params.test.ts
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,55 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import { ENTERPRISE_SEARCH_KIBANA_COOKIE } from '../../common/constants'; | ||
|
||
import { getOAuthTokenPackageParams } from './get_oauth_token_package_params'; | ||
|
||
describe('getOAuthTokenPackage', () => { | ||
const tokenPackage = 'some_encrypted_secrets'; | ||
const tokenPackageCookie = `${ENTERPRISE_SEARCH_KIBANA_COOKIE}=${tokenPackage}`; | ||
const tokenPackageParams = { token_package: tokenPackage }; | ||
|
||
describe('when there are no cookie headers', () => { | ||
it('returns an empty parameter set', () => { | ||
expect(getOAuthTokenPackageParams(undefined)).toEqual({}); | ||
}); | ||
}); | ||
|
||
describe('when there is a single cookie header', () => { | ||
it('returns an empty parameter set when our cookie is not there', () => { | ||
const cookieHeader = '_st_fruit=banana'; | ||
|
||
expect(getOAuthTokenPackageParams(cookieHeader)).toEqual({}); | ||
}); | ||
|
||
it('returns the token package when our cookie is the only one', () => { | ||
const cookieHeader = `${tokenPackageCookie}`; | ||
|
||
expect(getOAuthTokenPackageParams(cookieHeader)).toEqual(tokenPackageParams); | ||
}); | ||
|
||
it('returns the token package when there are other cookies in the header', () => { | ||
const cookieHeader = `_chocolate=chip; ${tokenPackageCookie}; _oatmeal=raisin`; | ||
|
||
expect(getOAuthTokenPackageParams(cookieHeader)).toEqual(tokenPackageParams); | ||
}); | ||
}); | ||
|
||
describe('when there are multiple cookie headers', () => { | ||
it('returns an empty parameter set when none of them include our cookie', () => { | ||
const cookieHeaders = ['_st_fruit=banana', '_sid=12345']; | ||
|
||
expect(getOAuthTokenPackageParams(cookieHeaders)).toEqual({}); | ||
}); | ||
|
||
it('returns the token package when our cookie is present', () => { | ||
const cookieHeaders = ['_st_fruit=banana', `_heat=spicy; ${tokenPackageCookie}`]; | ||
|
||
expect(getOAuthTokenPackageParams(cookieHeaders)).toEqual(tokenPackageParams); | ||
}); | ||
}); | ||
}); |
39 changes: 39 additions & 0 deletions
39
x-pack/plugins/enterprise_search/server/lib/get_oauth_token_package_params.ts
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,39 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { ENTERPRISE_SEARCH_KIBANA_COOKIE } from '../../common/constants'; | ||
|
||
export const getOAuthTokenPackageParams = (rawCookieHeader: string | string[] | undefined) => { | ||
// In the future the token package will be stored in the login session. For now it's in a cookie. | ||
|
||
if (!rawCookieHeader) { | ||
return {}; | ||
} | ||
|
||
/** | ||
* A request can have multiple cookie headers and each header can hold multiple cookies. | ||
* Within a header, cookies are separated by '; '. Here we are splitting out the individual | ||
* cookies from the header(s) and looking for the specific one that holds our token package. | ||
*/ | ||
|
||
const cookieHeaders = Array.isArray(rawCookieHeader) ? rawCookieHeader : [rawCookieHeader]; | ||
|
||
let tokenPackage: string | undefined; | ||
|
||
cookieHeaders | ||
.flatMap((rawHeader) => rawHeader.split('; ')) | ||
.forEach((rawCookie) => { | ||
const [cookieName, cookieValue] = rawCookie.split('='); | ||
if (cookieName === ENTERPRISE_SEARCH_KIBANA_COOKIE) tokenPackage = cookieValue; | ||
}); | ||
|
||
if (tokenPackage) { | ||
return { token_package: tokenPackage }; | ||
} else { | ||
return {}; | ||
} | ||
}; |
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