mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-05-05 14:47:12 -04:00
Moved api tests to oauth2.
This commit is contained in:
parent
6f3f127808
commit
85bb520355
16 changed files with 501 additions and 214 deletions
|
@ -21,30 +21,12 @@
|
|||
|
||||
import {MapArrayType} from "../types/MapType";
|
||||
|
||||
const absolutePaths = require('../utils/AbsolutePaths');
|
||||
import fs from 'fs';
|
||||
const api = require('../db/API');
|
||||
import log4js from 'log4js';
|
||||
const padManager = require('../db/PadManager');
|
||||
const randomString = require('../utils/randomstring');
|
||||
const argv = require('../utils/Cli').argv;
|
||||
import createHTTPError from 'http-errors';
|
||||
|
||||
const apiHandlerLogger = log4js.getLogger('APIHandler');
|
||||
|
||||
// ensure we have an apikey
|
||||
let apikey:string|null = null;
|
||||
const apikeyFilename = absolutePaths.makeAbsolute(argv.apikey || './APIKEY.txt');
|
||||
|
||||
try {
|
||||
apikey = fs.readFileSync(apikeyFilename, 'utf8');
|
||||
apiHandlerLogger.info(`Api key file read from: "${apikeyFilename}"`);
|
||||
} catch (e) {
|
||||
apiHandlerLogger.info(
|
||||
`Api key file "${apikeyFilename}" not found. Creating with random contents.`);
|
||||
apikey = randomString(32);
|
||||
fs.writeFileSync(apikeyFilename, apikey!, 'utf8');
|
||||
}
|
||||
import {Http2ServerRequest, Http2ServerResponse} from "node:http2";
|
||||
import {publicKeyExported} from "../security/OAuth2Provider";
|
||||
import {jwtVerify} from "jose";
|
||||
|
||||
// a list of all functions
|
||||
const version:MapArrayType<any> = {};
|
||||
|
@ -174,14 +156,14 @@ type APIFields = {
|
|||
}
|
||||
|
||||
/**
|
||||
* Handles a HTTP API call
|
||||
* Handles an HTTP API call
|
||||
* @param {String} apiVersion the version of the api
|
||||
* @param {String} functionName the name of the called function
|
||||
* @param fields the params of the called function
|
||||
* @req express request object
|
||||
* @res express response object
|
||||
* @param req express request object
|
||||
* @param res express response object
|
||||
*/
|
||||
exports.handle = async function (apiVersion: string, functionName: string, fields: APIFields) {
|
||||
exports.handle = async function (apiVersion: string, functionName: string, fields: APIFields, req: Http2ServerRequest, res: Http2ServerResponse) {
|
||||
// say goodbye if this is an unknown API version
|
||||
if (!(apiVersion in version)) {
|
||||
throw new createHTTPError.NotFound('no such api version');
|
||||
|
@ -192,13 +174,20 @@ exports.handle = async function (apiVersion: string, functionName: string, field
|
|||
throw new createHTTPError.NotFound('no such function');
|
||||
}
|
||||
|
||||
// check the api key!
|
||||
fields.apikey = fields.apikey || fields.api_key;
|
||||
|
||||
if (fields.apikey !== apikey!.trim()) {
|
||||
if(!req.headers.authorization) {
|
||||
throw new createHTTPError.Unauthorized('no or wrong API Key');
|
||||
}
|
||||
|
||||
try {
|
||||
await jwtVerify(req.headers.authorization!.replace("Bearer ", ""), publicKeyExported!, {algorithms: ['RS256'],
|
||||
requiredClaims: ["admin"]})
|
||||
|
||||
} catch (e) {
|
||||
throw new createHTTPError.Unauthorized('no or wrong API Key');
|
||||
}
|
||||
|
||||
|
||||
|
||||
// sanitize any padIDs before continuing
|
||||
if (fields.padID) {
|
||||
fields.padID = await padManager.sanitizePadId(fields.padID);
|
||||
|
@ -217,7 +206,3 @@ exports.handle = async function (apiVersion: string, functionName: string, field
|
|||
// call the api function
|
||||
return api[functionName].apply(this, functionParams);
|
||||
};
|
||||
|
||||
exports.exportedForTestingOnly = {
|
||||
apiKey: apikey,
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue