|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +import { extractApiKeyIdFromAuthzHeader, decodeApiKeyId } from './utils'; |
| 11 | + |
| 12 | +describe('extractApiKeyIdFromAuthzHeader', () => { |
| 13 | + it('returns the API key ID from a valid ApiKey authorization header', () => { |
| 14 | + const id = 'my-api-key-id'; |
| 15 | + const secret = 'my-api-key-secret'; |
| 16 | + const encoded = Buffer.from(`${id}:${secret}`).toString('base64'); |
| 17 | + expect(extractApiKeyIdFromAuthzHeader(`ApiKey ${encoded}`)).toBe(id); |
| 18 | + }); |
| 19 | + |
| 20 | + it('is case-insensitive for the ApiKey prefix', () => { |
| 21 | + const id = 'test-id'; |
| 22 | + const encoded = Buffer.from(`${id}:secret`).toString('base64'); |
| 23 | + expect(extractApiKeyIdFromAuthzHeader(`apikey ${encoded}`)).toBe(id); |
| 24 | + expect(extractApiKeyIdFromAuthzHeader(`APIKEY ${encoded}`)).toBe(id); |
| 25 | + expect(extractApiKeyIdFromAuthzHeader(`aPiKeY ${encoded}`)).toBe(id); |
| 26 | + }); |
| 27 | + |
| 28 | + it('returns undefined when the header is undefined', () => { |
| 29 | + expect(extractApiKeyIdFromAuthzHeader(undefined)).toBeUndefined(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('returns undefined when the header is an array', () => { |
| 33 | + expect(extractApiKeyIdFromAuthzHeader(['ApiKey abc'])).toBeUndefined(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('returns undefined when the header does not start with ApiKey prefix', () => { |
| 37 | + expect(extractApiKeyIdFromAuthzHeader('Bearer some-token')).toBeUndefined(); |
| 38 | + expect(extractApiKeyIdFromAuthzHeader('Basic dXNlcjpwYXNz')).toBeUndefined(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('returns the ID when there is no colon in the decoded value', () => { |
| 42 | + const encoded = Buffer.from('just-an-id').toString('base64'); |
| 43 | + expect(extractApiKeyIdFromAuthzHeader(`ApiKey ${encoded}`)).toBe('just-an-id'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('returns undefined when the decoded ID is empty', () => { |
| 47 | + const encoded = Buffer.from(':secret').toString('base64'); |
| 48 | + expect(extractApiKeyIdFromAuthzHeader(`ApiKey ${encoded}`)).toBeUndefined(); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe('decodeApiKeyId', () => { |
| 53 | + it('returns the API key ID from a base64-encoded api key', () => { |
| 54 | + const id = 'my-api-key-id'; |
| 55 | + const secret = 'my-api-key-secret'; |
| 56 | + const encoded = Buffer.from(`${id}:${secret}`).toString('base64'); |
| 57 | + expect(decodeApiKeyId(encoded)).toBe(id); |
| 58 | + }); |
| 59 | + |
| 60 | + it('returns the full decoded value when there is no colon', () => { |
| 61 | + const encoded = Buffer.from('just-an-id').toString('base64'); |
| 62 | + expect(decodeApiKeyId(encoded)).toBe('just-an-id'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('returns undefined when the decoded ID is empty', () => { |
| 66 | + const encoded = Buffer.from(':secret').toString('base64'); |
| 67 | + expect(decodeApiKeyId(encoded)).toBeUndefined(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('returns undefined for an empty string', () => { |
| 71 | + expect(decodeApiKeyId('')).toBeUndefined(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('returns undefined when the decoded ID is only whitespace', () => { |
| 75 | + const encoded = Buffer.from(' :secret').toString('base64'); |
| 76 | + expect(decodeApiKeyId(encoded)).toBeUndefined(); |
| 77 | + }); |
| 78 | +}); |
0 commit comments