Skip to content

Commit

Permalink
Merge pull request #60 from drasticactions/internal-password
Browse files Browse the repository at this point in the history
Make CreateSessionAsync internal
  • Loading branch information
drasticactions authored Sep 27, 2024
2 parents 85d0581 + 1585037 commit ca8207d
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 29 deletions.
5 changes: 4 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
<PackageVersion Include="Microsoft.Testing.Extensions.CodeCoverage" Version="17.12.4" />
<PackageVersion Include="GitVersion.MSBuild" Version="6.0.2" />
<PackageVersion Include="IdentityModel" Version="7.0.0" />
<PackageVersion Include="ConsoleAppFramework" Version="5.2.3" />
<PackageVersion Include="ConsoleAppFramework" Version="5.2.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
</ItemGroup>
<ItemGroup Condition="$(UseMaui) == true">
<PackageVersion Include="Microsoft.Maui.Controls" Version="8.0.21" />
Expand Down
23 changes: 23 additions & 0 deletions samples/PasswordAuth/PasswordAuth.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ConsoleAppFramework">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\FishyFlip\FishyFlip.csproj" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions samples/PasswordAuth/PasswordAuth.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PasswordAuth", "PasswordAuth.csproj", "{CA58096E-487C-4C4B-B72F-FC5A48AD6A4B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FishyFlip", "..\..\src\FishyFlip\FishyFlip.csproj", "{7E7D4B52-3C34-430A-AA04-E3E77852F052}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CA58096E-487C-4C4B-B72F-FC5A48AD6A4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CA58096E-487C-4C4B-B72F-FC5A48AD6A4B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CA58096E-487C-4C4B-B72F-FC5A48AD6A4B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CA58096E-487C-4C4B-B72F-FC5A48AD6A4B}.Release|Any CPU.Build.0 = Release|Any CPU
{7E7D4B52-3C34-430A-AA04-E3E77852F052}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7E7D4B52-3C34-430A-AA04-E3E77852F052}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7E7D4B52-3C34-430A-AA04-E3E77852F052}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7E7D4B52-3C34-430A-AA04-E3E77852F052}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
43 changes: 43 additions & 0 deletions samples/PasswordAuth/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using ConsoleAppFramework;
using FishyFlip;
using Microsoft.Extensions.Logging.Debug;

var app = ConsoleApp.Create();
app.Add<AppCommands>();
app.Run(args);

/// <summary>
/// App Commands.
/// </summary>
#pragma warning disable SA1649 // File name should match first type name
public class AppCommands
#pragma warning restore SA1649 // File name should match first type name
{
/// <summary>
/// Authenticate with the ATProtocol using a password.
/// </summary>
/// <param name="identifier">The user's identifier.</param>
/// <param name="password">The user's password.</param>
/// <param name="cancellationToken">Cancellation Token.</param>
/// <returns>Task.</returns>
[Command("authenticate")]
public async Task AuthenticateAsync([Argument] string identifier, [Argument] string password, CancellationToken cancellationToken = default)
{
var protocol = new ATProtocolBuilder()
.WithLogger(new DebugLoggerProvider().CreateLogger("FishyFlip"))
.Build();

var session = await protocol.AuthenticateWithPasswordAsync(identifier, password, cancellationToken);
if (session is null)
{
Console.WriteLine("Failed to authenticate.");
return;
}

Console.WriteLine("Authenticated.");
Console.WriteLine($"Session Did: {session.Did}");
Console.WriteLine($"Session Email: {session.Email}");
Console.WriteLine($"Session Handle: {session.Handle}");
Console.WriteLine($"Session Token: {session.AccessJwt}");
}
}
6 changes: 0 additions & 6 deletions src/FishyFlip/ISessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,4 @@ internal interface ISessionManager : IDisposable
/// <param name="cancellationToken">Cancellation Token.</param>
/// <returns>Task.</returns>
public Task RefreshSessionAsync(CancellationToken cancellationToken = default);

/// <summary>
/// Set the current session.
/// </summary>
/// <param name="session">Session.</param>
public void SetSession(Session session);
}
21 changes: 12 additions & 9 deletions src/FishyFlip/OAuth2SessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,18 @@ public Task RefreshSessionAsync(CancellationToken cancellationToken = default)
=> this.RefreshTokenAsync(cancellationToken);

/// <inheritdoc/>
public void SetSession(Session session)
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
this.Dispose(disposing: true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Set the session.
/// </summary>
/// <param name="session">Session.</param>
internal void SetSession(Session session)
{
if (this.protocol.Options.UseServiceEndpointUponLogin)
{
Expand Down Expand Up @@ -218,14 +229,6 @@ public void SetSession(Session session)
this.session = session;
}

/// <inheritdoc/>
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
this.Dispose(disposing: true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Refresh Token.
/// </summary>
Expand Down
16 changes: 10 additions & 6 deletions src/FishyFlip/PasswordSessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,14 @@ public Task RefreshSessionAsync(CancellationToken cancellationToken = default)
/// <param name="password">The password of the user.</param>
/// <param name="cancellationToken">Optional. A CancellationToken that can be used to cancel the operation.</param>
/// <returns>A Task that represents the asynchronous operation. The task result contains a Result object with the session details, or null if the session could not be created.</returns>
public async Task<Session?> CreateSessionAsync(string identifier, string password, CancellationToken cancellationToken = default)
internal async Task<Session?> CreateSessionAsync(string identifier, string password, CancellationToken cancellationToken = default)
{
var session = (await this.protocol.Server.CreateSessionAsync(identifier, password, cancellationToken)).HandleResult();
if (session is not null)
var sessionResult = await this.protocol.Server.CreateSessionAsync(identifier, password, cancellationToken);
Session? resultSession = null;
sessionResult.Switch(
session =>
{
resultSession = session;
if (this.protocol.Options.UseServiceEndpointUponLogin)
{
var logger = this.protocol.Options.Logger;
Expand All @@ -130,16 +133,17 @@ public Task RefreshSessionAsync(CancellationToken cancellationToken = default)
}
this.SetSession(session);
}
},
e => this.logger?.LogError(e.ToString(), e));

return session;
return resultSession;
}

/// <summary>
/// Sets the given session.
/// </summary>
/// <param name="session"><see cref="Session"/>.</param>
public void SetSession(Session session)
internal void SetSession(Session session)
{
this.session = session;
this.UpdateBearerToken(session);
Expand Down
17 changes: 10 additions & 7 deletions src/FishyFlip/UnauthenticatedSessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ public Task RefreshSessionAsync(CancellationToken cancellationToken = default)
return Task.CompletedTask;
}

/// <inheritdoc/>
public void SetSession(Session session)
{
this.logger?.LogWarning("Unauthenticated session manager, session not set.");
this.SessionUpdated?.Invoke(this, new SessionUpdatedEventArgs(new AuthSession(session), null));
}

/// <inheritdoc/>
public void Dispose()
{
Expand All @@ -60,6 +53,16 @@ public void Dispose()
GC.SuppressFinalize(this);
}

/// <summary>
/// Set the session.
/// </summary>
/// <param name="session">Session.</param>
internal void SetSession(Session session)
{
this.logger?.LogWarning("Unauthenticated session manager, session not set.");
this.SessionUpdated?.Invoke(this, new SessionUpdatedEventArgs(new AuthSession(session), null));
}

/// <summary>
/// Dispose.
/// </summary>
Expand Down

0 comments on commit ca8207d

Please sign in to comment.