metadata.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package saml
  2. import (
  3. "encoding/xml"
  4. "time"
  5. "github.com/beevik/etree"
  6. )
  7. // HTTPPostBinding is the official URN for the HTTP-POST binding (transport)
  8. var HTTPPostBinding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
  9. // HTTPRedirectBinding is the official URN for the HTTP-Redirect binding (transport)
  10. var HTTPRedirectBinding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
  11. // EntitiesDescriptor represents the SAML object of the same name.
  12. //
  13. // See http://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf §2.3.1
  14. type EntitiesDescriptor struct {
  15. XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:metadata EntitiesDescriptor"`
  16. ID *string `xml:",attr,omitempty"`
  17. ValidUntil *time.Time `xml:"validUntil,attr,omitempty"`
  18. CacheDuration *time.Duration `xml:"cacheDuration,attr,omitempty"`
  19. Name *string `xml:",attr,omitempty"`
  20. Signature *etree.Element
  21. EntitiesDescriptors []EntitiesDescriptor `xml:"urn:oasis:names:tc:SAML:2.0:metadata EntitiesDescriptor"`
  22. EntityDescriptors []EntityDescriptor `xml:"urn:oasis:names:tc:SAML:2.0:metadata EntityDescriptor"`
  23. }
  24. // Metadata as been renamed to EntityDescriptor
  25. //
  26. // This change was made to be consistent with the rest of the API which uses names
  27. // from the SAML specification for types.
  28. //
  29. // This is a tombstone to help you discover this fact. You should update references
  30. // to saml.Metadata to be saml.EntityDescriptor.
  31. var Metadata = struct{}{}
  32. // EntityDescriptor represents the SAML EntityDescriptor object.
  33. //
  34. // See http://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf §2.3.2
  35. type EntityDescriptor struct {
  36. XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:metadata EntityDescriptor"`
  37. EntityID string `xml:"entityID,attr"`
  38. ID string `xml:",attr,omitempty"`
  39. ValidUntil time.Time `xml:"validUntil,attr,omitempty"`
  40. CacheDuration time.Duration `xml:"cacheDuration,attr,omitempty"`
  41. Signature *etree.Element
  42. RoleDescriptors []RoleDescriptor `xml:"RoleDescriptor"`
  43. IDPSSODescriptors []IDPSSODescriptor `xml:"IDPSSODescriptor"`
  44. SPSSODescriptors []SPSSODescriptor `xml:"SPSSODescriptor"`
  45. AuthnAuthorityDescriptors []AuthnAuthorityDescriptor `xml:"AuthnAuthorityDescriptor"`
  46. AttributeAuthorityDescriptors []AttributeAuthorityDescriptor `xml:"AttributeAuthorityDescriptor"`
  47. PDPDescriptors []PDPDescriptor `xml:"PDPDescriptor"`
  48. AffiliationDescriptor *AffiliationDescriptor
  49. Organization *Organization
  50. ContactPerson *ContactPerson
  51. AdditionalMetadataLocations []string `xml:"AdditionalMetadataLocation"`
  52. }
  53. // MarshalXML implements xml.Marshaler
  54. func (m EntityDescriptor) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  55. type Alias EntityDescriptor
  56. aux := &struct {
  57. ValidUntil RelaxedTime `xml:"validUntil,attr,omitempty"`
  58. CacheDuration Duration `xml:"cacheDuration,attr,omitempty"`
  59. *Alias
  60. }{
  61. ValidUntil: RelaxedTime(m.ValidUntil),
  62. CacheDuration: Duration(m.CacheDuration),
  63. Alias: (*Alias)(&m),
  64. }
  65. return e.Encode(aux)
  66. }
  67. // UnmarshalXML implements xml.Unmarshaler
  68. func (m *EntityDescriptor) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  69. type Alias EntityDescriptor
  70. aux := &struct {
  71. ValidUntil RelaxedTime `xml:"validUntil,attr,omitempty"`
  72. CacheDuration Duration `xml:"cacheDuration,attr,omitempty"`
  73. *Alias
  74. }{
  75. Alias: (*Alias)(m),
  76. }
  77. if err := d.DecodeElement(aux, &start); err != nil {
  78. return err
  79. }
  80. m.ValidUntil = time.Time(aux.ValidUntil)
  81. m.CacheDuration = time.Duration(aux.CacheDuration)
  82. return nil
  83. }
  84. // Organization represents the SAML Organization object.
  85. //
  86. // See http://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf §2.3.2.1
  87. type Organization struct {
  88. OrganizationNames []LocalizedName `xml:"OrganizationName"`
  89. OrganizationDisplayNames []LocalizedName `xml:"OrganizationDisplayName"`
  90. OrganizationURLs []LocalizedURI `xml:"OrganizationURL"`
  91. }
  92. // LocalizedName represents the SAML type localizedNameType.
  93. //
  94. // See http://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf §2.2.4
  95. type LocalizedName struct {
  96. Lang string `xml:"xml lang,attr"`
  97. Value string `xml:",chardata"`
  98. }
  99. // LocalizedURI represents the SAML type localizedURIType.
  100. //
  101. // See http://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf §2.2.5
  102. type LocalizedURI struct {
  103. Lang string `xml:"xml lang,attr"`
  104. Value string `xml:",chardata"`
  105. }
  106. // ContactPerson represents the SAML element ContactPerson.
  107. //
  108. // See http://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf §2.3.2.2
  109. type ContactPerson struct {
  110. ContactType string `xml:"contactType,attr"`
  111. Company string
  112. GivenName string
  113. SurName string
  114. EmailAddresses []string `xml:"EmailAddress"`
  115. TelephoneNumbers []string `xml:"TelephoneNumber"`
  116. }
  117. // RoleDescriptor represents the SAML element RoleDescriptor.
  118. //
  119. // See http://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf §2.4.1
  120. type RoleDescriptor struct {
  121. ID string `xml:",attr,omitempty"`
  122. ValidUntil time.Time `xml:"validUntil,attr,omitempty"`
  123. CacheDuration time.Duration `xml:"cacheDuration,attr,omitempty"`
  124. ProtocolSupportEnumeration string `xml:"protocolSupportEnumeration,attr"`
  125. ErrorURL string `xml:"errorURL,attr,omitempty"`
  126. Signature *etree.Element
  127. KeyDescriptors []KeyDescriptor `xml:"KeyDescriptor,omitempty"`
  128. Organization *Organization `xml:"Organization,omitempty"`
  129. ContactPeople []ContactPerson `xml:"ContactPerson,omitempty"`
  130. }
  131. // KeyDescriptor represents the XMLSEC object of the same name
  132. type KeyDescriptor struct {
  133. Use string `xml:"use,attr"`
  134. KeyInfo KeyInfo `xml:"http://www.w3.org/2000/09/xmldsig# KeyInfo"`
  135. EncryptionMethods []EncryptionMethod `xml:"EncryptionMethod"`
  136. }
  137. // EncryptionMethod represents the XMLSEC object of the same name
  138. type EncryptionMethod struct {
  139. Algorithm string `xml:"Algorithm,attr"`
  140. }
  141. // KeyInfo represents the XMLSEC object of the same name
  142. //
  143. // TODO(ross): revisit xmldsig and make this type more complete
  144. type KeyInfo struct {
  145. XMLName xml.Name `xml:"http://www.w3.org/2000/09/xmldsig# KeyInfo"`
  146. Certificate string `xml:"X509Data>X509Certificate"`
  147. }
  148. // Endpoint represents the SAML EndpointType object.
  149. //
  150. // See http://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf §2.2.2
  151. type Endpoint struct {
  152. Binding string `xml:"Binding,attr"`
  153. Location string `xml:"Location,attr"`
  154. ResponseLocation string `xml:"ResponseLocation,attr,omitempty"`
  155. }
  156. // IndexedEndpoint represents the SAML IndexedEndpointType object.
  157. //
  158. // See http://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf §2.2.3
  159. type IndexedEndpoint struct {
  160. Binding string `xml:"Binding,attr"`
  161. Location string `xml:"Location,attr"`
  162. ResponseLocation *string `xml:"ResponseLocation,attr,omitempty"`
  163. Index int `xml:"index,attr"`
  164. IsDefault *bool `xml:"isDefault,attr"`
  165. }
  166. // SSODescriptor represents the SAML complex type SSODescriptor
  167. //
  168. // See http://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf §2.4.2
  169. type SSODescriptor struct {
  170. RoleDescriptor
  171. ArtifactResolutionServices []IndexedEndpoint `xml:"ArtifactResolutionService"`
  172. SingleLogoutServices []Endpoint `xml:"SingleLogoutService"`
  173. ManageNameIDServices []Endpoint `xml:"ManageNameIDService"`
  174. NameIDFormats []NameIDFormat `xml:"NameIDFormat"`
  175. }
  176. // IDPSSODescriptor represents the SAML IDPSSODescriptorType object.
  177. //
  178. // See http://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf §2.4.3
  179. type IDPSSODescriptor struct {
  180. XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:metadata IDPSSODescriptor"`
  181. SSODescriptor
  182. WantAuthnRequestsSigned *bool `xml:",attr"`
  183. SingleSignOnServices []Endpoint `xml:"SingleSignOnService"`
  184. NameIDMappingServices []Endpoint `xml:"NameIDMappingService"`
  185. AssertionIDRequestServices []Endpoint `xml:"AssertionIDRequestService"`
  186. AttributeProfiles []string `xml:"AttributeProfile"`
  187. Attributes []Attribute `xml:"Attribute"`
  188. }
  189. // SPSSODescriptor represents the SAML SPSSODescriptorType object.
  190. //
  191. // See http://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf §2.4.2
  192. type SPSSODescriptor struct {
  193. XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:metadata SPSSODescriptor"`
  194. SSODescriptor
  195. AuthnRequestsSigned *bool `xml:",attr"`
  196. WantAssertionsSigned *bool `xml:",attr"`
  197. AssertionConsumerServices []IndexedEndpoint `xml:"AssertionConsumerService"`
  198. AttributeConsumingServices []AttributeConsumingService `xml:"AttributeConsumingService"`
  199. }
  200. // AttributeConsumingService represents the SAML AttributeConsumingService object.
  201. //
  202. // See http://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf §2.4.4.1
  203. type AttributeConsumingService struct {
  204. Index int `xml:"index,attr"`
  205. IsDefault *bool `xml:"isDefault,attr"`
  206. ServiceNames []LocalizedName `xml:"ServiceName"`
  207. ServiceDescriptions []LocalizedName `xml:"ServiceDescription"`
  208. RequestedAttributes []RequestedAttribute `xml:"RequestedAttribute"`
  209. }
  210. // RequestedAttribute represents the SAML RequestedAttribute object.
  211. //
  212. // See http://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf §2.4.4.2
  213. type RequestedAttribute struct {
  214. Attribute
  215. IsRequired *bool `xml:"isRequired,attr"`
  216. }
  217. // AuthnAuthorityDescriptor represents the SAML AuthnAuthorityDescriptor object.
  218. //
  219. // See http://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf §2.4.5
  220. type AuthnAuthorityDescriptor struct {
  221. RoleDescriptor
  222. AuthnQueryServices []Endpoint `xml:"AuthnQueryService"`
  223. AssertionIDRequestServices []Endpoint `xml:"AssertionIDRequestService"`
  224. NameIDFormats []NameIDFormat `xml:"NameIDFormat"`
  225. }
  226. // PDPDescriptor represents the SAML PDPDescriptor object.
  227. //
  228. // See http://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf §2.4.6
  229. type PDPDescriptor struct {
  230. RoleDescriptor
  231. AuthzServices []Endpoint `xml:"AuthzService"`
  232. AssertionIDRequestServices []Endpoint `xml:"AssertionIDRequestService"`
  233. NameIDFormats []NameIDFormat `xml:"NameIDFormat"`
  234. }
  235. // AttributeAuthorityDescriptor represents the SAML AttributeAuthorityDescriptor object.
  236. //
  237. // See http://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf §2.4.7
  238. type AttributeAuthorityDescriptor struct {
  239. RoleDescriptor
  240. AttributeServices []Endpoint `xml:"AttributeService"`
  241. AssertionIDRequestServices []Endpoint `xml:"AssertionIDRequestService"`
  242. NameIDFormats []NameIDFormat `xml:"NameIDFormat"`
  243. AttributeProfiles []string `xml:"AttributeProfile"`
  244. Attributes []Attribute `xml:"Attribute"`
  245. }
  246. // AffiliationDescriptor represents the SAML AffiliationDescriptor object.
  247. //
  248. // See http://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf §2.5
  249. type AffiliationDescriptor struct {
  250. AffiliationOwnerID string `xml:"affiliationOwnerID,attr"`
  251. ID string `xml:",attr"`
  252. ValidUntil time.Time `xml:"validUntil,attr,omitempty"`
  253. CacheDuration time.Duration `xml:"cacheDuration,attr"`
  254. Signature *etree.Element
  255. AffiliateMembers []string `xml:"AffiliateMember"`
  256. KeyDescriptors []KeyDescriptor `xml:"KeyDescriptor"`
  257. }