Sha256: cb214284ed628a95153835722011bc3232b98b55caf336a09832f7726da580bc

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true


module SocketLabs
  module InjectionApi
    module Core
      class ApiKeyParser


        # Parse the API key to determine what kind of key was provided.
        # @param [String] wholeApiKey: A ApiKeyParseResult with the parsing results
        # @return [ApiKeyParseResult] the ApiKeyParseResult from the request
        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

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
socketlabs-injectionapi-1.4.4 lib/socketlabs/injectionapi/core/api_key_parser.rb
socketlabs-injectionapi-1.4.3 lib/socketlabs/injectionapi/core/api_key_parser.rb