-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from krutoo/jwt-middleware
JWT middleware
- Loading branch information
Showing
10 changed files
with
95 additions
and
48 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { Middleware } from '../types.ts'; | ||
|
||
/** Options of JWT middleware. */ | ||
export interface JwtMiddlewareOptions { | ||
/** JWT Token. */ | ||
token: string | (() => string | Promise<string>); | ||
|
||
/** Filter. Takes request, should return boolean. When returns false, JWT payload will not be added to request. */ | ||
filter?: (request: Request) => boolean; | ||
} | ||
|
||
/** | ||
* Simple JWT middleware. Will add "Authorization" header with JWT token to request. | ||
* @param options Options. | ||
* @returns Middleware. | ||
*/ | ||
export function jwt({ | ||
token, | ||
filter = () => true, | ||
}: JwtMiddlewareOptions): Middleware { | ||
const getToken = typeof token === 'function' ? token : () => token; | ||
|
||
return async (request, next) => { | ||
if (!filter(request)) { | ||
return next(request); | ||
} | ||
|
||
// IMPORTANT: for avoid mutate request, just create new Headers and Request here | ||
const headers = new Headers(request.headers); | ||
|
||
headers.set('Authorization', `Bearer ${await getToken()}`); | ||
|
||
return next(new Request(request, { headers })); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
export { defaultHeaders } from './default-headers.ts'; | ||
export { defaultHeaders, type DefaultHeadersOptions } from './default-headers.ts'; | ||
|
||
export { | ||
log, | ||
type LogData, | ||
type DoneLogData, | ||
type FailLogData, | ||
log, | ||
type LogData, | ||
type LogHandler, | ||
type LogHandlerFactory, | ||
} from './log.ts'; | ||
|
||
export { retry } from './retry.ts'; | ||
|
||
export { validateStatus, type ValidateStatusOptions } from './validate-status.ts'; | ||
|
||
export { proxy, type ProxyOptions, type ProxyRequestFilter } from './proxy.ts'; | ||
|
||
export { jwt, type JwtMiddlewareOptions } from './jwt.ts'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export type { Handler, Enhancer, Middleware } from './types.ts'; | ||
export { configureFetch, applyMiddleware } from './configure.ts'; | ||
export type { Enhancer, Handler, Middleware } from './types.ts'; | ||
export { applyMiddleware, configureFetch } from './configure.ts'; | ||
export { html, json } from './response.ts'; | ||
export { router, route } from './server.ts'; | ||
export { route, router } from './server.ts'; | ||
export { dump } from './utils/dump.ts'; |