Skip to content

Commit

Permalink
Add default OAuth2 ClientAssistantFactory implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kenelin committed Aug 26, 2021
1 parent 012c8d8 commit 3b3a33d
Show file tree
Hide file tree
Showing 4 changed files with 620 additions and 115 deletions.
60 changes: 57 additions & 3 deletions source/Htc.Vita.Core.Tests/OAuth2Test.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Htc.Vita.Core.Auth;
using Htc.Vita.Core.Net;
using Htc.Vita.Core.Util;
using Xunit;

namespace Htc.Vita.Core.Tests
Expand Down Expand Up @@ -212,7 +218,37 @@ public static void AuthorizationCodeReceiver_1_Receive()
Assert.NotNull(receiver);
var receiveResult = receiver.Receive();
var receiveStatus = receiveResult.Status;
Assert.Equal(OAuth2.AuthorizationCodeReceiver.ReceiveStatus.NotImplemented, receiveStatus);
Assert.Equal(OAuth2.AuthorizationCodeReceiver.ReceiveStatus.UnsupportedReceiver, receiveStatus);
}
}

[Fact]
public static void AuthorizationCodeReceiver_1_Receive_withRedirectUriAndCode()
{
var clientAssistantFactory = OAuth2.ClientAssistantFactory.GetInstance();
var unusedPort = LocalPortManager.GetRandomUnusedPort();
var redirectUriString = $"http://localhost:{unusedPort}/";
const string authorizationCode = "testAuthorizationCode";
var options = new Dictionary<string, object>
{
{ OAuth2.AuthorizationCodeReceiver.OptionRedirectUri, redirectUriString }
};
Task.Run(() =>
{
using (WebRequestFactory.GetInstance()
.GetHttpWebRequest(new Uri($"{redirectUriString}?{OAuth2.Key.Code.GetDescription()}={authorizationCode}"))
.GetResponse())
{
// Skip
}
});
using (var authorizationCodeReceiver = clientAssistantFactory.GetAuthorizationCodeReceiver(options, CancellationToken.None))
{
Assert.NotNull(authorizationCodeReceiver);
var receiveResult = authorizationCodeReceiver.Receive();
var receiveStatus = receiveResult.Status;
Assert.Equal(OAuth2.AuthorizationCodeReceiver.ReceiveStatus.Ok, receiveStatus);
Assert.Equal(authorizationCode, receiveResult.Code);
}
}

Expand All @@ -225,7 +261,7 @@ public static void AuthorizationCodeUserAgent_0_PublicConstants()
}

[Fact]
public static void AuthorizationCodeUserAgent_0_Launch()
public static void AuthorizationCodeUserAgent_1_Launch()
{
var clientAssistantFactory = OAuth2.ClientAssistantFactory.GetInstance();
Assert.NotNull(clientAssistantFactory);
Expand All @@ -234,7 +270,25 @@ public static void AuthorizationCodeUserAgent_0_Launch()
Assert.NotNull(userAgent);
var launchResult = userAgent.Launch();
var launchStatus = launchResult.Status;
Assert.Equal(OAuth2.AuthorizationCodeUserAgent.LaunchStatus.NotImplemented, launchStatus);
Assert.Equal(OAuth2.AuthorizationCodeUserAgent.LaunchStatus.InvalidAuthorizationUri, launchStatus);
}
}

[Fact]
public static void AuthorizationCodeUserAgent_1_Launch_withAuthorizationUrl()
{
var clientAssistantFactory = OAuth2.ClientAssistantFactory.GetInstance();
const string authorizationUrlString = "https://www.microsoft.com/";
var options = new Dictionary<string, object>
{
{ OAuth2.AuthorizationCodeUserAgent.OptionAuthorizationUrl, authorizationUrlString }
};
using (var authorizationCodeUserAgent = clientAssistantFactory.GetAuthorizationCodeUserAgent(options, CancellationToken.None))
{
Assert.NotNull(authorizationCodeUserAgent);
var launchResult = authorizationCodeUserAgent.Launch();
var launchStatus = launchResult.Status;
Assert.Equal(OAuth2.AuthorizationCodeUserAgent.LaunchStatus.Ok, launchStatus);
}
}
}
Expand Down
Loading

0 comments on commit 3b3a33d

Please sign in to comment.