using System.Security.Cryptography.X509Certificates; using dk.nsi.seal; using NUnit.Framework; using dk.nsi.seal.Model; using System; using dk.nsi.seal.Vault; using System.ServiceModel; using SealTest.NSTWsProvider; using System.Threading.Tasks; using dk.nsi.seal.dgwstypes; using static dk.nsi.seal.dgwstypes.SubjectIdentifierType; using static dk.nsi.seal.MessageHeaders.XmlMessageHeader; using static dk.nsi.seal.MessageHeaders.IdCardMessageHeader; namespace SealTest.AssertionTests { [TestFixture] public class NemIdAssertionTest { /** * In this example the user has a single authorization in the authorization register. The id card request is * made for that one specific authorization. */ [Test] public async Task UserWithOneSpecificAuthorization() { const string userCpr = "0102732379"; const string userGivenName = "Brian"; const string userSurName = "Rasmussen"; const string userEmail = "some@mail.dk"; const string userRole = "7170"; const string userAuthorizationCode = "XCBHH"; var idCard = await TestNemId2SealAssertion(Global.Moces3BrianRasmussen, userCpr, userGivenName, userSurName, userEmail, userRole, userAuthorizationCode); Assert.IsTrue(SealUtilities.CheckAssertionSignature(idCard.Xassertion)); Assert.IsNotNull(idCard.IdCardId, "No user information found"); } /** * In this example the user has a single authorization in the authorization register. The id card request is * made with only a limitations on the role/education code. This is only possible because the user has one * and only one authorization with the "Doctor" role. */ [Test] public async Task UserWithOneDoctorAuthorization() { const string userCpr = "0102732379"; const string userGivenName = "Brian"; const string userSurName = "Rasmussen"; const string userEmail = "some@mail.dk"; const string userRole = "7170"; const string userAuthorizationCode = null; var idCard = await TestNemId2SealAssertion(Global.Moces3BrianRasmussen, userCpr, userGivenName, userSurName, userEmail, userRole, userAuthorizationCode); Assert.IsTrue(SealUtilities.CheckAssertionSignature(idCard.Xassertion)); } /** * In this example the user has a single authorization in the authorization register. The id card request is * made without a limitations on the authorization that will be used. This is only possible because the user * has one and only one authorization. */ [Test] public async Task UserWithOneAuthorization() { const string userCpr = "0102732379"; const string userGivenName = "Brian"; const string userSurName = "Rasmussen"; const string userEmail = "some@mail.dk"; const string userRole = "IGNORED"; // Must not be an empty string or null, but all values that are not four digits are ignored by STS const string userAuthorizationCode = null; var idCard = await TestNemId2SealAssertion(Global.Moces3BrianRasmussen, userCpr, userGivenName, userSurName, userEmail, userRole, userAuthorizationCode); Assert.IsTrue(SealUtilities.CheckAssertionSignature(idCard.Xassertion)); } /** * In this example the user has two authorizations in the authorization register. The id card request is * made for one specifid authorization. If no limitations were made in the request, the call would fail * because STS will not choose one at random for us. */ [Test] public async Task UserWithSeveralAuthorization() { const string userCpr = "1611809960", userGivenName = "Anne", userSurName = "Bonde", userEmail = "some@mail.dk", ecucationCode = "7170", userAuthorizationCode = "09QL4"; var idCard = await TestNemId2SealAssertion(Global.Moces3AnneBonde, userCpr, userGivenName, userSurName, userEmail, ecucationCode, userAuthorizationCode); Assert.IsTrue(SealUtilities.CheckAssertionSignature(idCard.Xassertion)); } public static async Task TestNemId2SealAssertion(X509Certificate2 userCert, string userCpr, string userGivenName, string userSurName, string userEmail, string userRole, string userAuthorizationCode) { const string cvr = "33257872"; var signingCert = Global.TestFoces2024; var systemInfo = new SystemInfo(new CareProvider(medcomcprnumber, cvr, "Sundhedsdatastyrelsen"), "Sygdom.dk"); var userInfo = new UserInfo(userCpr, userGivenName, userSurName, userEmail, "", userRole, userAuthorizationCode); var userIdCard = new UserIdCard(Configuration.SosiDgwsVersion, AuthenticationLevel.VocesTrustedSystem, "N/A", systemInfo, userInfo, null, new CertificateInfo(userCert).ToString(), null, null); var nemIdAssertion = new OioSamlAssertionBuilder { Issuer = "https://saml.test-nemlog-in.dk", UserIdCard = userIdCard, UserCertificate = userCert, SigningVault = new InMemoryCredentialVault(signingCert), NotBefore = DateTime.Now.AddDays(-1), NotOnOrAfter = DateTime.Now.AddHours(8), AudienceRestriction = "https://sts.sosi.dk/", RecipientUrl = "https://staging.fmk-online.dk/fmk/saml/SAMLAssertionConsumer" } .Build(); var idCard = ExchangeNemLoginAssertionForSosiStsCard(signingCert, nemIdAssertion, userAuthorizationCode); await CallNts(idCard); return idCard; } private static Task CallNts(IdCard idc) { var client = new NtsWSProviderClient(new BasicHttpsBinding(), new EndpointAddress("https://test1-cnsp.ekstern-test.nspop.dk:8443/nts/service")); client.Endpoint.EndpointBehaviors.Add(new SealEndpointBehavior()); var dgwsHeader = new Header { SecurityLevel = 4, SecurityLevelSpecified = true, Linking = new Linking { MessageID = Guid.NewGuid().ToString("D") } }; using (new OperationContextScope(client.InnerChannel)) { // Adding seal-security and dgws-header soap header OperationContext.Current.OutgoingMessageHeaders.Add(IdCardHeader(idc)); OperationContext.Current.OutgoingMessageHeaders.Add(XmlHeader(dgwsHeader)); // Throws Exception if not succesful. return client.invokeAsync("test"); } } public static IdCard ExchangeNemLoginAssertionForSosiStsCard(X509Certificate2 clientCertificate, OioSamlAssertion nemidAssertion, string authorizationCode) => Saml2SosiStsClient.ExchangeAssertion(clientCertificate, new Uri(TestConstants.SecurityTokenServiceOioSaml2Sosi), nemidAssertion, authorizationCode); } }