ukamf-entity.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict'
  2. const _ = require('lodash')
  3. class UKAMFEntity {
  4. constructor(data) {
  5. this.data = data
  6. }
  7. getSamlConfig() {
  8. let hiddenIdP = false
  9. const idp = this.data.IDPSSODescriptor[0]
  10. const idpMetaData =
  11. _.get(this.data, [
  12. 'Extensions',
  13. 0,
  14. 'mdattr:EntityAttributes',
  15. 0,
  16. 'saml:Attribute',
  17. ]) || []
  18. idpMetaData.forEach(data => {
  19. const value = _.get(data, ['saml:AttributeValue', 0])
  20. if (
  21. value === 'http://refeds.org/category/hide-from-discovery' ||
  22. value === 'https://refeds.org/category/hide-from-discovery'
  23. ) {
  24. hiddenIdP = true
  25. }
  26. })
  27. const keys = idp.KeyDescriptor
  28. const signingKey =
  29. keys.length === 1
  30. ? keys[0]
  31. : keys.find(key => _.get(key, ['$', 'use']) === 'signing')
  32. const entityId = this.data.$.entityID
  33. let cert = _.get(signingKey, [
  34. 'ds:KeyInfo',
  35. 0,
  36. 'ds:X509Data',
  37. 0,
  38. 'ds:X509Certificate',
  39. 0,
  40. ])
  41. if (!cert) {
  42. throw new Error('no cert')
  43. }
  44. cert = cert.replace(/\s/g, '')
  45. let entryPoint = idp.SingleSignOnService.find(
  46. sso =>
  47. _.get(sso, ['$', 'Binding']) ===
  48. 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect'
  49. )
  50. entryPoint = _.get(entryPoint, ['$', 'Location'])
  51. if (!entryPoint) {
  52. throw new Error('no entryPoint')
  53. }
  54. return {
  55. cert,
  56. entityId,
  57. entryPoint,
  58. hiddenIdP,
  59. }
  60. }
  61. }
  62. module.exports = UKAMFEntity