@@ -15,6 +15,7 @@ import { decodeJWT } from "../../src/utils/util";
15
15
16
16
export type Bindings = Env & {
17
17
OAUTH_PROVIDER : OAuthHelpers ;
18
+ OAUTH_KV : KVNamespace ;
18
19
} ;
19
20
20
21
const app = new Hono < {
@@ -74,16 +75,23 @@ async function handleApprove(c: any) {
74
75
await parseApproveFormBody ( await c . req . parseBody ( ) ) ;
75
76
76
77
if ( ! oauthReqInfo ) {
77
- return c . html ( "INVALID LOGIN" , 401 ) ;
78
+ // Add WWW-Authenticate header with resource_metadata
79
+ const url = new URL ( c . req . url ) ;
80
+ const base = url . origin ;
81
+ return c . html ( "INVALID LOGIN" , 401 , {
82
+ "WWW-Authenticate" : `Bearer resource_metadata=\"${ base } /.well-known/oauth-authorization-server\"` ,
83
+ } ) ;
78
84
}
79
85
86
+ const jwtInfo = decodeJWT ( apiKey ) ;
87
+
80
88
// The user must be successfully logged in and have approved the scopes, so we
81
89
// can complete the authorization request
82
90
const { redirectTo } = await c . env . OAUTH_PROVIDER . completeAuthorization ( {
83
91
request : oauthReqInfo ,
84
92
userId : apiKey ,
85
93
metadata : {
86
- label : "User label" ,
94
+ label : jwtInfo ?. payload ?. sub || "User label" ,
87
95
} ,
88
96
scope : oauthReqInfo . scope ,
89
97
props : {
@@ -172,4 +180,19 @@ app.post("/customer-context", async (c) => {
172
180
// then completing the authorization request with the OAUTH_PROVIDER
173
181
app . post ( "/approve" , handleApprove ) ;
174
182
183
+ // Add /.well-known/oauth-authorization-server endpoint
184
+ app . get ( "/.well-known/oauth-authorization-server" , ( c ) => {
185
+ // Extract base URL (protocol + host)
186
+ const url = new URL ( c . req . url ) ;
187
+ const base = url . origin ;
188
+ return c . json ( {
189
+ issuer : base ,
190
+ authorization_endpoint : `${ base } /authorize` ,
191
+ token_endpoint : `${ base } /token` ,
192
+ registration_endpoint : `${ base } /register` ,
193
+ scopes_supported : [ "*" ] ,
194
+ code_challenge_methods_supported : [ "S256" ] ,
195
+ } ) ;
196
+ } ) ;
197
+
175
198
export default app ;
0 commit comments