Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update recovery code generation #40723

Merged
merged 3 commits into from
Mar 16, 2022
Merged

Update recovery code generation #40723

merged 3 commits into from
Mar 16, 2022

Conversation

HaoK
Copy link
Member

@HaoK HaoK commented Mar 15, 2022

Per @GrabYourPitchforks request

New format is now 5 by 5 and looks something like: 4KPMY-NK9NK

Valid characters are taken from the set that windows product key uses, and the implementation is copied from https://source.dot.net/#System.Security.Cryptography/System/Security/Cryptography/RandomNumberGenerator.cs,100 but modified for our specific usage (we unfortunately can't just use RNG since identity also targets non netcoreapp)

@ghost ghost added the area-identity Includes: Identity and providers label Mar 15, 2022
{
// We don't want to use any confusing characters like 0/O 1/I/L/l
// Taken from windows valid product key source
const string AllowedChars = "23456789BCDFGHJKMNPQRTVWXY";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand some confusing chars were removed, in OC we also use them as lower-case which I find even more explicit. But I have the feeling upper case is more standard.

{
// We don't want to use any confusing characters like 0/O 1/I/L/l
// Taken from windows valid product key source
const string AllowedChars = "23456789BCDFGHJKMNPQRTVWXY";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah but that looks gross if I have to do it like this

private static byte[] LookupTable => new byte[] {
    (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4',
    (byte)'5', (byte)'6', (byte)'7', (byte)'8', (byte)'9',
    (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E',
    (byte)'F',
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I am wondering if a char[] is better than a string

private static readonly char[] AllowedChars = "23456789BCDFGHJKMNPQRTVWXY".ToCharArray();

Copy link
Member

@sebastienros sebastienros Mar 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perf doesn't care about gross ;) But it was just FYI, I don't think it matters here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, totally agree that the lookup table could be faster, but yeah this is done generally only on user creation, so readability is more important. That said CharArray change seems fine though, do you want me to make that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer personally the char array, more common for the usage. And also as a private static field in that case.

@HaoK HaoK marked this pull request as ready for review March 15, 2022 20:18
@HaoK HaoK requested a review from Pilchie as a code owner March 15, 2022 20:18
@HaoK HaoK enabled auto-merge (squash) March 16, 2022 16:18
@HaoK HaoK merged commit 618b78e into main Mar 16, 2022
@HaoK HaoK deleted the haok/recov branch March 16, 2022 18:16
@ghost ghost added this to the 7.0-preview3 milestone Mar 16, 2022
@@ -2276,7 +2277,59 @@ public virtual string GenerateNewAuthenticatorKey()
/// </summary>
/// <returns></returns>
protected virtual string CreateTwoFactorRecoveryCode()
=> Guid.NewGuid().ToString().Substring(0, 8);
{
var recoveryCode = new StringBuilder(11);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's a fixed-length string, maybe we could use char[] instead of StringBuilder?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the goal is to generate a string, in that case it wouldn't matter much, SB is allocating an array internally and then generates a single string out of it.

@ghost
Copy link

ghost commented Aug 9, 2022

Hi @Rand-Random. It looks like you just commented on a closed PR. The team will most probably miss it. If you'd like to bring something important up to their attention, consider filing a new issue and add enough details to build context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-identity Includes: Identity and providers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants