metadata-processor.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict'
  2. /**
  3. * Run with: node metadata-processor /path/ukamf.xml http://idp/entity/id
  4. *
  5. * `npm install` must be run for scripts/ukamf first.
  6. *
  7. * The ukamf metadata xml file can be downloaded from:
  8. * http://metadata.ukfederation.org.uk/
  9. *
  10. * The entity id should be provided by the university.
  11. */
  12. const { Certificate } = require('@fidm/x509')
  13. const moment = require('moment')
  14. const UKAMFDB = require('./ukamf-db')
  15. main().catch(err => {
  16. console.error(err.stack)
  17. })
  18. async function main() {
  19. const [, , file, entityId] = process.argv
  20. console.log(`loading file ${file}...\n`)
  21. const ukamfDB = new UKAMFDB(file)
  22. await ukamfDB.init()
  23. const entity = ukamfDB.findByEntityID(entityId)
  24. if (!entity) {
  25. throw new Error(`could not find entity for ${entityId}`)
  26. }
  27. const samlConfig = entity.getSamlConfig()
  28. const certificate = Certificate.fromPEM(
  29. Buffer.from(
  30. `-----BEGIN CERTIFICATE-----\n${
  31. samlConfig.cert
  32. }\n-----END CERTIFICATE-----`,
  33. 'utf8'
  34. )
  35. )
  36. const validFrom = moment(certificate.validFrom)
  37. const validTo = moment(certificate.validTo)
  38. if (validFrom.isAfter(moment())) {
  39. throw new Error(`certificate not valid till: ${validFrom.format('LLL')}`)
  40. }
  41. if (validTo.isBefore(moment())) {
  42. throw new Error(`certificate expired: ${validTo.format('LLL')}`)
  43. }
  44. console.log(
  45. `!!!!!!!!!!!!!\nCERTIFICATE EXPIRES: ${validTo.format(
  46. 'LLL'
  47. )}\n!!!!!!!!!!!!!\n`
  48. )
  49. console.log(`SSO Entity ID: ${samlConfig.entityId}\n`)
  50. console.log(`SSO Entry Point: ${samlConfig.entryPoint}\n`)
  51. console.log(`SSO Certificate: ${samlConfig.cert}\n`)
  52. }