Skip to content

Commit

Permalink
Workaround for Cisco Meraki uris
Browse files Browse the repository at this point in the history
Closes #14
  • Loading branch information
timokoessler committed Apr 17, 2024
1 parent c000e4e commit 367a1a9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
11 changes: 11 additions & 0 deletions Guard.Test/Import/OTPUriParsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,16 @@ public void ParseUriWithUriEncodedLabel()
Assert.Equal("SECRET", otpUri.Secret);
Assert.Equal(OtpUriType.TOTP, otpUri.Type);
}

[Fact]
public void ParseCiscoMerakiUri()
{
string uri = "otpauth://totp/test%40example.com%2FMeraki?secret=ABCDEFG";
OTPUri otpUri = OTPUriParser.Parse(uri);
Assert.Equal("test@example.com", otpUri.Account);
Assert.Equal("Meraki", otpUri.Issuer);
Assert.Equal("ABCDEFG", otpUri.Secret);
Assert.Equal(OtpUriType.TOTP, otpUri.Type);
}
}
}
35 changes: 27 additions & 8 deletions Guard/Core/Import/OTPUriParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,39 @@ internal static OTPUri Parse(string uriString)
OTPUri otpUri = new() { Type = OtpUriType.TOTP, };

string label = uri.LocalPath;
if (label.Length < 2)
{
throw new Exception("Invalid URI label");
}
// Remove leading slash
label = label[1..];

string[] labelSplitChars = [":", "%3A", "/", "%2F"];

if (label.Contains(':'))
foreach (string splitChar in labelSplitChars)
{
otpUri.Issuer = label.Split(':')[0][1..];
otpUri.Account = label.Split(':')[1];
if (label.Contains(splitChar))
{
var splitted = label.Split(splitChar);
if (splitted.Length == 2)
{
otpUri.Issuer = splitted[0];
otpUri.Account = splitted[1];
break;
}
}
}
else if (label.Contains("%3A"))

if (string.IsNullOrEmpty(otpUri.Account))
{
otpUri.Issuer = label.Split("%3A")[0][1..];
otpUri.Account = label.Split("%3A")[1];
otpUri.Account = label;
}
else

// Workaround for Cisco Meraki (issue #14)
if (otpUri.Account.Equals("Meraki") && !string.IsNullOrEmpty(otpUri.Issuer))
{
otpUri.Account = label[1..];
otpUri.Account = otpUri.Issuer;
otpUri.Issuer = "Meraki";
}

if (uri.Query == null || uri.Query.Length < 2)
Expand Down

0 comments on commit 367a1a9

Please sign in to comment.