diff --git a/package.json b/package.json index 017cd4d..631a7f6 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/src/env.ts b/src/env.ts index 581ae48..93a24d1 100644 --- a/src/env.ts +++ b/src/env.ts @@ -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 diff --git a/src/index.ts b/src/index.ts index e599d6f..9899ae5 100644 --- a/src/index.ts +++ b/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[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; diff --git a/src/middleware/authenticated.ts b/src/middleware/authenticated.ts index 82844d5..9564a03 100644 --- a/src/middleware/authenticated.ts +++ b/src/middleware/authenticated.ts @@ -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()) { diff --git a/src/middleware/initialize.ts b/src/middleware/initialize.ts index cb69205..3e5fd5d 100644 --- a/src/middleware/initialize.ts +++ b/src/middleware/initialize.ts @@ -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); }; diff --git a/src/middleware/jwt.ts b/src/middleware/jwt.ts index 521e808..35a55fe 100644 --- a/src/middleware/jwt.ts +++ b/src/middleware/jwt.ts @@ -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) => { diff --git a/src/middleware/session.ts b/src/middleware/session.ts index 0df597b..b747fb1 100644 --- a/src/middleware/session.ts +++ b/src/middleware/session.ts @@ -1,5 +1,5 @@ import { Middleware } from 'koa'; -import { GatekeeperUser } from './types/GatekeeperUser'; +import { GatekeeperUser } from '../types/GatekeeperUser'; type Fn = (u: P) => T; diff --git a/src/strategy.ts b/src/strategy.ts index 4100fef..661ee80 100644 --- a/src/strategy.ts +++ b/src/strategy.ts @@ -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; diff --git a/src/utils/jwt/index.ts b/src/utils/jwt/index.ts deleted file mode 100644 index c0b4590..0000000 --- a/src/utils/jwt/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { sign } from './sign'; -import { verify } from './verify'; - -export { sign, verify }; diff --git a/src/utils/jwt/sign.ts b/src/utils/jwt/sign.ts deleted file mode 100644 index d668bba..0000000 --- a/src/utils/jwt/sign.ts +++ /dev/null @@ -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, - ); -}; diff --git a/src/utils/jwt/verify.ts b/src/utils/jwt/verify.ts deleted file mode 100644 index 5d294ef..0000000 --- a/src/utils/jwt/verify.ts +++ /dev/null @@ -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); diff --git a/yarn.lock b/yarn.lock index 3b61f4e..5e9e9a8 100644 --- a/yarn.lock +++ b/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"