github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/model/saml.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package model 5 6 import ( 7 "encoding/json" 8 "encoding/xml" 9 "io" 10 "time" 11 ) 12 13 const ( 14 USER_AUTH_SERVICE_SAML = "saml" 15 USER_AUTH_SERVICE_SAML_TEXT = "SAML" 16 ) 17 18 type SamlAuthRequest struct { 19 Base64AuthRequest string 20 URL string 21 RelayState string 22 } 23 24 type SamlCertificateStatus struct { 25 IdpCertificateFile bool `json:"idp_certificate_file"` 26 PrivateKeyFile bool `json:"private_key_file"` 27 PublicCertificateFile bool `json:"public_certificate_file"` 28 } 29 30 type SamlMetadataResponse struct { 31 IdpDescriptorUrl string `json:"idp_descriptor_url"` 32 IdpUrl string `json:"idp_url"` 33 IdpPublicCertificate string `json:"idp_public_certificate"` 34 } 35 36 type NameIDFormat struct { 37 XMLName xml.Name 38 Format string `xml:",attr,omitempty"` 39 Value string `xml:",innerxml"` 40 } 41 42 type NameID struct { 43 NameQualifier string `xml:",attr"` 44 SPNameQualifier string `xml:",attr"` 45 Format string `xml:",attr,omitempty"` 46 SPProvidedID string `xml:",attr"` 47 Value string `xml:",chardata"` 48 } 49 50 type AttributeValue struct { 51 Type string `xml:"http://www.w3.org/2001/XMLSchema-instance type,attr"` 52 Value string `xml:",chardata"` 53 NameID *NameID 54 } 55 56 type Attribute struct { 57 XMLName xml.Name 58 FriendlyName string `xml:",attr"` 59 Name string `xml:",attr"` 60 NameFormat string `xml:",attr"` 61 Values []AttributeValue `xml:"AttributeValue"` 62 } 63 64 type Endpoint struct { 65 XMLName xml.Name 66 Binding string `xml:"Binding,attr"` 67 Location string `xml:"Location,attr"` 68 ResponseLocation string `xml:"ResponseLocation,attr,omitempty"` 69 } 70 71 type IndexedEndpoint struct { 72 XMLName xml.Name 73 Binding string `xml:"Binding,attr"` 74 Location string `xml:"Location,attr"` 75 ResponseLocation *string `xml:"ResponseLocation,attr,omitempty"` 76 Index int `xml:"index,attr"` 77 IsDefault *bool `xml:"isDefault,attr"` 78 } 79 80 type IDPSSODescriptor struct { 81 XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:metadata IDPSSODescriptor"` 82 SSODescriptor 83 WantAuthnRequestsSigned *bool `xml:",attr"` 84 85 SingleSignOnServices []Endpoint `xml:"SingleSignOnService"` 86 NameIDMappingServices []Endpoint `xml:"NameIDMappingService"` 87 AssertionIDRequestServices []Endpoint `xml:"AssertionIDRequestService"` 88 AttributeProfiles []string `xml:"AttributeProfile"` 89 Attributes []Attribute `xml:"Attribute"` 90 } 91 92 type SSODescriptor struct { 93 XMLName xml.Name 94 RoleDescriptor 95 ArtifactResolutionServices []IndexedEndpoint `xml:"ArtifactResolutionService"` 96 SingleLogoutServices []Endpoint `xml:"SingleLogoutService"` 97 ManageNameIDServices []Endpoint `xml:"ManageNameIDService"` 98 NameIDFormats []NameIDFormat `xml:"NameIDFormat"` 99 } 100 101 type X509Certificate struct { 102 XMLName xml.Name 103 Cert string `xml:",innerxml"` 104 } 105 106 type X509Data struct { 107 XMLName xml.Name 108 X509Certificate X509Certificate `xml:"X509Certificate"` 109 } 110 111 type KeyInfo struct { 112 XMLName xml.Name 113 DS string `xml:"xmlns:ds,attr"` 114 X509Data X509Data `xml:"X509Data"` 115 } 116 type EncryptionMethod struct { 117 Algorithm string `xml:"Algorithm,attr"` 118 } 119 120 type KeyDescriptor struct { 121 XMLName xml.Name 122 Use string `xml:"use,attr,omitempty"` 123 KeyInfo KeyInfo `xml:"http://www.w3.org/2000/09/xmldsig# KeyInfo,omitempty"` 124 } 125 126 type RoleDescriptor struct { 127 XMLName xml.Name 128 ID string `xml:",attr,omitempty"` 129 ValidUntil time.Time `xml:"validUntil,attr,omitempty"` 130 CacheDuration time.Duration `xml:"cacheDuration,attr,omitempty"` 131 ProtocolSupportEnumeration string `xml:"protocolSupportEnumeration,attr"` 132 ErrorURL string `xml:"errorURL,attr,omitempty"` 133 KeyDescriptors []KeyDescriptor `xml:"KeyDescriptor,omitempty"` 134 Organization *Organization `xml:"Organization,omitempty"` 135 ContactPersons []ContactPerson `xml:"ContactPerson,omitempty"` 136 } 137 138 type ContactPerson struct { 139 XMLName xml.Name 140 ContactType string `xml:"contactType,attr"` 141 Company string 142 GivenName string 143 SurName string 144 EmailAddresses []string `xml:"EmailAddress"` 145 TelephoneNumbers []string `xml:"TelephoneNumber"` 146 } 147 148 type LocalizedName struct { 149 Lang string `xml:"xml lang,attr"` 150 Value string `xml:",chardata"` 151 } 152 153 type LocalizedURI struct { 154 Lang string `xml:"xml lang,attr"` 155 Value string `xml:",chardata"` 156 } 157 158 type Organization struct { 159 XMLName xml.Name 160 OrganizationNames []LocalizedName `xml:"OrganizationName"` 161 OrganizationDisplayNames []LocalizedName `xml:"OrganizationDisplayName"` 162 OrganizationURLs []LocalizedURI `xml:"OrganizationURL"` 163 } 164 165 type EntityDescriptor struct { 166 XMLName xml.Name `xml:"urn:oasis:names:tc:SAML:2.0:metadata EntityDescriptor"` 167 EntityID string `xml:"entityID,attr"` 168 ID string `xml:",attr,omitempty"` 169 ValidUntil time.Time `xml:"validUntil,attr,omitempty"` 170 CacheDuration time.Duration `xml:"cacheDuration,attr,omitempty"` 171 RoleDescriptors []RoleDescriptor `xml:"RoleDescriptor"` 172 IDPSSODescriptors []IDPSSODescriptor `xml:"IDPSSODescriptor"` 173 Organization Organization `xml:"Organization"` 174 ContactPerson ContactPerson `xml:"ContactPerson"` 175 } 176 177 func (s *SamlCertificateStatus) ToJson() string { 178 b, _ := json.Marshal(s) 179 return string(b) 180 } 181 182 func SamlCertificateStatusFromJson(data io.Reader) *SamlCertificateStatus { 183 var status *SamlCertificateStatus 184 json.NewDecoder(data).Decode(&status) 185 return status 186 } 187 188 func (s *SamlMetadataResponse) ToJson() string { 189 b, _ := json.Marshal(s) 190 return string(b) 191 } 192 193 func SamlMetadataResponseFromJson(data io.Reader) *SamlMetadataResponse { 194 var status *SamlMetadataResponse 195 json.NewDecoder(data).Decode(&status) 196 return status 197 }