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