metadata-processor.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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(`UPDATE universities SET
  50. sso_entity_id = '${samlConfig.entityId}',
  51. sso_entry_point = '${samlConfig.entryPoint}',
  52. sso_cert = '${samlConfig.cert}',
  53. sso_user_id_attribute = 'eduPersonPrincipalName',
  54. sso_user_email_attribute = 'mail',
  55. sso_license_entitlement_attribute = 'eduPersonPrincipalName',
  56. sso_license_entitlement_matcher = '.'
  57. WHERE id =
  58. `)
  59. }