Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cffa395
Basic Apple provider
martincostello Jun 6, 2019
5c3d7cd
Implement Apple provider
martincostello Jun 8, 2019
f78fa43
Enable Sign In with Apple
martincostello Jun 8, 2019
7ba0afb
Update tests
martincostello Jun 8, 2019
7a24782
Enable token lifetime validation
martincostello Jun 8, 2019
c0fc31c
Add null annotations
martincostello Jun 9, 2019
cd8ed33
Improve exception handling
martincostello Jun 9, 2019
9bc3817
Extend integration tests
martincostello Jun 9, 2019
404730e
Move expiry period to options
martincostello Jun 9, 2019
8b11fe7
Add ClientSecretExpiresAfter validation
martincostello Jun 9, 2019
c7b2f74
Add tests for options validation
martincostello Jun 9, 2019
05a8102
Add unit tests for client secret
martincostello Jun 9, 2019
9948d08
Make KeyId required
martincostello Jun 9, 2019
5e72f38
Fix test
martincostello Jun 9, 2019
2d4794a
Fix Linux and macOS secret generation
martincostello Jun 9, 2019
13012dd
Add password option for pfx files
martincostello Jun 9, 2019
b9f329a
Fix flaky test
martincostello Jun 9, 2019
210fbf9
Add UsePrivateKey() method
martincostello Jun 9, 2019
94f1737
Bump System.IdentityModel.Tokens.Jwt
martincostello Jun 9, 2019
67dc9a3
Set response_mode to form_post
martincostello Sep 8, 2019
eae3d43
Use latest C# version
martincostello Sep 8, 2019
4a93eb6
Retrieve user details after sign-in
martincostello Sep 8, 2019
74f607a
Update branding
martincostello Sep 15, 2019
dbbf2db
Access events via options
martincostello Sep 15, 2019
785a01a
Resolve logging TODO
martincostello Sep 15, 2019
eb5ccfd
Comment out Apple option
martincostello Sep 20, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add unit tests for client secret
Add unit tests for the generated client secret's format.
  • Loading branch information
martincostello committed Sep 8, 2019
commit 05a81029065e36ca7e2a154b54c63aa33ff7ef6d
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
* for more information concerning the license and the contributors participating to this project.
*/

using System;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Xunit;

namespace AspNet.Security.OAuth.Apple
{
public static class AppleClientSecretGeneratorTests
{
private static readonly byte[] TestPrivateKey = Convert.FromBase64String("MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgU208KCg/doqiSzsVF5sknVtYSgt8/3oiYGbvryIRrzSgCgYIKoZIzj0DAQehRANCAAQfrvDWizEnWAzB2Hx2r/NyvIBO6KGBDL7wkZoKnz4Sm4+1P1dhD9fVEhbsdoq9RKEf8dvzTOZMaC/iLqZFKSN6");

[Fact]
public static async Task GenerateAsync_Generates_Valid_Signed_Jwt()
{
// Arrange
var options = new AppleAuthenticationOptions()
{
ClientId = "my-client-id",
ClientSecretExpiresAfter = TimeSpan.FromMinutes(1),
KeyId = "my-key-id",
TeamId = "my-team-id",
PrivateKeyBytes = (keyId) => Task.FromResult(TestPrivateKey),
};

await GenerateTokenAsync(options, async (generator, context) =>
{
var utcNow = DateTimeOffset.UtcNow;

// Act
string token = await generator.GenerateAsync(context);

// Assert
token.ShouldNotBeNullOrWhiteSpace();
token.Count((c) => c == '.').ShouldBe(2); // Format: "{header}.{body}.{signature}"

// Act
var validator = new JwtSecurityTokenHandler();
var securityToken = validator.ReadJwtToken(token);

// Assert - See https://developer.apple.com/documentation/signinwithapplerestapi/generate_and_validate_tokens
securityToken.ShouldNotBeNull();

securityToken.Header.ShouldNotBeNull();
securityToken.Header.ShouldContainKeyAndValue("alg", "ES256");
securityToken.Header.ShouldContainKeyAndValue("kid", "my-key-id");

securityToken.Payload.ShouldNotBeNull();
securityToken.Payload.ShouldContainKey("exp");
securityToken.Payload.ShouldContainKey("iat");
securityToken.Payload.ShouldContainKeyAndValue("aud", "https://appleid.apple.com");
securityToken.Payload.ShouldContainKeyAndValue("iss", "my-team-id");
securityToken.Payload.ShouldContainKeyAndValue("sub", "my-client-id");

((long)securityToken.Payload.Iat.Value).ShouldBeGreaterThanOrEqualTo(utcNow.ToUnixTimeSeconds());
((long)securityToken.Payload.Exp.Value).ShouldBeGreaterThanOrEqualTo(utcNow.AddSeconds(60).ToUnixTimeSeconds());
((long)securityToken.Payload.Exp.Value).ShouldBeLessThanOrEqualTo(utcNow.AddSeconds(70).ToUnixTimeSeconds());
});
}

[Fact]
public static async Task GenerateAsync_Caches_Jwt_Until_Expired()
{
// Arrange
var options = new AppleAuthenticationOptions()
{
ClientId = "my-client-id",
KeyId = "my-key-id",
TeamId = "my-team-id",
PrivateKeyBytes = (keyId) => Task.FromResult(TestPrivateKey),
};

await GenerateTokenAsync(options, async (generator, context) =>
{
// Act
string token1 = await generator.GenerateAsync(context);
string token2 = await generator.GenerateAsync(context);

// Assert
token2.ShouldBe(token1);

// Act
await Task.Delay(TimeSpan.FromSeconds(3));
string token3 = await generator.GenerateAsync(context);

// Assert
token3.ShouldNotBe(token1);
});
}

private static async Task GenerateTokenAsync(
AppleAuthenticationOptions options,
Func<AppleClientSecretGenerator, AppleGenerateClientSecretContext, Task> actAndAssert)
{
// Arrange
var builder = new WebHostBuilder()
.Configure((app) => app.UseAuthentication())
.ConfigureServices((services) =>
{
services.AddAuthentication()
.AddApple();
});

using (var host = builder.Build())
{
var httpContext = new DefaultHttpContext();
var scheme = new AuthenticationScheme("Apple", "Apple", typeof(AppleAuthenticationHandler));

var context = new AppleGenerateClientSecretContext(httpContext, scheme, options);
var generator = host.Services.GetRequiredService<AppleClientSecretGenerator>();

await actAndAssert(generator, context);
}
}
}
}