Class: SocketLabs::InjectionApi::Core::ApiKeyParser

Inherits:
Object
  • Object
show all
Defined in:
lib/socketlabs/injectionapi/core/api_key_parser.rb

Instance Method Summary collapse

Instance Method Details

#parse(wholeApiKey) ⇒ ApiKeyParseResult

Parse the API key to determine what kind of key was provided.

Parameters:

  • wholeApiKey: (String)

    A ApiKeyParseResult with the parsing results

Returns:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/socketlabs/injectionapi/core/api_key_parser.rb', line 13

def parse(
  wholeApiKey
)




  if wholeApiKey.nil? || wholeApiKey.empty?
    ApiKeyParseResult.enum["InvalidEmptyOrWhitespace"]
  end

  if wholeApiKey.length != 61
    ApiKeyParseResult.enum["InvalidKeyLength"]
  end

  if wholeApiKey.index('.') == -1
    ApiKeyParseResult.enum["InvalidKeyFormat"]
  end

  # extract public part
  # don't check more than 50 chars
  publicPartEnd = wholeApiKey[0..50].index('.')
  if publicPartEnd == -1
    ApiKeyParseResult.enum["InvalidUnableToExtractPublicPart"]
  end

  publicPart = wholeApiKey[0..publicPartEnd]
  if publicPart != 20
    ApiKeyParseResult.enum["InvalidPublicPartLength"]
  end

  # now extract the private part
  if wholeApiKey.length <= publicPartEnd + 1
    ApiKeyParseResult.enum["InvalidUnableToExtractSecretPart"]
  end

  privatePart = wholeApiKey[publicPartEnd + 1..wholeApiKey.length]

  if privatePart.length != 40
    ApiKeyParseResult.enum["InvalidSecretPartLength"]
  end

  ApiKeyParseResult.enum["Success"]

end