11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT License.
33
4+ using System . Security . Cryptography ;
5+ using System . Security . Cryptography . X509Certificates ;
6+
47using Azure . ApiManagement . PolicyToolkit . Authoring ;
8+ using Azure . ApiManagement . PolicyToolkit . Authoring . Expressions ;
59using Azure . ApiManagement . PolicyToolkit . Testing ;
610using Azure . ApiManagement . PolicyToolkit . Testing . Document ;
711
@@ -10,18 +14,41 @@ namespace Test.Emulator.Emulator.Policies;
1014[ TestClass ]
1115public class AuthenticationCertificateTests
1216{
13- class SimpleAuthenticationCertificate : IDocument
17+ class ByIdCertificate : IDocument
1418 {
1519 public void Inbound ( IInboundContext context )
1620 {
1721 context . AuthenticationCertificate ( new CertificateAuthenticationConfig { CertificateId = "abcdefgh" } ) ;
1822 }
1923 }
2024
25+ class ByThumbprintCertificate : IDocument
26+ {
27+ public void Inbound ( IInboundContext context )
28+ {
29+ context . AuthenticationCertificate ( new CertificateAuthenticationConfig { Thumbprint = "abcdefgh" } ) ;
30+ }
31+ }
32+
33+ class ByBodyCertificate : IDocument
34+ {
35+ public void Inbound ( IInboundContext context )
36+ {
37+ context . AuthenticationCertificate (
38+ new CertificateAuthenticationConfig
39+ {
40+ Body = GetCertBody ( context . ExpressionContext ) , Password = "testPass"
41+ } ) ;
42+ }
43+
44+ public byte [ ] GetCertBody ( IExpressionContext context ) =>
45+ context . Deployment . Certificates [ "someKey" ] . Export ( X509ContentType . Pfx , "testPass" ) ;
46+ }
47+
2148 [ TestMethod ]
2249 public void AuthenticationCertificate_Callback ( )
2350 {
24- var test = new SimpleAuthenticationCertificate ( ) . AsTestDocument ( ) ;
51+ var test = new ByIdCertificate ( ) . AsTestDocument ( ) ;
2552 var executedCallback = false ;
2653 test . SetupInbound ( ) . AuthenticationCertificate ( ) . WithCallback ( ( _ , _ ) =>
2754 {
@@ -32,4 +59,75 @@ public void AuthenticationCertificate_Callback()
3259
3360 executedCallback . Should ( ) . BeTrue ( ) ;
3461 }
62+
63+ [ TestMethod ]
64+ public void AuthenticationCertificate_ReturnCertificate ( )
65+ {
66+ var certificate = CreateTestCertificate ( ) ;
67+ var test = new ByIdCertificate ( ) . AsTestDocument ( ) ;
68+ test . SetupInbound ( ) . AuthenticationCertificate ( ) . WithCertificate ( certificate ) ;
69+
70+ test . RunInbound ( ) ;
71+
72+ test . Context . Request . Certificate . Should ( ) . Be ( certificate ) ;
73+ }
74+
75+ [ TestMethod ]
76+ public void AuthenticationCertificate_SetupCertificateStore_WithCertificateByThumbprint ( )
77+ {
78+ var certificate = CreateTestCertificate ( ) ;
79+ var test = new ByThumbprintCertificate ( ) . AsTestDocument ( ) ;
80+ test . SetupCertificateStore ( ) . WithCertificateByThumbprint ( "abcdefgh" , certificate ) ;
81+
82+ test . RunInbound ( ) ;
83+
84+ test . Context . Request . Certificate . Should ( ) . Be ( certificate ) ;
85+ }
86+
87+ [ TestMethod ]
88+ public void AuthenticationCertificate_SetupCertificateStore_WithCertificateById ( )
89+ {
90+ var certificate = CreateTestCertificate ( ) ;
91+ var test = new ByIdCertificate ( ) . AsTestDocument ( ) ;
92+ test . SetupCertificateStore ( ) . WithCertificateById ( "abcdefgh" , certificate ) ;
93+
94+ test . RunInbound ( ) ;
95+
96+ test . Context . Request . Certificate . Should ( ) . Be ( certificate ) ;
97+ }
98+
99+ [ TestMethod ]
100+ public void AuthenticationCertificate_Body ( )
101+ {
102+ var certificate = CreateTestCertificate ( ) ;
103+ var test = new ByBodyCertificate ( ) . AsTestDocument ( ) ;
104+ test . Context . Deployment . Certificates . Add ( "someKey" , certificate ) ;
105+
106+ test . RunInbound ( ) ;
107+
108+ test . Context . Request . Certificate . Should ( ) . Be ( certificate ) ;
109+ }
110+
111+ public X509Certificate2 CreateTestCertificate ( )
112+ {
113+ using RSA rsa = RSA . Create ( 2048 ) ;
114+ var request = new CertificateRequest (
115+ "CN=MyCertificate" ,
116+ rsa ,
117+ HashAlgorithmName . SHA256 ,
118+ RSASignaturePadding . Pkcs1 ) ;
119+
120+ // Add extensions
121+ request . CertificateExtensions . Add (
122+ new X509BasicConstraintsExtension ( false , false , 0 , false ) ) ;
123+ request . CertificateExtensions . Add (
124+ new X509KeyUsageExtension ( X509KeyUsageFlags . DigitalSignature , false ) ) ;
125+ request . CertificateExtensions . Add (
126+ new X509SubjectKeyIdentifierExtension ( request . PublicKey , false ) ) ;
127+
128+ var certificate = request . CreateSelfSigned (
129+ DateTimeOffset . Now ,
130+ DateTimeOffset . Now . AddYears ( 1 ) ) ;
131+ return certificate ;
132+ }
35133}
0 commit comments