This repository was archived by the owner on Jan 5, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +35
-1
lines changed
Expand file tree Collapse file tree 2 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -12,10 +12,11 @@ app.use(session({
1212} ) )
1313
1414const { login, logout, checkAuth } = require ( './express-handlers' )
15+ const headerAuth = require ( './header-auth-middleware' ) ;
1516
1617app . post ( '/session' , login )
1718app . delete ( '/session' , logout )
18- app . get ( '/session' , checkAuth )
19+ app . get ( '/session' , headerAuth , checkAuth )
1920
2021const PORT = process . env . PORT || 3000
2122app . listen ( PORT , ( ) => {
Original file line number Diff line number Diff line change 1+ const apiKeyToUser = {
2+ '76b1e728-1c14-43f9-aa06-6de5cbc064c2' : 'hugo' ,
3+ } ;
4+
5+ const apiKeys = new Set ( Object . keys ( apiKeyToUser ) )
6+
7+ const isApiKey = key => apiKeys . has ( key )
8+
9+ function headerAuth ( req , res , next ) {
10+ if ( req . session . data ) {
11+ return next ( )
12+ }
13+
14+ const authenticationHeader = req . get ( 'authorization' )
15+
16+ if ( ! authenticationHeader ) {
17+ return next ( )
18+ }
19+
20+ const apiKey = authenticationHeader
21+ . replace ( 'Bearer' , '' )
22+ . trim ( ) ;
23+
24+ if ( ! isApiKey ( apiKey ) ) {
25+ return next ( )
26+ }
27+
28+ req . session . data = { username : apiKeyToUser [ apiKey ] } ;
29+
30+ next ( ) ;
31+ }
32+
33+ module . exports = headerAuth ;
You can’t perform that action at this time.
0 commit comments