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