metadata-processor.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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${samlConfig.cert}\n-----END CERTIFICATE-----`,
  31. 'utf8'
  32. )
  33. )
  34. const validFrom = moment(certificate.validFrom)
  35. const validTo = moment(certificate.validTo)
  36. if (validFrom.isAfter(moment())) {
  37. throw new Error(`certificate not valid till: ${validFrom.format('LLL')}`)
  38. }
  39. if (validTo.isBefore(moment())) {
  40. throw new Error(`certificate expired: ${validTo.format('LLL')}`)
  41. }
  42. console.log(
  43. `!!!!!!!!!!!!!\nCERTIFICATE EXPIRES: ${validTo.format(
  44. 'LLL'
  45. )}\n!!!!!!!!!!!!!\n`
  46. )
  47. console.log(`SSO Entity ID: ${samlConfig.entityId}\n`)
  48. console.log(`SSO Entry Point: ${samlConfig.entryPoint}\n`)
  49. console.log(`SSO Certificate: ${samlConfig.cert}\n`)
  50. }