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
2
6
{
3
7
/// <summary>
4
8
/// A code describing the result of an attempt to parse an Api key.
@@ -11,6 +15,15 @@ public enum ApiKeyParseResult
11
15
/// </summary>
12
16
None ,
13
17
/// <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>
14
27
/// The key was found to be blank or invalid.
15
28
/// </summary>
16
29
InvalidEmptyOrWhitespace ,
@@ -23,6 +36,14 @@ public enum ApiKeyParseResult
23
36
/// </summary>
24
37
InvalidUnableToExtractSecretPart ,
25
38
/// <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>
26
47
/// Key was successfully parsed.
27
48
/// </summary>
28
49
Success
Original file line number Diff line number Diff line change @@ -16,17 +16,31 @@ public ApiKeyParseResult Parse(string wholeApiKey)
16
16
{
17
17
if ( string . IsNullOrWhiteSpace ( wholeApiKey ) )
18
18
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
+
20
26
//extract public part
21
27
var maxCount = Math . Min ( 50 , wholeApiKey . Length ) ;
22
28
var publicPartEnd = wholeApiKey . IndexOf ( '.' , startIndex : 0 , maxCount ) ; //don't check more than 50 chars
23
29
if ( publicPartEnd == - 1 )
24
30
return ApiKeyParseResult . InvalidUnableToExtractPublicPart ;
25
-
31
+
32
+ var publicPart = wholeApiKey . Substring ( 0 , publicPartEnd ) ;
33
+ if ( publicPart . Length != 20 )
34
+ return ApiKeyParseResult . InvalidPublicPartLength ;
35
+
26
36
//now extract the private part
27
37
if ( wholeApiKey . Length <= publicPartEnd + 1 )
28
38
return ApiKeyParseResult . InvalidUnableToExtractSecretPart ;
29
39
40
+ var privatePart = wholeApiKey . Substring ( publicPartEnd + 1 ) ;
41
+ if ( privatePart . Length != 40 )
42
+ return ApiKeyParseResult . InvalidSecretPartLength ;
43
+
30
44
//success
31
45
return ApiKeyParseResult . Success ;
32
46
}
You can’t perform that action at this time.
0 commit comments