ltijs
Version:
Easily turn your web application into a LTI 1.3 Learning Tool.
52 lines • 2.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KeysetService = void 0;
const http_handler_types_1 = require("#services/http-handler/http-handler.types");
const keys_1 = require("#utils/crypto/keys");
const jwt_constants_1 = require("#utils/crypto/jwt.constants");
const cache_manager_utils_1 = require("#services/cache-manager/cache-manager.utils");
const keyset_constants_1 = require("#services/keyset/keyset.constants");
class KeysetService {
LOG_COMPONENT = 'keysetService';
DEFAULT_KEYSET_ROUTE = '/lti/keys';
SIG_USE = 'sig';
KEYSET_CACHE_TTL_MS = 60 * 1000;
platformManager;
httpHandler;
cacheManager;
logger;
constructor(platformManager, httpHandler, cacheManager, logger) {
this.platformManager = platformManager;
this.httpHandler = httpHandler;
this.cacheManager = cacheManager;
this.logger = logger;
}
prepareHttpRoutes(route = this.DEFAULT_KEYSET_ROUTE) {
this.httpHandler.registerRoute(route, [http_handler_types_1.HttpMethod.Get], async (_request, response) => {
const keyset = await this.buildKeyset();
this.logger.debug(this.LOG_COMPONENT, `Served keyset with ${keyset.keys.length} key(s)`);
response.json(keyset);
});
}
// Short-TTL cache (via the injected CacheManager): this endpoint is polled by every registered
// platform verifying a signed response, and buildKeyset() would otherwise re-scan the full platform
// table (plus, on the mongo-legacy store, 3 additional queries per returned platform) on every single
// hit. The TTL is a safety net, not the primary invalidation mechanism; PlatformManager actively
// deletes this same KEYSET_CACHE_KEY entry on register/delete/rotateKeys, so a change is normally
// visible immediately, not just once the TTL happens to expire.
async buildKeyset() {
const cached = await (0, cache_manager_utils_1.safeCacheGet)(this.cacheManager, keyset_constants_1.KEYSET_CACHE_KEY);
if (cached !== undefined)
return cached;
const platforms = await this.platformManager.getPlatforms();
const keyset = { keys: platforms.map(platform => this.buildJwk(platform)) };
await (0, cache_manager_utils_1.safeCacheSet)(this.cacheManager, keyset_constants_1.KEYSET_CACHE_KEY, keyset, this.KEYSET_CACHE_TTL_MS);
return keyset;
}
buildJwk(platform) {
return { ...(0, keys_1.rsaToJwk)(platform.keys.public), kid: platform.id, alg: jwt_constants_1.RS256_ALGORITHM, use: this.SIG_USE };
}
}
exports.KeysetService = KeysetService;
exports.default = KeysetService;
//# sourceMappingURL=keyset.service.js.map