-
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 #27 from FireBlinkLTD/feat/permissions_api
feat: add permissions api endpoint
- Loading branch information
Showing
28 changed files
with
1,019 additions
and
172 deletions.
There are no files selected for viewing
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,4 +1,5 @@ | ||
#!/bin/sh | ||
set -e | ||
|
||
############# | ||
# VARIABLES # | ||
|
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,4 +1,5 @@ | ||
#!/bin/sh | ||
set -e | ||
|
||
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" | ||
|
||
|
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ export interface Config { | |
login: string; | ||
api: { | ||
whoami?: string; | ||
permissions?: string; | ||
} | ||
} | ||
|
||
|
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,62 @@ | ||
import { HttpMethod, HttpRequestHandlerConfig, ProxyRequest, Request, Response } from "prxi"; | ||
import { Context } from "../../../types/Context"; | ||
import { getConfig } from "../../../config/getConfig"; | ||
import { RequestUtils } from "../../../utils/RequestUtils"; | ||
import { JwtPayload, verify } from "jsonwebtoken"; | ||
import { handleHttpAuthenticationFlow } from "../../../utils/AccessUtils"; | ||
|
||
export abstract class BaseAccessHandler implements HttpRequestHandlerConfig { | ||
/** | ||
* @inheritdoc | ||
*/ | ||
abstract isMatching(method: HttpMethod, path: string, context: Context): boolean; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
async handle(req: Request, res: Response, proxyRequest: ProxyRequest, method: HttpMethod, path: string, context: Context): Promise<void> { | ||
context.api = true; | ||
|
||
const _ = context.debugger.child('BaseAccessHandler -> handle()', { context, headers: req.headers, method, path }); | ||
const cookies = RequestUtils.getCookies(req.headers); | ||
_.debug('-> RequestUtils.getCookies()', { cookies }); | ||
|
||
let metaPayload: Record<string, any> = null; | ||
const metaToken = cookies[getConfig().cookies.names.meta]; | ||
if (metaToken) { | ||
metaPayload = <JwtPayload> verify(metaToken, getConfig().jwt.metaTokenSecret, { | ||
complete: false, | ||
}); | ||
_.debug('Meta cookie found', { metaPayload }); | ||
} | ||
context.metaPayload = metaPayload?.p; | ||
|
||
const breakFlow = await handleHttpAuthenticationFlow( | ||
_.child('-> handleAuthenticationFlow()'), | ||
cookies, | ||
req, | ||
res, | ||
method, | ||
path, | ||
context, | ||
metaPayload | ||
); | ||
if (breakFlow) { | ||
_.debug('Breaking upon authentication'); | ||
return; | ||
} | ||
|
||
await this.process(req, res, proxyRequest, method, path, context); | ||
} | ||
|
||
/** | ||
* Called after handle() | ||
* @param req | ||
* @param res | ||
* @param proxyRequest | ||
* @param method | ||
* @param path | ||
* @param context | ||
*/ | ||
abstract process(req: Request, res: Response, proxyRequest: ProxyRequest, method: HttpMethod, path: string, context: Context): Promise<void>; | ||
} |
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,116 @@ | ||
import { HttpMethod, ProxyRequest, Request, Response } from "prxi"; | ||
import { Context } from "../../../types/Context"; | ||
import { getConfig } from "../../../config/getConfig"; | ||
import { RequestUtils } from "../../../utils/RequestUtils"; | ||
import { BaseAccessHandler } from "./BaseAccessHandler"; | ||
import { sendJsonResponse } from "../../../utils/ResponseUtils"; | ||
import { Mapping } from "../../../config/Mapping"; | ||
|
||
interface Resource { | ||
path: string; | ||
method: HttpMethod; | ||
allowed: boolean; | ||
} | ||
|
||
export class PermissionsAPIHandler extends BaseAccessHandler { | ||
/** | ||
* @inheritdoc | ||
*/ | ||
isMatching(method: HttpMethod, path: string, context: Context): boolean { | ||
return RequestUtils.isMatching( | ||
context.debugger.child('PermissionsAPIHandler -> isMatching()', {method, path}), | ||
// request | ||
method, path, | ||
// expected | ||
'POST', getConfig().paths.api.permissions, | ||
); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
async process(req: Request, res: Response, proxyRequest: ProxyRequest, method: HttpMethod, path: string, context: Context): Promise<void> { | ||
const _ = context.debugger.child('PermissionsAPIHandler -> process()', { context, headers: req.headers, method, path }); | ||
|
||
// parse body | ||
const body: Resource[] = await RequestUtils.readJsonBody(req); | ||
|
||
// validate | ||
if (!body) { | ||
return sendJsonResponse(_, 400, { | ||
error: 'body is missing', | ||
}, res); | ||
} | ||
|
||
if (!Array.isArray(body)) { | ||
return sendJsonResponse(_, 400, { | ||
error: 'body is not an array', | ||
}, res); | ||
} | ||
|
||
for (const r of body) { | ||
if (!r) { | ||
return sendJsonResponse(_, 400, { | ||
error: 'one of the body array elements is missing', | ||
}, res); | ||
} | ||
|
||
if (!r.path) { | ||
return sendJsonResponse(_, 400, { | ||
error: 'one of the body array elements is missing "path" property', | ||
}, res); | ||
} | ||
|
||
if (!r.method) { | ||
return sendJsonResponse(_, 400, { | ||
error: 'one of the body array elements is missing "method" property', | ||
}, res); | ||
} | ||
} | ||
|
||
// process | ||
const mappings = [ | ||
getConfig().mappings.public, | ||
getConfig().mappings.api, | ||
getConfig().mappings.pages, | ||
]; | ||
|
||
const resources: Resource[] = []; | ||
for (const r of body) { | ||
let mapping: Mapping; | ||
for (let m of mappings) { | ||
mapping = RequestUtils.findMapping( | ||
_, | ||
m, | ||
r.method, | ||
r.path | ||
); | ||
|
||
if (mapping) { | ||
break; | ||
} | ||
} | ||
|
||
let allowed = !!mapping; | ||
if (allowed) { | ||
allowed = !!RequestUtils.isAllowedAccess( | ||
_.child('RequestUtils'), | ||
context.accessTokenJWT, | ||
context.idTokenJWT, | ||
mapping, | ||
); | ||
} | ||
|
||
resources.push({ | ||
method: r.method, | ||
path: r.path, | ||
allowed, | ||
}); | ||
} | ||
|
||
await sendJsonResponse(_, 200, { | ||
anonymous: !context.accessTokenJWT, | ||
resources, | ||
}, res); | ||
} | ||
} |
Oops, something went wrong.