-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#404 - add a sample of how the phone auth/sign work
- Loading branch information
1 parent
23f6e7e
commit 33b2225
Showing
8 changed files
with
347 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
using ActiveLogin.Authentication.BankId.Api; | ||
using ActiveLogin.Authentication.BankId.Api.Models; | ||
using ActiveLogin.Identity.Swedish; | ||
|
||
using Microsoft.Extensions.Hosting; | ||
|
||
using Phone.ConsoleSample; | ||
|
||
namespace Phone.ConsoleSample; | ||
|
||
internal sealed class BankIdDemoHostedService : IHostedService | ||
{ | ||
private readonly IBankIdAppApiClient _bankIdApiClient; | ||
private readonly IHostApplicationLifetime _appLifetime; | ||
|
||
public BankIdDemoHostedService( | ||
IBankIdAppApiClient bankIdApiClient, | ||
IHostApplicationLifetime appLifetime | ||
) | ||
{ | ||
_bankIdApiClient = bankIdApiClient; | ||
_appLifetime = appLifetime; | ||
} | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
await RunBankIdFlowAsync(); | ||
_appLifetime.StopApplication(); | ||
|
||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public async Task RunBankIdFlowAsync() | ||
{ | ||
var personalIdentityNumber = GetPersonalIdentityNumber(); | ||
var sessionType = GetSessionType(); | ||
var callInitiatior = GetCallInitiator(); | ||
|
||
var orderRef = await InitiateAsync(personalIdentityNumber, sessionType, callInitiatior); | ||
await CollectAsync(orderRef); | ||
} | ||
|
||
private string GetPersonalIdentityNumber() | ||
{ | ||
ConsoleHelper.WriteHeader("Enter your personal identity number (YYYYMMDDXXXX):"); | ||
while (true) | ||
{ | ||
var personalIdentityNumber = Console.ReadLine(); | ||
|
||
if (!string.IsNullOrEmpty(personalIdentityNumber)) | ||
{ | ||
var success = PersonalIdentityNumber.TryParse(personalIdentityNumber, out var parsedPersonalIdentityNumber); | ||
if (success) | ||
{ | ||
Console.WriteLine(); | ||
return parsedPersonalIdentityNumber.To12DigitString(); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Invalid personal identity number. Please try again."); | ||
} | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Input cannot be empty. Please try again."); | ||
} | ||
} | ||
} | ||
|
||
private SessionType GetSessionType() | ||
{ | ||
ConsoleHelper.WriteHeader("Do you want to test an auth or sign session?"); | ||
var sessionType = ConsoleHelper.DisplayMenuAndGetSelectedKey(new List<(string Key, string DisplayName)> | ||
{ | ||
(SessionType.Auth.ToString(), "Auth"), | ||
(SessionType.Sign.ToString(), "Sign") | ||
}); | ||
return Enum.Parse<SessionType>(sessionType); | ||
} | ||
|
||
private CallInitiator GetCallInitiator() | ||
{ | ||
ConsoleHelper.WriteHeader("Who is the initiator of the session?"); | ||
var callInitiatior = ConsoleHelper.DisplayMenuAndGetSelectedKey(new List<(string Key, string DisplayName)> | ||
{ | ||
(CallInitiator.RP.ToString(), $"{CallInitiator.RP} - user called the RP"), | ||
(CallInitiator.User.ToString(), $"{CallInitiator.User} - RP called the user") | ||
}); | ||
return Enum.Parse<CallInitiator>(callInitiatior); | ||
} | ||
|
||
private async Task<string> InitiateAsync(string personalIdentityNumber, SessionType sessionType, CallInitiator callInitiator) | ||
{ | ||
ConsoleHelper.WriteHeader($"Initiates a {sessionType} session"); | ||
var orderRef = ""; | ||
if (sessionType == SessionType.Auth) | ||
{ | ||
var response = await _bankIdApiClient.PhoneAuthAsync(new PhoneAuthRequest( | ||
personalIdentityNumber: personalIdentityNumber, | ||
callInitiator: callInitiator, | ||
requirement: null, | ||
userVisibleData: null, | ||
userNonVisibleData: null, | ||
userVisibleDataFormat: null | ||
)); | ||
orderRef = response.OrderRef; | ||
} | ||
else | ||
{ | ||
var response = await _bankIdApiClient.PhoneSignAsync(new PhoneSignRequest( | ||
personalIdentityNumber: personalIdentityNumber, | ||
callInitiator: callInitiator, | ||
requirement: null, | ||
userVisibleData: "Hello, this is just a sample", | ||
userNonVisibleData: null, | ||
userVisibleDataFormat: null | ||
)); | ||
orderRef = response.OrderRef; | ||
} | ||
Console.WriteLine($"Successfully initiated a session with orderRef: {orderRef}"); | ||
Console.WriteLine(); | ||
return orderRef; | ||
} | ||
|
||
private async Task CollectAsync(string orderRef) | ||
{ | ||
ConsoleHelper.WriteHeader($"Collecte status for orderRef: {orderRef}"); | ||
while (true) | ||
{ | ||
var collectResponse = await _bankIdApiClient.CollectAsync(new CollectRequest(orderRef)); | ||
var status = collectResponse.GetCollectStatus(); | ||
if (status == CollectStatus.Pending) | ||
{ | ||
Console.WriteLine($"Pending. HintCode: {collectResponse.HintCode}"); | ||
await Task.Delay(2000); | ||
} | ||
else if (status == CollectStatus.Complete) | ||
{ | ||
Console.WriteLine(""); | ||
ConsoleHelper.WriteHeader("Collect completed"); | ||
Console.WriteLine($"Name: {collectResponse.CompletionData!.User.Name}"); | ||
Console.WriteLine($"Ip-Adress: {collectResponse.CompletionData!.Device.IpAddress}"); | ||
break; | ||
} | ||
else | ||
{ | ||
Console.WriteLine(""); | ||
ConsoleHelper.WriteHeader($"Failed. HintCode: {collectResponse.HintCode}"); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Phone.ConsoleSample; | ||
public static class ConsoleHelper | ||
{ | ||
public static void WriteHeader(string text) | ||
{ | ||
Console.ForegroundColor = ConsoleColor.Yellow; | ||
Console.WriteLine(text); | ||
Console.ResetColor(); | ||
} | ||
|
||
public static string DisplayMenuAndGetSelectedKey(List<(string Key, string DisplayName)> menuItems) | ||
{ | ||
int currentIndex = 0; | ||
ConsoleKeyInfo keyInfo; | ||
int menuStartRow = Console.CursorTop; | ||
|
||
do | ||
{ | ||
for (int i = 0; i < menuItems.Count; i++) | ||
{ | ||
Console.SetCursorPosition(0, menuStartRow + i); | ||
|
||
if (i == currentIndex) | ||
{ | ||
Console.BackgroundColor = ConsoleColor.Gray; | ||
Console.ForegroundColor = ConsoleColor.Black; | ||
} | ||
|
||
Console.WriteLine("* " + menuItems[i].DisplayName.PadRight(Console.WindowWidth - 1)); | ||
Console.ResetColor(); | ||
} | ||
|
||
keyInfo = Console.ReadKey(true); | ||
|
||
if (keyInfo.Key == ConsoleKey.UpArrow) | ||
{ | ||
currentIndex = (currentIndex - 1 + menuItems.Count) % menuItems.Count; | ||
} | ||
else if (keyInfo.Key == ConsoleKey.DownArrow) | ||
{ | ||
currentIndex = (currentIndex + 1) % menuItems.Count; | ||
} | ||
|
||
} while (keyInfo.Key != ConsoleKey.Enter); | ||
|
||
return menuItems[currentIndex].Key; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<LangVersion>latest</LangVersion> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
|
||
<Product>Active Login</Product> | ||
<Description>ASP.NET sample for Active Login.</Description> | ||
<Authors>Active Solution</Authors> | ||
<Company>Active Solution</Company> | ||
<Copyright>Copyright © 2018-2024 Active Solution</Copyright> | ||
|
||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
|
||
<OutputType>Exe</OutputType> | ||
|
||
<UserSecretsId>b910e9a7-c8bc-4350-8de2-e5b4c57753ed</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\ActiveLogin.Authentication.BankId.Api\ActiveLogin.Authentication.BankId.Api.csproj" /> | ||
<ProjectReference Include="..\..\src\ActiveLogin.Authentication.BankId.AzureKeyVault\ActiveLogin.Authentication.BankId.AzureKeyVault.csproj" /> | ||
<ProjectReference Include="..\..\src\ActiveLogin.Authentication.BankId.Core\ActiveLogin.Authentication.BankId.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using ActiveLogin.Authentication.BankId.Core; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using ActiveLogin.Authentication.BankId.AzureKeyVault; | ||
using Phone.ConsoleSample; | ||
// | ||
// DISCLAIMER - DO NOT USE FOR REAL | ||
// | ||
// This is samples to show how the BankID phone flow works. | ||
// You can't use BankID in this way in an application for real | ||
// as the client certificates would be exposed. | ||
// | ||
// Please see this as technical demo of how the flow works, | ||
// not something to use. | ||
// | ||
|
||
using var host = Host.CreateDefaultBuilder(args) | ||
.ConfigureAppConfiguration(config => | ||
{ | ||
config.AddUserSecrets(typeof(Program).Assembly); | ||
}) | ||
.ConfigureLogging(context => | ||
{ | ||
context.ClearProviders(); | ||
}) | ||
.ConfigureServices((context, services) => | ||
{ | ||
var configuration = context.Configuration; | ||
services.AddBankId(bankId => | ||
{ | ||
if (configuration.GetValue("ActiveLogin:BankId:UseSimulatedEnvironment", false)) | ||
{ | ||
bankId.UseSimulatedEnvironment(); | ||
} | ||
else if (configuration.GetValue("ActiveLogin:BankId:UseTestEnvironment", false)) | ||
{ | ||
bankId.UseTestEnvironment(); | ||
} | ||
else | ||
{ | ||
bankId.UseProductionEnvironment(); | ||
bankId.UseClientCertificateFromAzureKeyVault(configuration.GetSection("ActiveLogin:BankId:ClientCertificate")); | ||
} | ||
}); | ||
|
||
services.AddHostedService<BankIdDemoHostedService>(); | ||
}) | ||
.Build(); | ||
|
||
await host.RunAsync(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Phone.ConsoleSample; | ||
public enum SessionType | ||
{ | ||
Auth, | ||
Sign | ||
} |
Oops, something went wrong.