File tree Expand file tree Collapse file tree 2 files changed +38
-3
lines changed
src/SocketLabs/InjectionApi/Core Expand file tree Collapse file tree 2 files changed +38
-3
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments