|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Collections.ObjectModel; |
| 7 | +using System.IdentityModel.Selectors; |
| 8 | +using System.IdentityModel.Tokens; |
| 9 | +using System.Linq; |
| 10 | +using System.ServiceModel.Description; |
| 11 | +using System.Xml; |
| 12 | +using Infrastructure.Common; |
| 13 | +using Microsoft.IdentityModel.Protocols.WsFed; |
| 14 | +using Microsoft.IdentityModel.Protocols.WsTrust; |
| 15 | +using Microsoft.IdentityModel.Tokens.Saml2; |
| 16 | +using Xunit; |
| 17 | + |
| 18 | +namespace System.ServiceModel.Federation.Tests |
| 19 | +{ |
| 20 | + public static class WSTrustChannelSecurityTokenProviderTest |
| 21 | + { |
| 22 | + [WcfFact] |
| 23 | + public static void EnsibilityTest() |
| 24 | + { |
| 25 | + string claimUri = "http://example.org/claims/simplecustomclaim"; |
| 26 | + string claimValue = "sample claim value"; |
| 27 | + var claims = new Claims("dialect", new List<ClaimType>() { new ClaimType() { Uri = claimUri, IsOptional = false, Value = claimValue } }); |
| 28 | + var issuerAddress = new EndpointAddress(new Uri("http://localhost/issuer.svc")); |
| 29 | + var targetAddress = new EndpointAddress(new Uri("http://localhost/target.svc")); |
| 30 | + var issuerBinding = new WSHttpBinding(SecurityMode.Transport); |
| 31 | + string eln1 = "Element1"; |
| 32 | + string eln2 = "Element2"; |
| 33 | + var additionalElements= new Collection<XmlElement>() { new XmlDocument().CreateElement(eln1), new XmlDocument().CreateElement(eln2) }; |
| 34 | + |
| 35 | + var tokenParams = new WSTrustTokenParameters |
| 36 | + { |
| 37 | + Claims= claims, |
| 38 | + IssuerAddress = issuerAddress, |
| 39 | + IssuerBinding = issuerBinding, |
| 40 | + KeyType = SecurityKeyType.SymmetricKey, |
| 41 | + TokenType = Saml2Constants.OasisWssSaml2TokenProfile11, |
| 42 | + MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11 |
| 43 | + }; |
| 44 | + |
| 45 | + foreach(XmlElement element in additionalElements) |
| 46 | + { |
| 47 | + tokenParams.AdditionalRequestParameters.Add(element); |
| 48 | + } |
| 49 | + |
| 50 | + var tokenRequirement = new System.IdentityModel.Selectors.SecurityTokenRequirement() |
| 51 | + { |
| 52 | + TokenType = "urn:oasis:names:tc:SAML:1.0:assertion" |
| 53 | + }; |
| 54 | + |
| 55 | + tokenRequirement.Properties["http://schemas.microsoft.com/ws/2006/05/servicemodel/securitytokenrequirement/IssuedSecurityTokenParameters"] = tokenParams; |
| 56 | + tokenRequirement.Properties["http://schemas.microsoft.com/ws/2006/05/servicemodel/securitytokenrequirement/TargetAddress"] = targetAddress; |
| 57 | + tokenRequirement.Properties["http://schemas.microsoft.com/ws/2006/05/servicemodel/securitytokenrequirement/SecurityAlgorithmSuite"] = System.ServiceModel.Security.SecurityAlgorithmSuite.Default; |
| 58 | + |
| 59 | + var derivedTokenProvider = new WSTrustChannelSecurityTokenProviderDerived(tokenRequirement); |
| 60 | + |
| 61 | + (derivedTokenProvider as ICommunicationObject).Open(); |
| 62 | + |
| 63 | + WsTrustRequest trustRequest = derivedTokenProvider.CreateWsTrustRequestHelper(); |
| 64 | + |
| 65 | + Assert.NotNull(trustRequest); |
| 66 | + Assert.NotNull(trustRequest.Claims); |
| 67 | + Assert.Equal(claims.Dialect, trustRequest.Claims.Dialect); |
| 68 | + ClaimType ctype = trustRequest.Claims.ClaimTypes.FirstOrDefault(); |
| 69 | + Assert.NotNull(ctype); |
| 70 | + Assert.Equal(claimUri, ctype.Uri); |
| 71 | + Assert.Equal(claimValue, ctype.Value); |
| 72 | + Assert.False(ctype.IsOptional); |
| 73 | + Assert.Equal(2, trustRequest.AdditionalXmlElements.Count); |
| 74 | + Assert.Equal(eln1, trustRequest.AdditionalXmlElements[0].Name); |
| 75 | + Assert.Equal(eln2, trustRequest.AdditionalXmlElements[1].Name); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public class WSTrustChannelSecurityTokenProviderDerived : WSTrustChannelSecurityTokenProvider |
| 80 | + { |
| 81 | + public WSTrustChannelSecurityTokenProviderDerived(SecurityTokenRequirement tokenRequirement) : base(tokenRequirement) |
| 82 | + { |
| 83 | + } |
| 84 | + |
| 85 | + public Microsoft.IdentityModel.Protocols.WsTrust.WsTrustRequest CreateWsTrustRequestHelper() |
| 86 | + { |
| 87 | + ClientCredentials = new ClientCredentials(); |
| 88 | + return base.CreateWsTrustRequest(); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments