Skip to content

Commit d3ac880

Browse files
committed
Add -Device parameter to Connect-Keeper to prevent config file load
1 parent 1c0a8f2 commit d3ac880

4 files changed

Lines changed: 74 additions & 2 deletions

File tree

KeeperSdk/auth/AuthCommon.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ public interface IAuth : IAuthEndpoint
181181
/// </summary>
182182
bool AlternatePassword { get; set; }
183183

184+
/// <summary>
185+
/// Uses a configured device token without requiring a stored device private key.
186+
/// </summary>
187+
public bool NoNewDevice { get; set; }
188+
184189
/// <summary>
185190
/// Login to Keeper account with email.
186191
/// </summary>

KeeperSdk/auth/AuthSync.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public AuthSync(IConfigurationStorage storage, IKeeperEndpoint endpoint = null)
6161
/// <inheritdoc/>>
6262
public bool AlternatePassword { get; set; }
6363

64+
/// <inheritdoc/>>
65+
public bool NoNewDevice { get; set; }
66+
6467
/// <inheritdoc cref="IAuth.Username" />>
6568
public new string Username
6669
{
@@ -216,6 +219,10 @@ private async Task<AuthStep> StartLoginSync(StartLoginRequest request)
216219
switch (response.EncryptedDataKeyType)
217220
{
218221
case EncryptedDataKeyType.ByDevicePublicKey:
222+
if (_loginContext.DeviceKey == null)
223+
{
224+
throw new KeeperInvalidDeviceToken("device private key is required for this login flow");
225+
}
219226
context.DataKey = CryptoUtils.DecryptEc(encryptedDataKey, _loginContext.DeviceKey);
220227
break;
221228
}

KeeperSdk/auth/LoginV3Extensions.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ public static async Task EnsureDeviceTokenIsRegistered(this IAuth auth, LoginCon
8080
}
8181

8282
var configuration = auth.Storage.Get();
83+
if (auth.NoNewDevice)
84+
{
85+
var token = configuration.Users.Get(auth.Username)?.LastDevice?.DeviceToken
86+
?? configuration.Devices.List.FirstOrDefault()?.DeviceToken;
87+
auth.DeviceToken = token.Base64UrlDecode();
88+
return;
89+
}
90+
8391
IDeviceConfiguration deviceConf = null;
8492
if (auth.DeviceToken != null)
8593
{
@@ -355,7 +363,7 @@ internal static async Task<T> StartLogin<T>(
355363
catch (Exception e)
356364
{
357365
auth.SetPushNotifications(null);
358-
if (attempt < 3 && e is KeeperInvalidDeviceToken)
366+
if (attempt < 3 && e is KeeperInvalidDeviceToken && !auth.NoNewDevice)
359367
{
360368
var configuration = auth.Storage.Get();
361369
configuration.Devices.Delete(auth.DeviceToken.Base64UrlEncode());

PowerCommander/AuthCommands.ps1

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,42 @@ function executeStepAction ([KeeperSecurity.Authentication.IAuthentication] $aut
463463
}
464464
}
465465

466+
function getConfigurationForDevice {
467+
param(
468+
[Parameter(Mandatory=$true)] [string] $Device,
469+
[Parameter()] [string] $Username,
470+
[Parameter()] [string] $Server
471+
)
472+
473+
$deviceToken = $Device.Trim()
474+
if (-not $deviceToken) {
475+
Write-Error "Device parameter requires a device_token value." -ErrorAction Stop
476+
}
477+
478+
$configuration = New-Object KeeperSecurity.Configuration.KeeperConfiguration
479+
$configuration.LastLogin = $Username
480+
$configuration.LastServer = $Server
481+
482+
$deviceConf = New-Object KeeperSecurity.Configuration.DeviceConfiguration $deviceToken
483+
if ($Server) {
484+
$deviceConf.ServerInfo.Put((New-Object KeeperSecurity.Configuration.DeviceServerConfiguration $Server))
485+
}
486+
$configuration.Devices.Put($deviceConf)
487+
488+
if ($Username) {
489+
$userConf = New-Object KeeperSecurity.Configuration.UserConfiguration $Username
490+
$userConf.Server = $Server
491+
$userConf.LastDevice = New-Object KeeperSecurity.Configuration.UserDeviceConfiguration $deviceToken
492+
$configuration.Users.Put($userConf)
493+
}
494+
495+
if ($Server) {
496+
$configuration.Servers.Put((New-Object KeeperSecurity.Configuration.ServerConfiguration $Server))
497+
}
498+
499+
return New-Object KeeperSecurity.Configuration.InMemoryConfigurationStorage $configuration
500+
}
501+
466502
function Connect-Keeper {
467503
<#
468504
.Synopsis
@@ -487,6 +523,9 @@ function Connect-Keeper {
487523
.Parameter Server
488524
Change default keeper server
489525
526+
.Parameter Device
527+
Device token. When provided, skips loading configuration from file.
528+
490529
.Parameter Config
491530
Config file name
492531
@@ -507,14 +546,23 @@ function Connect-Keeper {
507546
[Parameter(ParameterSetName = 'sso_password')][switch] $SsoPassword,
508547
[Parameter(ParameterSetName = 'sso_provider')][switch] $SsoProvider,
509548
[Parameter()][string] $Server,
549+
[Parameter()][string] $Device,
510550
[Parameter()][string] $Config,
511551
[Parameter()][switch] $UseOfflineStorage,
512552
[Parameter()][string] $VaultDatabasePath,
513553
[Parameter()][switch] $SkipSync
514554
)
515555

516556
Disconnect-Keeper -Resume | Out-Null
517-
if ($Config) {
557+
$deviceTokenOnly = $false
558+
if ($Device) {
559+
if ($Config) {
560+
Write-Error "The Device and Config parameters cannot be used together." -ErrorAction Stop
561+
}
562+
$storage = getConfigurationForDevice -Device $Device -Username $Username -Server $Server
563+
$deviceTokenOnly = $true
564+
}
565+
elseif ($Config) {
518566
$storage = New-Object KeeperSecurity.Configuration.JsonConfigurationStorage $Config
519567
} else {
520568
$storage = New-Object KeeperSecurity.Configuration.JsonConfigurationStorage
@@ -529,6 +577,7 @@ function Connect-Keeper {
529577
$authFlow = New-Object KeeperSecurity.Authentication.Sync.AuthSync($storage, $endpoint)
530578

531579
$authFlow.AlternatePassword = $SsoPassword.IsPresent
580+
$authFlow.NoNewDevice = $deviceTokenOnly
532581

533582
if (-not $NewLogin.IsPresent -and -not $SsoProvider.IsPresent) {
534583
if (-not $Username) {
@@ -565,6 +614,9 @@ function Connect-Keeper {
565614
}
566615
}
567616
$authFlow.ResumeSession = $canResume
617+
if ($deviceTokenOnly) {
618+
$authFlow.ResumeSession = $false
619+
}
568620
Write-Verbose "Resume Session: $($authFlow.ResumeSession)"
569621

570622
$biometricPresent = $false

0 commit comments

Comments
 (0)