Skip to content

Commit

Permalink
add tests and fix warnings (#2140)
Browse files Browse the repository at this point in the history
  • Loading branch information
jennyf19 authored Mar 23, 2023
1 parent e9bcf43 commit 307ef45
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#if !NETCOREAPP3_1_OR_GREATER
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#pragma warning disable IDE0073 // The file header does not match the required text
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Runtime.CompilerServices
#pragma warning restore IDE0073 // The file header does not match the required text
{
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
internal sealed class CallerArgumentExpressionAttribute : Attribute
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#pragma warning disable IDE0073 // The file header does not match the required text
// Licensed to the .NET Foundation under one or more agreements.
#pragma warning restore IDE0073 // The file header does not match the required text
// The .NET Foundation licenses this file to you under the MIT license.
#if !NETCOREAPP3_1_OR_GREATER
namespace System.Diagnostics.CodeAnalysis
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// © Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
Expand Down
5 changes: 4 additions & 1 deletion src/Microsoft.Identity.Web.TokenAcquisition/MsAuth10AtPop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ namespace Microsoft.Identity.Web
{
internal static class MsAuth10AtPop
{
public static AcquireTokenForClientParameterBuilder WithAtPop(
internal static AcquireTokenForClientParameterBuilder WithAtPop(
this AcquireTokenForClientParameterBuilder builder,
X509Certificate2 clientCertificate,
string popPublicKey,
string jwkClaim,
string clientId)
{
_ = Throws.IfNull(popPublicKey);
_ = Throws.IfNull(jwkClaim);

builder.WithProofOfPosessionKeyId(popPublicKey);
builder.OnBeforeTokenRequest((data) =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Licensed under the MIT License.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ public Task<AuthenticationResult> GetAuthenticationResultForAppAsync(
{
builder.WithAtPop(
application.AppConfig.ClientCredentialCertificate,
tokenAcquisitionOptions.PopPublicKey,
tokenAcquisitionOptions.JwkClaim,
tokenAcquisitionOptions.PopPublicKey!,
tokenAcquisitionOptions.JwkClaim!,
application.AppConfig.ClientId);
}
}
Expand Down
68 changes: 68 additions & 0 deletions tests/Microsoft.Identity.Web.Test/MsAuth10AtPopTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Identity.Client;
using Microsoft.Identity.Web.Test.Common;
using Xunit;

namespace Microsoft.Identity.Web.Test
{
public class MsAuth10AtPopTests
{
[Fact]
public void MsAuth10AtPop_WithAtPop_ShouldPopulateBuilderWithProofOfPosessionKeyIdAndOnBeforeTokenRequestTest()
{
// Arrange
var builder = ConfidentialClientApplicationBuilder.Create(TestConstants.ClientId);
builder.WithExperimentalFeatures();
builder.WithClientSecret(TestConstants.ClientSecret);
var app = builder.Build();
var clientCertificate = new X509Certificate2(new byte[0]);
var popPublicKey = "pop_key";
var jwkClaim = "jwk_claim";
var clientId = "client_id";

// Act
var result = MsAuth10AtPop.WithAtPop(app.AcquireTokenForClient(new[] { TestConstants.Scopes }), clientCertificate, popPublicKey, jwkClaim, clientId);

// Assert
Assert.NotNull(result);
}

[Fact]
public void MsAuth10AtPop_ThrowsWithNullPopKeyTest()
{
// Arrange
IConfidentialClientApplication app = CreateBuilder();
var clientCertificate = new X509Certificate2(new byte[0]);
var jwkClaim = "jwk_claim";
var clientId = "client_id";

// Act & Assert
Assert.Throws<ArgumentNullException>(() => MsAuth10AtPop.WithAtPop(app.AcquireTokenForClient(new[] { TestConstants.Scopes }), clientCertificate, string.Empty, jwkClaim, clientId));
}

[Fact]
public void MsAuth10AtPop_ThrowsWithNullJwkClaimTest()
{
// Arrange
IConfidentialClientApplication app = CreateBuilder();
var clientCertificate = new X509Certificate2(new byte[0]);
var popPublicKey = "pop_key";
var clientId = "client_id";

// Act & Assert
Assert.Throws<ArgumentNullException>(() => MsAuth10AtPop.WithAtPop(app.AcquireTokenForClient(new[] { TestConstants.Scopes }), clientCertificate, popPublicKey, null, clientId));
}

private static IConfidentialClientApplication CreateBuilder()
{
var builder = ConfidentialClientApplicationBuilder.Create(TestConstants.ClientId);
builder.WithExperimentalFeatures();
builder.WithClientSecret(TestConstants.ClientSecret);
return builder.Build();
}
}
}

0 comments on commit 307ef45

Please sign in to comment.