Skip to content

Official SDK to build Apps for d.velop cloud

License

Notifications You must be signed in to change notification settings

1muen/dvelop-sdk-node

 
 

Repository files navigation

GitHub

npm (scoped) npm (scoped) npm (scoped)

GitHub milestone

dvelop-sdk-node

Explore the docs »

About

This is the official SDK to build apps for d.velop cloud using node.js and typescirpt.

Getting started

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>');
})();

Build an app with express

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`);
});

Contributing

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.

License

Please read LICENSE for licensing information.

Acknowledgments

Thanks to the following projects for inspiration

About

Official SDK to build Apps for d.velop cloud

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 100.0%