ukamf-entity.js 1.5 KB

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