-
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.
Implement HTTP Authentication provider and allow
ApiKey
authenticat…
…ion by default.
- Loading branch information
Showing
34 changed files
with
1,809 additions
and
1,528 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
58 changes: 58 additions & 0 deletions
58
x-pack/plugins/security/server/authentication/get_http_authentication_scheme.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,58 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { httpServerMock } from '../../../../../src/core/server/http/http_server.mocks'; | ||
|
||
import { getHTTPAuthenticationScheme } from './get_http_authentication_scheme'; | ||
|
||
describe('getHTTPAuthenticationScheme', () => { | ||
it('returns `null` if request does not have authorization header', () => { | ||
expect(getHTTPAuthenticationScheme(httpServerMock.createKibanaRequest())).toBeNull(); | ||
}); | ||
|
||
it('returns `null` if authorization header value isn not a string', () => { | ||
expect( | ||
getHTTPAuthenticationScheme( | ||
httpServerMock.createKibanaRequest({ | ||
headers: { authorization: ['Basic xxx', 'Bearer xxx'] as any }, | ||
}) | ||
) | ||
).toBeNull(); | ||
}); | ||
|
||
it('returns `null` if authorization header value is an empty string', () => { | ||
expect( | ||
getHTTPAuthenticationScheme( | ||
httpServerMock.createKibanaRequest({ headers: { authorization: '' } }) | ||
) | ||
).toBeNull(); | ||
}); | ||
|
||
it('returns only scheme portion of the authorization header value in lower case', () => { | ||
const headerValueAndSchemeMap = [ | ||
['Basic xxx', 'basic'], | ||
['Basic xxx yyy', 'basic'], | ||
['basic xxx', 'basic'], | ||
['basic', 'basic'], | ||
// We don't trim leading whitespaces in scheme. | ||
[' Basic xxx', ''], | ||
['Negotiate xxx', 'negotiate'], | ||
['negotiate xxx', 'negotiate'], | ||
['negotiate', 'negotiate'], | ||
['ApiKey xxx', 'apikey'], | ||
['apikey xxx', 'apikey'], | ||
['Api Key xxx', 'api'], | ||
]; | ||
|
||
for (const [authorization, scheme] of headerValueAndSchemeMap) { | ||
expect( | ||
getHTTPAuthenticationScheme( | ||
httpServerMock.createKibanaRequest({ headers: { authorization } }) | ||
) | ||
).toBe(scheme); | ||
} | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
x-pack/plugins/security/server/authentication/get_http_authentication_scheme.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,21 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { KibanaRequest } from '../../../../../src/core/server'; | ||
|
||
/** | ||
* Parses request's `Authorization` HTTP header if present and extracts authentication scheme. | ||
* https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml#authschemes | ||
* @param request Request instance to extract authentication scheme for. | ||
*/ | ||
export function getHTTPAuthenticationScheme(request: KibanaRequest) { | ||
const authorizationHeaderValue = request.headers.authorization; | ||
if (!authorizationHeaderValue || typeof authorizationHeaderValue !== 'string') { | ||
return null; | ||
} | ||
|
||
return authorizationHeaderValue.split(/\s+/)[0].toLowerCase(); | ||
} |
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
Oops, something went wrong.