Skip to content

Commit

Permalink
Allow passing LTI platform details through env
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsTheGlidingSquirrel committed Jun 11, 2024
1 parent ca34bc4 commit f88ab0e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
10 changes: 9 additions & 1 deletion .env-template
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# A template for your local .env file
# A template for your local .env file during local development

# Symmetric HS256 key used by ltijs to sign ltik and database entries & to sign the jwt access token
LTIJS_KEY=DONOTUSETHISKEYINPRODUCTION

# https://www.mongodb.com/docs/drivers/go/current/fundamentals/connections/connection-guide/#connection-uri
MONGODB_CONNECTION_URI=mongodb://localhost:27017/

# LTI platform
LTI_PLATFORM_URL=https://saltire.lti.app/platform
LTI_PLATFORM_NAME=saltire.lti.app
LTI_PLATFORM_CLIENT_ID=saltire.lti.app
LTI_PLATFORM_AUTHENTICATION_ENDPOINT=https://saltire.lti.app/platform/auth
LTI_PLATFORM_ACCESS_TOKEN_ENDPOINT=https://saltire.lti.app/platform/token/sc24671cd70c6e45554e6c405a2f5d966
LTI_PLATFORM_KEYSET_ENDPOINT=https://saltire.lti.app/platform/jwks/sc24671cd70c6e45554e6c405a2f5d966
50 changes: 30 additions & 20 deletions src/backend/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { Provider as ltijs } from "ltijs";
import "dotenv/config";
import path from "node:path";
import path from "path";
import * as t from "io-ts";
import * as jwt from "jsonwebtoken";

const __dirname = import.meta.dirname;

const ltijsKey = process.env.LTIJS_KEY;
if (!ltijsKey) {
throw new Error(
"Missing LTIJS_KEY. Please ensure there is an .env file and it contains a LTIJS_KEY."
);
}
const mongodbConnectionUri = process.env.MONGODB_CONNECTION_URI;
if (!mongodbConnectionUri) {
throw new Error(
"Missing MONGODB_CONNECTION_URI. Please ensure there is an .env file and it contains a MONGODB_CONNECTION_URI."
);
}
const ltijsKey = readEnvVariable("LTIJS_KEY");
const mongodbConnectionUri = readEnvVariable("MONGODB_CONNECTION_URI");
const ltiPlatform = {
url: readEnvVariable("LTI_PLATFORM_URL"),
name: readEnvVariable("LTI_PLATFORM_NAME"),
clientId: readEnvVariable("LTI_PLATFORM_CLIENT_ID"),
authenticationEndpoint: readEnvVariable(
"LTI_PLATFORM_AUTHENTICATION_ENDPOINT"
),
accessTokenEndpoint: readEnvVariable("LTI_PLATFORM_ACCESS_TOKEN_ENDPOINT"),
keysetEndpoint: readEnvVariable("LTI_PLATFORM_KEYSET_ENDPOINT"),
};

export const LtiCustomType = t.type({
editor_mode: t.union([t.literal("read"), t.literal("write")]),
Expand All @@ -33,6 +33,7 @@ ltijs.setup(
appUrl: "/lti/launch",
loginUrl: "/lti/login",
keysetUrl: "/lti/keys",
// @ts-expect-error @types/ltijs is missing this
dynRegRoute: "/lti/register",
staticPath: path.join(__dirname, "./../../dist"), // Path to static files
cookies: {
Expand Down Expand Up @@ -203,15 +204,14 @@ const setup = async () => {
* Register platform
*/
await ltijs.registerPlatform({
url: "https://saltire.lti.app/platform",
name: "saltire.lti.app",
clientId: "saltire.lti.app",
authenticationEndpoint: "https://saltire.lti.app/platform/auth",
accesstokenEndpoint:
"https://saltire.lti.app/platform/token/sc24671cd70c6e45554e6c405a2f5d966",
url: ltiPlatform.url,
name: ltiPlatform.name,
clientId: ltiPlatform.clientId,
authenticationEndpoint: ltiPlatform.authenticationEndpoint,
accesstokenEndpoint: ltiPlatform.accessTokenEndpoint,
authConfig: {
method: "JWK_SET",
key: "https://saltire.lti.app/platform/jwks/sc24671cd70c6e45554e6c405a2f5d966",
key: ltiPlatform.keysetEndpoint,
},
});
// await ltijs.registerPlatform({
Expand All @@ -228,3 +228,13 @@ const setup = async () => {
};

setup();

function readEnvVariable(name: string): string {
const value = process.env[name];
if (!value) {
throw new Error(
`Missing env variable ${name}. In local development, please copy '.env-template' to new file '.env' and change values if needed.`
);
}
return value;
}

0 comments on commit f88ab0e

Please sign in to comment.