metadata-processor.js 2.0 KB

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