11import { Hono } from "hono" ;
22import { type Env } from "./types.ts" ;
3- import { authMiddleware , type AuthContext } from "./middleware.ts" ;
3+ import { authMiddleware , type RequestContext } from "./middleware.ts" ;
44import {
55 createApiKeyProvider ,
66 isUsingLocalProvider ,
@@ -16,8 +16,9 @@ import {
1616import { RuntError , ErrorType } from "./types.ts" ;
1717import { D1Driver } from "@japikey/cloudflare" ;
1818import { JSONWebKeySet } from "jose" ;
19+ import { getBearerToken } from "./utils/request-utils.ts" ;
1920
20- const apiKeyRoutes = new Hono < { Bindings : Env ; Variables : AuthContext } > ( ) ;
21+ const apiKeyRoutes = new Hono < { Bindings : Env ; Variables : RequestContext } > ( ) ;
2122
2223/**
2324 * Middleware to ensure OAuth authentication (not API key) for sensitive operations
@@ -38,7 +39,7 @@ const oauthOnlyMiddleware = async (c: any, next: any) => {
3839 }
3940
4041 // Check if this was authenticated via API key
41- const authToken = c . req . header ( "Authorization" ) ?. replace ( "Bearer " , "" ) ;
42+ const authToken = getBearerToken ( c . req ) ;
4243 if ( authToken ) {
4344 const provider = createApiKeyProvider ( c . env ) ;
4445 const providerContext = createProviderContext ( c . env , authToken ) ;
@@ -85,7 +86,7 @@ apiKeyRoutes.get("/:kid/.well-known/jwks.json", async (c) => {
8586 */
8687apiKeyRoutes . post ( "/" , oauthOnlyMiddleware , async ( c ) => {
8788 const passport = c . get ( "passport" ) ;
88- const authToken = c . req . header ( "Authorization" ) ?. replace ( "Bearer " , "" ) ;
89+ const authToken = getBearerToken ( c . req ) ;
8990
9091 if ( ! passport || ! authToken ) {
9192 return c . json (
@@ -138,7 +139,7 @@ apiKeyRoutes.post("/", oauthOnlyMiddleware, async (c) => {
138139apiKeyRoutes . get ( "/:id" , authMiddleware , async ( c ) => {
139140 const keyId = c . req . param ( "id" ) ;
140141 const passport = c . get ( "passport" ) ;
141- const authToken = c . req . header ( "Authorization" ) ?. replace ( "Bearer " , "" ) ;
142+ const authToken = getBearerToken ( c . req ) ;
142143
143144 if ( ! passport || ! authToken ) {
144145 return c . json (
@@ -193,7 +194,7 @@ apiKeyRoutes.get("/:id", authMiddleware, async (c) => {
193194 */
194195apiKeyRoutes . get ( "/" , authMiddleware , async ( c ) => {
195196 const passport = c . get ( "passport" ) ;
196- const authToken = c . req . header ( "Authorization" ) ?. replace ( "Bearer " , "" ) ;
197+ const authToken = getBearerToken ( c . req ) ;
197198
198199 if ( ! passport || ! authToken ) {
199200 return c . json (
@@ -271,7 +272,7 @@ apiKeyRoutes.get("/", authMiddleware, async (c) => {
271272apiKeyRoutes . delete ( "/:id" , authMiddleware , async ( c ) => {
272273 const keyId = c . req . param ( "id" ) ;
273274 const passport = c . get ( "passport" ) ;
274- const authToken = c . req . header ( "Authorization" ) ?. replace ( "Bearer " , "" ) ;
275+ const authToken = getBearerToken ( c . req ) ;
275276
276277 if ( ! passport || ! authToken ) {
277278 return c . json (
@@ -335,7 +336,7 @@ apiKeyRoutes.delete("/:id", authMiddleware, async (c) => {
335336apiKeyRoutes . patch ( "/:id" , authMiddleware , async ( c ) => {
336337 const keyId = c . req . param ( "id" ) ;
337338 const passport = c . get ( "passport" ) ;
338- const authToken = c . req . header ( "Authorization" ) ?. replace ( "Bearer " , "" ) ;
339+ const authToken = getBearerToken ( c . req ) ;
339340
340341 if ( ! passport || ! authToken ) {
341342 return c . json (
0 commit comments