Gatekeeper! Better
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@mifi/auth-gatekeeper",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.3",
|
||||
"author": "mifi (Mike Fitzpatrick)",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
@@ -47,7 +47,8 @@
|
||||
},
|
||||
"packageManager": "yarn@3.5.1",
|
||||
"dependencies": {
|
||||
"@mifi/services-common": "^1.0.8",
|
||||
"@mifi/auth-common": "^1.0.5",
|
||||
"@mifi/services-common": "^1.0.11",
|
||||
"http-status-codes": "^2.2.0",
|
||||
"jsonwebtoken": "^9.0.0"
|
||||
}
|
||||
|
||||
@@ -1,9 +1,3 @@
|
||||
export const PACKAGE_NAME = '@mifi/auth-gatekeeper';
|
||||
|
||||
export const JWT_AUDIENCE = process.env.JWT_AUDIENCE || 'mifi.dev';
|
||||
export const JWT_ISSUER = process.env.JWT_ISSUER || PACKAGE_NAME;
|
||||
export const JWT_SECRET = process.env.JWT_SECRET || 'secret';
|
||||
|
||||
export const LOGIN_VALID_TIMEOUT = process.env.LOGIN_VALID_TIMEOUT || '12h'; // ###d|h|m
|
||||
export const RESET_VALID_TIMEOUT = process.env.RESET_VALID_TIMEOUT || '15m'; // ###d|h|m
|
||||
export const VERIFY_VALID_TIMEOUT = process.env.VERIFY_VALID_TIMEOUT || '60d'; // ###d|h|m
|
||||
|
||||
11
src/index.ts
11
src/index.ts
@@ -4,6 +4,7 @@ import { Middleware } from 'koa';
|
||||
import { initialize } from './middleware/initialize';
|
||||
import { Strategy as StrategyBase, VerifyFunction } from './strategy';
|
||||
import { authenticated } from './middleware/authenticated';
|
||||
import { jwt } from './middleware/jwt';
|
||||
|
||||
class Gatekeeper {
|
||||
private static instance: Gatekeeper;
|
||||
@@ -22,9 +23,7 @@ class Gatekeeper {
|
||||
}
|
||||
};
|
||||
|
||||
public static authenticated = authenticated;
|
||||
|
||||
public initialize = initialize;
|
||||
public authenticated = authenticated;
|
||||
|
||||
public static getInstance = () => {
|
||||
if (!Gatekeeper.instance) {
|
||||
@@ -33,6 +32,10 @@ class Gatekeeper {
|
||||
return Gatekeeper.instance;
|
||||
};
|
||||
|
||||
public initialize = initialize;
|
||||
|
||||
public jwt = jwt;
|
||||
|
||||
public use = (strategy: StrategyBase) => {
|
||||
this.strategies[<string>strategy.name] = strategy.verify;
|
||||
};
|
||||
@@ -40,4 +43,4 @@ class Gatekeeper {
|
||||
|
||||
export const gatekeeper = Gatekeeper.getInstance();
|
||||
|
||||
export const Strategy = StrategyBase;
|
||||
export const Strategy: typeof StrategyBase = StrategyBase;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Middleware } from 'koa';
|
||||
|
||||
import { LOGIN_ROUTE } from '../../constants/env';
|
||||
import { LOGIN_ROUTE } from '@mifi/auth-common/lib/routes';
|
||||
|
||||
export const authenticated: Middleware = async (ctx, next) => {
|
||||
if (ctx?.isAuthenticated()) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Middleware } from 'koa';
|
||||
import { GatekeeperUser } from './types/GatekeeperUser';
|
||||
import { Context, Middleware } from 'koa';
|
||||
import { GatekeeperUser } from '../types/GatekeeperUser';
|
||||
|
||||
declare module 'koa' {
|
||||
interface ExtendableContext {
|
||||
@@ -10,9 +10,21 @@ declare module 'koa' {
|
||||
}
|
||||
}
|
||||
|
||||
const isAuthenticated = function (this: Context) {
|
||||
return !!this.user;
|
||||
};
|
||||
|
||||
const isUnauthenticated = function (this: Context) {
|
||||
return !this.user;
|
||||
};
|
||||
|
||||
const logout = function (this: Context) {
|
||||
this.user = null;
|
||||
};
|
||||
|
||||
export const initialize: Middleware = (ctx) => {
|
||||
ctx.user = null;
|
||||
ctx.isAuthenticated = () => !!ctx.user;
|
||||
ctx.isUnauthenticated = () => !ctx.user;
|
||||
ctx.logout = () => (ctx.user = null);
|
||||
ctx.isAuthenticated = isAuthenticated.bind(ctx);
|
||||
ctx.isUnauthenticated = isUnauthenticated.bind(ctx);
|
||||
ctx.logout = logout.bind(ctx);
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Middleware } from 'koa';
|
||||
|
||||
import { verify } from '../utils/jwt';
|
||||
import { verify } from '@mifi/auth-common/lib/utils/jwt/verify';
|
||||
|
||||
import { GatekeeperUser } from '../types/GatekeeperUser';
|
||||
|
||||
export const jwt: Middleware = (ctx) => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Middleware } from 'koa';
|
||||
import { GatekeeperUser } from './types/GatekeeperUser';
|
||||
import { GatekeeperUser } from '../types/GatekeeperUser';
|
||||
|
||||
type Fn<P, T> = (u: P) => T;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Context } from 'koa';
|
||||
|
||||
import { STRATEGIES } from '../../constants/strategies';
|
||||
import { STRATEGIES } from '@mifi/auth-common/lib/strategies';
|
||||
|
||||
import { GatekeeperUser } from './types/GatekeeperUser';
|
||||
|
||||
export type VerifyFunction = (ctx: Context) => Promise<GatekeeperUser | false>;
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
import { sign } from './sign';
|
||||
import { verify } from './verify';
|
||||
|
||||
export { sign, verify };
|
||||
@@ -1,35 +0,0 @@
|
||||
import { sign as jwtSign } from 'jsonwebtoken';
|
||||
|
||||
import { JWT_AUDIENCE, JWT_ISSUER, JWT_SECRET } from '../../constants/env';
|
||||
import { Payload } from '@mifi/services-common/lib/types/Payload';
|
||||
|
||||
export type TokenProps = Payload & {
|
||||
aud?: string;
|
||||
exp?: number;
|
||||
iss?: string;
|
||||
sub: string | null;
|
||||
};
|
||||
|
||||
export type SignProps = string | TokenProps | void;
|
||||
|
||||
export const sign = (props: SignProps) => {
|
||||
const today = new Date();
|
||||
const { sub = null, ...rest }: TokenProps =
|
||||
typeof props === 'string' || typeof props === 'undefined' ? { sub: props || null } : props;
|
||||
let { exp } = rest;
|
||||
if (!exp) {
|
||||
const defaultExp = new Date(today);
|
||||
defaultExp.setDate(today.getDate() + parseInt(process.env.JWT_DAYS_VALID as string));
|
||||
exp = defaultExp.getTime() / 1000;
|
||||
}
|
||||
return jwtSign(
|
||||
{
|
||||
exp,
|
||||
sub,
|
||||
aud: rest.aud || JWT_AUDIENCE,
|
||||
iat: today.getTime(),
|
||||
iss: rest.iss || JWT_ISSUER,
|
||||
},
|
||||
JWT_SECRET,
|
||||
);
|
||||
};
|
||||
@@ -1,4 +0,0 @@
|
||||
import { verify as jwtVerify } from 'jsonwebtoken';
|
||||
import { JWT_SECRET } from '../../constants/env';
|
||||
|
||||
export const verify = (token: string) => jwtVerify(token, JWT_SECRET);
|
||||
55
yarn.lock
55
yarn.lock
@@ -1846,6 +1846,17 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@mifi/auth-common@npm:^1.0.5":
|
||||
version: 1.0.5
|
||||
resolution: "@mifi/auth-common@npm:1.0.5::__archiveUrl=https%3A%2F%2Fgit.mifi.dev%2Fapi%2Fpackages%2Fmifi%2Fnpm%2F%2540mifi%252Fauth-common%2F-%2F1.0.5%2Fauth-common-1.0.5.tgz"
|
||||
dependencies:
|
||||
"@mifi/breakerbox-db": ^1.0.3
|
||||
"@mifi/services-common": ^1.0.11
|
||||
jsonwebtoken: ^9.0.0
|
||||
checksum: 0eef5c417ae95dc2a31b8599b4e0e747478171d518a7dfd223090667fdc142c8d2cd9e29852883573cc6c7b2dd32eb0625e5bc73ff6f5908b0032a54815e0a98
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@mifi/auth-gatekeeper@workspace:.":
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@mifi/auth-gatekeeper@workspace:."
|
||||
@@ -1853,7 +1864,8 @@ __metadata:
|
||||
"@babel/core": ^7.21.8
|
||||
"@babel/preset-env": ^7.21.5
|
||||
"@babel/preset-typescript": ^7.21.5
|
||||
"@mifi/services-common": ^1.0.8
|
||||
"@mifi/auth-common": ^1.0.5
|
||||
"@mifi/services-common": ^1.0.11
|
||||
"@tsconfig/node16": ^1.0.4
|
||||
"@types/jest": ^29.5.1
|
||||
"@types/jsonwebtoken": ^9.0.2
|
||||
@@ -1881,10 +1893,20 @@ __metadata:
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@mifi/services-common@npm:^1.0.8":
|
||||
version: 1.0.8
|
||||
resolution: "@mifi/services-common@npm:1.0.8::__archiveUrl=https%3A%2F%2Fgit.mifi.dev%2Fapi%2Fpackages%2Fmifi%2Fnpm%2F%2540mifi%252Fservices-common%2F-%2F1.0.8%2Fservices-common-1.0.8.tgz"
|
||||
checksum: e7bb930ec2314505c16617a554124bdf60b1cc70116b0903cbf5bfb3886ddd3bd64b002c59cbd6a39c7743b68351255d2a73fab1093c91757bad39cb47ef34bf
|
||||
"@mifi/breakerbox-db@npm:^1.0.3":
|
||||
version: 1.0.3
|
||||
resolution: "@mifi/breakerbox-db@npm:1.0.3::__archiveUrl=https%3A%2F%2Fgit.mifi.dev%2Fapi%2Fpackages%2Fmifi%2Fnpm%2F%2540mifi%252Fbreakerbox-db%2F-%2F1.0.3%2Fbreakerbox-db-1.0.3.tgz"
|
||||
dependencies:
|
||||
lowdb: ^6.0.1
|
||||
yaml: ^2.3.1
|
||||
checksum: 16f2f841d4d1f87f29ddcbec222076d4186038cbd22b9116deb941c07bd0aca1473a5aa66ae113c0cb01c3ded866ef9ec4bbeda055b054d47c3696cb16cfeb62
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@mifi/services-common@npm:^1.0.11":
|
||||
version: 1.0.11
|
||||
resolution: "@mifi/services-common@npm:1.0.11::__archiveUrl=https%3A%2F%2Fgit.mifi.dev%2Fapi%2Fpackages%2Fmifi%2Fnpm%2F%2540mifi%252Fservices-common%2F-%2F1.0.11%2Fservices-common-1.0.11.tgz"
|
||||
checksum: 3faeba975bbf35f532826da658545c1faa04f2a90c4f5a428474628aa3d9a3a03690b9caa216ed78be87aa4564dbe78195945b3de6a234ad6de9fac0768ac999
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@@ -5753,6 +5775,15 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"lowdb@npm:^6.0.1":
|
||||
version: 6.0.1
|
||||
resolution: "lowdb@npm:6.0.1"
|
||||
dependencies:
|
||||
steno: ^3.0.0
|
||||
checksum: d555a5bcc2e4a963fae89209b693a6f2b7b69bae915ff67355537b7a14a4f6e44bc273467bc3d4d7e81660c1313587ee3bfebf044d50d3213a5e95ea7f07ded4
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"lru-cache@npm:^5.1.1":
|
||||
version: 5.1.1
|
||||
resolution: "lru-cache@npm:5.1.1"
|
||||
@@ -6972,6 +7003,13 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"steno@npm:^3.0.0":
|
||||
version: 3.0.0
|
||||
resolution: "steno@npm:3.0.0"
|
||||
checksum: fb928451a4f96342b496b71147fbca0a20a5daf7bfd23a4a1cec8640d3c6c67176809169e9a5801ea44490d448b5b7ecb151b9fba434872c2d65549847f39460
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"string-length@npm:^4.0.1":
|
||||
version: 4.0.2
|
||||
resolution: "string-length@npm:4.0.2"
|
||||
@@ -7672,6 +7710,13 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"yaml@npm:^2.3.1":
|
||||
version: 2.3.1
|
||||
resolution: "yaml@npm:2.3.1"
|
||||
checksum: 2c7bc9a7cd4c9f40d3b0b0a98e370781b68b8b7c4515720869aced2b00d92f5da1762b4ffa947f9e795d6cd6b19f410bd4d15fdd38aca7bd96df59bd9486fb54
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"yargs-parser@npm:^13.1.2":
|
||||
version: 13.1.2
|
||||
resolution: "yargs-parser@npm:13.1.2"
|
||||
|
||||
Reference in New Issue
Block a user