This is the official SDK to build apps for d.velop cloud using node.js and typescirpt.
This SDK is diveded into apps. Install individual packages per d-velop app you want to use.
npm i @dvelop/identityprovider @dvelop/task
import { getAuthSessionByApiKey } from '@dvelop/identityprovider';
import * as taskApp from '@dvelop/task';
(async function main() {
const authSessionId = await getAuthSessionByApiKey('<SYSTEM_BASE_URI>', '<API_KEY>');
await taskApp.completeTask(systemBaseUri, authSessionId, '<TASK_LOCATION>');
})();
This SDK was designed framework agnostic but with express in mind.
npm i express cookie-parser @dvelop-sdk/app-router @dvelop-sdk/identityprovider
npm i @types/express @types/cookie-parser -D
import express, { Application, Request, Response, NextFunction } from "express";
import cookieParser from "cookie-parser";
import * as appRouter from "@dvelop-sdk/app-router";
import { validateAuthSessionId, getLoginRedirectionUri, ScimUser } from "@dvelop-sdk/identityprovider";
const app: Application = express();
const appName: string = "acme-lklo";
declare global {
namespace Express {
interface Request {
systemBaseUri?: string;
tenantId?: string;
requestId?: string;
principal?: ScimUser;
}
}
}
// Middleware
app.use(cookieParser());
app.use((req: Request, res: Response, next: NextFunction) => {
const systemBaseUri: string = req.header(appRouter.DVELOP_SYSTEM_BASE_URI_HEADER);
const tenantId: string = req.header(appRouter.DVELOP_TENANT_ID_HEADER);
const requestSignature: string = req.header(appRouter.DVELOP_REQUEST_SIGNATURE_HEADER);
const validRequest: boolean = appRouter.validateRequestSignature(process.env.SIGNATURE_SECRET, systemBaseUri, tenantId, requestSignature)
if (validRequest) {
req.systemBaseUri = systemBaseUri;
req.tenantId = tenantId
req.requestId = req.header(appRouter.DVELOP_REQUEST_ID_HEADER);
next();
} else {
res.status(403).send('Forbidden');
}
});
// Routes
app.get(`/${appName}/me`, async (req: Request, res: Response) => {
let authSessionId: string;
const authorizationHeader = req.get("Authorization");
const authorizationCookie = req.cookies["AuthSessionId"];
if (authorizationHeader) {
authSessionId = new RegExp(/^bearer (.*)$/, "i").exec(authorizationHeader)[1];
} else if (authorizationCookie) {
authSessionId = authorizationCookie;
}
try {
const user: ScimUser = await validateAuthSessionId(req.systemBaseUri, authSessionId);
req.principal = user;
res.status(200).send(`Hello ${req.principal.displayName}!`)
} catch (e) {
console.log("Unauthorized. Redirecting ...");
res.redirect(getLoginRedirectionUri(req.originalUrl));
}
});
app.get(`/${appName}`, (_: Request, res: Response) => {
res.status(200).send(`Hello from ${process.env.npm_package_name} (${process.env.npm_package_version})!`);
});
app.use("/", (_: Request, res: Response) => {
res.redirect(`/${appName}`);
});
// Start
app.listen(8000, () => {
console.log(`Server listening on port 8000`);
});
This project is maintained by d-velop but is looking for contributers. If you consider contributing to this project please read CONTRIBUTING for details on how to get started.
Please read LICENSE for licensing information.
Thanks to the following projects for inspiration