move IdentityClient.getBearerToken
Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
@@ -4,5 +4,7 @@
|
||||
|
||||
```ts
|
||||
// @public
|
||||
export const COMMON_CONSTANT = 1;
|
||||
export function getBearerTokenFromAuthorizationHeader(
|
||||
authorizationHeader: unknown,
|
||||
): string | undefined;
|
||||
```
|
||||
|
||||
@@ -19,12 +19,12 @@
|
||||
"clean": "backstage-cli clean"
|
||||
},
|
||||
"dependencies": {
|
||||
"@backstage/backend-common": "^0.10.6",
|
||||
"@backstage/backend-common": "^0.10.7-next.0",
|
||||
"@backstage/config": "^0.1.13",
|
||||
"winston": "^3.2.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@backstage/cli": "^0.13.1"
|
||||
"@backstage/cli": "^0.13.2-next.0"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2022 The Backstage Authors
|
||||
*
|
||||
* Licensed 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 { getBearerTokenFromAuthorizationHeader } from './getBearerTokenFromAuthorizationHeader';
|
||||
|
||||
describe('getBearerToken', () => {
|
||||
it('should return undefined on bad input', async () => {
|
||||
expect(getBearerTokenFromAuthorizationHeader(undefined)).toBeUndefined();
|
||||
expect(getBearerTokenFromAuthorizationHeader(7)).toBeUndefined();
|
||||
expect(
|
||||
getBearerTokenFromAuthorizationHeader('Bearer \n token'),
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
getBearerTokenFromAuthorizationHeader('Bearer token '),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return undefined on malformed input', async () => {
|
||||
const token = getBearerTokenFromAuthorizationHeader('malformed');
|
||||
expect(token).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return undefined on unexpected scheme', async () => {
|
||||
const token = getBearerTokenFromAuthorizationHeader('Basic token');
|
||||
expect(token).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return Bearer token', async () => {
|
||||
const token = getBearerTokenFromAuthorizationHeader('Bearer token');
|
||||
expect(token).toEqual('token');
|
||||
});
|
||||
|
||||
it('should return Bearer token despite unconventional case', async () => {
|
||||
const token = getBearerTokenFromAuthorizationHeader('bEARER token');
|
||||
expect(token).toEqual('token');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2022 The Backstage Authors
|
||||
*
|
||||
* Licensed 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Parses the given authorization header and returns the bearer token, or
|
||||
* undefined if no bearer token is given.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* This function is explicitly built to tolerate bad inputs safely, so you may
|
||||
* call it directly with e.g. the output of `req.header('authorization')`
|
||||
* without first checking that it exists.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export function getBearerTokenFromAuthorizationHeader(
|
||||
authorizationHeader: unknown,
|
||||
): string | undefined {
|
||||
if (typeof authorizationHeader !== 'string') {
|
||||
return undefined;
|
||||
}
|
||||
const matches = authorizationHeader.match(/^Bearer[ ]+(\S+)$/i);
|
||||
return matches?.[1];
|
||||
}
|
||||
@@ -20,9 +20,4 @@
|
||||
* @packageDocumentation
|
||||
*/
|
||||
|
||||
/**
|
||||
* Dummy.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export const COMMON_CONSTANT = 1;
|
||||
export { getBearerTokenFromAuthorizationHeader } from './getBearerTokenFromAuthorizationHeader';
|
||||
|
||||
Reference in New Issue
Block a user