@@ -154,9 +154,10 @@ function verify(token: string, b64Keys: string[], ignoreExpiration = false): Pay
154154 * This function validates that the authorization header follows the correct Bearer token format
155155 * ("Bearer <token>") and extracts the token portion for further processing.
156156 *
157- * @param {string } authorization - The Authorization header value from an HTTP request
157+ * @param {string | undefined } authorization - The Authorization header value from an HTTP request
158158 * @returns {string } The extracted JWT token as a string
159- * @throws {Error } Will throw an error if the authorization header is empty or not in the format 'Bearer <token>'
159+ * @throws {Error } Will throw an error if the authorization parameter is undefined
160+ * @throws {Error } Will throw an error if the format is invalid
160161 *
161162 * @example
162163 * ```typescript
@@ -170,23 +171,28 @@ function verify(token: string, b64Keys: string[], ignoreExpiration = false): Pay
170171 * const token2 = parseBearerToken(headerWithSpaces);
171172 * // Returns: "token123"
172173 *
173- * // Invalid headers - these will throw errors
174+ * // Invalid headers - these will throw specific errors
174175 * try {
175- * parseBearerToken("Basic dXNlcjpwYXNz"); // Throws Error
176- * parseBearerToken("Bearer"); // Throws Error
177- * parseBearerToken("Bearer "); // Throws Error
178- * parseBearerToken(""); // Throws Error
176+ * parseBearerToken(undefined); // Throws: "Authorization header is missing"
177+ * parseBearerToken(""); // Throws: "Authorization header must be in the format 'Bearer <token>'"
178+ * parseBearerToken("Basic dXNlcjpwYXNz"); // Throws: "Authorization header must be in the format 'Bearer <token>'"
179+ * parseBearerToken("Bearer"); // Throws: "Authorization header must be in the format 'Bearer <token>'"
180+ * parseBearerToken("Bearer "); // Throws: "Authorization header must be in the format 'Bearer <token>'"
179181 * } catch (error) {
180- * console.error('Invalid authorization header :', error.message);
182+ * console.error('Authorization error :', error.message);
181183 * }
182184 * ```
183185 *
184186 */
185187const BEARER_TOKEN_ERROR_MESSAGE = "Authorization header must be in the format 'Bearer <token>'" ;
188+ const MISSING_AUTHORIZATION_ERROR_MESSAGE = "Authorization header is missing" ;
186189
187- function parseBearerToken ( authorization : string ) : string {
190+ function parseBearerToken ( authorization : string | undefined ) : string {
188191
189- if ( ! authorization ?. startsWith ( "Bearer " ) )
192+ if ( ! authorization )
193+ throw new Error ( MISSING_AUTHORIZATION_ERROR_MESSAGE ) ;
194+
195+ if ( ! authorization . startsWith ( "Bearer " ) )
190196 throw new Error ( BEARER_TOKEN_ERROR_MESSAGE ) ;
191197
192198 // Split by spaces and filter out empty strings to handle multiple spaces
@@ -199,10 +205,15 @@ function parseBearerToken(authorization: string): string {
199205
200206}
201207
202-
203- // Generate a random index based on the array length
208+ // Generate a random index based on an array length
204209function randomPick ( array : string [ ] ) : number {
205210 return Math . floor ( Math . random ( ) * array . length ) ;
206211}
207212
208- export { sign , verify , parseBearerToken , BEARER_TOKEN_ERROR_MESSAGE } ;
213+ export {
214+ sign ,
215+ verify ,
216+ parseBearerToken ,
217+ BEARER_TOKEN_ERROR_MESSAGE ,
218+ MISSING_AUTHORIZATION_ERROR_MESSAGE ,
219+ } ;
0 commit comments