| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package samlutils
- import (
- "encoding/xml"
- "yunion.io/x/pkg/errors"
- )
- func ParseMetadata(data []byte) (EntityDescriptor, error) {
- ed := EntityDescriptor{}
- err := xml.Unmarshal(data, &ed)
- if err != nil {
- return ed, errors.Wrap(err, "xml.Unmarshal")
- }
- return ed, nil
- }
- type SSAMLIdpMetadataInput struct {
- EntityId string
- CertString string
- RedirectLoginUrl string
- RedirectLogoutUrl string
- }
- func NewIdpMetadata(input SSAMLIdpMetadataInput) EntityDescriptor {
- desc := EntityDescriptor{
- XMLName: xml.Name{
- Space: XMLNS_MD,
- Local: "EntityDescriptor",
- },
- EntityId: input.EntityId,
- IDPSSODescriptor: &SSODescriptor{
- XMLName: xml.Name{
- Space: XMLNS_MD,
- Local: "IDPSSODescriptor",
- },
- ProtocolSupportEnumeration: PROTOCOL_SAML2,
- KeyDescriptors: []KeyDescriptor{
- {
- XMLName: xml.Name{
- Space: XMLNS_MD,
- Local: "KeyDescriptor",
- },
- Use: KEY_USE_SIGNING,
- KeyInfo: KeyInfo{
- XMLName: xml.Name{
- Space: XMLNS_DS,
- Local: "KeyInfo",
- },
- X509Data: &X509Data{
- XMLName: xml.Name{
- Space: XMLNS_DS,
- Local: "X509Data",
- },
- X509Certificate: X509Certificate{
- XMLName: xml.Name{
- Space: XMLNS_DS,
- Local: "X509Certificate",
- },
- Cert: input.CertString,
- },
- },
- },
- },
- {
- XMLName: xml.Name{
- Space: XMLNS_MD,
- Local: "KeyDescriptor",
- },
- Use: KEY_USE_ENCRYPTION,
- KeyInfo: KeyInfo{
- XMLName: xml.Name{
- Space: XMLNS_DS,
- Local: "KeyInfo",
- },
- X509Data: &X509Data{
- XMLName: xml.Name{
- Space: XMLNS_DS,
- Local: "X509Data",
- },
- X509Certificate: X509Certificate{
- XMLName: xml.Name{
- Space: XMLNS_DS,
- Local: "X509Certificate",
- },
- Cert: input.CertString,
- },
- },
- },
- },
- },
- SingleLogoutServices: []SSAMLService{
- {
- XMLName: xml.Name{
- Space: XMLNS_MD,
- Local: "SingleLogoutService",
- },
- Binding: BINDING_HTTP_REDIRECT,
- Location: input.RedirectLogoutUrl,
- },
- },
- NameIDFormat: []SSAMLNameIDFormat{
- {
- XMLName: xml.Name{
- Space: XMLNS_MD,
- Local: "NameIDFormat",
- },
- Format: NAME_ID_FORMAT_TRANSIENT,
- },
- },
- SingleSignOnServices: []SSAMLService{
- {
- XMLName: xml.Name{
- Space: XMLNS_MD,
- Local: "SingleSignOnService",
- },
- Binding: BINDING_HTTP_REDIRECT,
- Location: input.RedirectLoginUrl,
- },
- },
- },
- }
- return desc
- }
- type SSAMLSpMetadataInput struct {
- EntityId string
- CertString string
- AssertionConsumerUrl string
- ServiceName string
- RequestedAttributes []RequestedAttribute
- }
- func NewSpMetadata(input SSAMLSpMetadataInput) EntityDescriptor {
- strTrue := "true"
- strIndex := "1"
- reqAttrs := make([]RequestedAttribute, len(input.RequestedAttributes))
- for i := range input.RequestedAttributes {
- reqAttrs[i] = RequestedAttribute{
- XMLName: xml.Name{
- Space: XMLNS_MD,
- Local: "RequestedAttribute",
- },
- IsRequired: input.RequestedAttributes[i].IsRequired,
- Name: input.RequestedAttributes[i].Name,
- FriendlyName: input.RequestedAttributes[i].FriendlyName,
- }
- }
- desc := EntityDescriptor{
- XMLName: xml.Name{
- Space: XMLNS_MD,
- Local: "EntityDescriptor",
- },
- EntityId: input.EntityId,
- SPSSODescriptor: &SSODescriptor{
- XMLName: xml.Name{
- Space: XMLNS_MD,
- Local: "SPSSODescriptor",
- },
- ProtocolSupportEnumeration: PROTOCOL_SAML2,
- WantAssertionsSigned: &strTrue,
- KeyDescriptors: []KeyDescriptor{
- {
- XMLName: xml.Name{
- Space: XMLNS_MD,
- Local: "KeyDescriptor",
- },
- Use: KEY_USE_SIGNING,
- KeyInfo: KeyInfo{
- XMLName: xml.Name{
- Space: XMLNS_DS,
- Local: "KeyInfo",
- },
- X509Data: &X509Data{
- XMLName: xml.Name{
- Space: XMLNS_DS,
- Local: "X509Data",
- },
- X509Certificate: X509Certificate{
- XMLName: xml.Name{
- Space: XMLNS_DS,
- Local: "X509Certificate",
- },
- Cert: input.CertString,
- },
- },
- },
- },
- {
- XMLName: xml.Name{
- Space: XMLNS_MD,
- Local: "KeyDescriptor",
- },
- Use: KEY_USE_ENCRYPTION,
- KeyInfo: KeyInfo{
- XMLName: xml.Name{
- Space: XMLNS_DS,
- Local: "KeyInfo",
- },
- X509Data: &X509Data{
- XMLName: xml.Name{
- Space: XMLNS_DS,
- Local: "X509Data",
- },
- X509Certificate: X509Certificate{
- XMLName: xml.Name{
- Space: XMLNS_DS,
- Local: "X509Certificate",
- },
- Cert: input.CertString,
- },
- },
- },
- },
- },
- NameIDFormat: []SSAMLNameIDFormat{
- {
- XMLName: xml.Name{
- Space: XMLNS_MD,
- Local: "NameIDFormat",
- },
- Format: NAME_ID_FORMAT_TRANSIENT,
- },
- },
- AssertionConsumerServices: []SSAMLService{
- {
- XMLName: xml.Name{
- Space: XMLNS_MD,
- Local: "AssertionConsumerService",
- },
- Binding: BINDING_HTTP_POST,
- Location: input.AssertionConsumerUrl,
- Index: &strIndex,
- IsDefault: &strTrue,
- },
- },
- AttributeConsumingServices: []AttributeConsumingService{
- {
- XMLName: xml.Name{
- Space: XMLNS_MD,
- Local: "AttributeConsumingService",
- },
- Index: strIndex,
- ServiceName: SXMLText{
- XMLName: xml.Name{
- Space: XMLNS_MD,
- Local: "ServiceName",
- },
- Lang: "en",
- Text: input.ServiceName,
- },
- RequestedAttributes: reqAttrs,
- },
- },
- },
- }
- return desc
- }
|