Skip to content

Commit 3883272

Browse files
testing API key parser
1 parent 0458951 commit 3883272

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/SocketLabs/InjectionApi/Core/ApiKeyParseResult.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
namespace SocketLabs.InjectionApi.Core
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace SocketLabs.InjectionApi.Core
26
{
37
/// <summary>
48
/// A code describing the result of an attempt to parse an Api key.
@@ -11,6 +15,15 @@ public enum ApiKeyParseResult
1115
/// </summary>
1216
None,
1317
/// <summary>
18+
/// The key length was found.
19+
/// </summary>
20+
InvalidKeyLength,
21+
22+
/// <summary>
23+
/// The key format was found.
24+
/// </summary>
25+
InvalidKeyFormat,
26+
/// <summary>
1427
/// The key was found to be blank or invalid.
1528
/// </summary>
1629
InvalidEmptyOrWhitespace,
@@ -23,6 +36,14 @@ public enum ApiKeyParseResult
2336
/// </summary>
2437
InvalidUnableToExtractSecretPart,
2538
/// <summary>
39+
/// The public portion of the key is the incorrect length.
40+
/// </summary>
41+
InvalidPublicPartLength,
42+
/// <summary>
43+
/// The secret portion of the key is the incorrect length.
44+
/// </summary>
45+
InvalidSecretPartLength,
46+
/// <summary>
2647
/// Key was successfully parsed.
2748
/// </summary>
2849
Success

src/SocketLabs/InjectionApi/Core/ApiKeyParser.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,31 @@ public ApiKeyParseResult Parse(string wholeApiKey)
1616
{
1717
if (string.IsNullOrWhiteSpace(wholeApiKey))
1818
return ApiKeyParseResult.InvalidEmptyOrWhitespace;
19-
19+
20+
if (wholeApiKey.Length != 61)
21+
return ApiKeyParseResult.InvalidKeyLength;
22+
23+
if (wholeApiKey.IndexOf(".", StringComparison.Ordinal) == -1)
24+
return ApiKeyParseResult.InvalidKeyFormat;
25+
2026
//extract public part
2127
var maxCount = Math.Min(50, wholeApiKey.Length);
2228
var publicPartEnd = wholeApiKey.IndexOf('.', startIndex: 0, maxCount); //don't check more than 50 chars
2329
if (publicPartEnd == -1)
2430
return ApiKeyParseResult.InvalidUnableToExtractPublicPart;
25-
31+
32+
var publicPart = wholeApiKey.Substring(0, publicPartEnd);
33+
if (publicPart.Length != 20)
34+
return ApiKeyParseResult.InvalidPublicPartLength;
35+
2636
//now extract the private part
2737
if (wholeApiKey.Length <= publicPartEnd + 1)
2838
return ApiKeyParseResult.InvalidUnableToExtractSecretPart;
2939

40+
var privatePart = wholeApiKey.Substring(publicPartEnd + 1);
41+
if (privatePart.Length != 40)
42+
return ApiKeyParseResult.InvalidSecretPartLength;
43+
3044
//success
3145
return ApiKeyParseResult.Success;
3246
}

0 commit comments

Comments
 (0)