Sha256: 4ae55f5a242f847ce4fd640c4ae8557df74c7bebcb77ca85bf847f37ccbfe01c

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

class OpenAPI::Loader::Translator
  #
  # Wraps the schema and moves its securityDefinitions
  # to components.securitySchemes
  #
  # Translates every securityScheme for oauth2 into new format.
  #
  # @private
  #
  class ConvertSecuritySchemes < SimpleDelegator
    def call
      return unless schemes.is_a? Hash
      convert_oauth_schemes
      self["components"] = { "securitySchemes" => schemes }
    end

    private

    def schemes
      @schemes ||= delete("securityDefinitions")
    end

    def oauth_keys
      @oauth_keys ||= schemes.select do |_, scheme|
        scheme.is_a?(Hash) && scheme["type"] == "oauth2"
      end.keys
    end

    def convert_oauth_schemes
      oauth_keys.each { |key| convert_scheme(key) }
    end

    def convert_scheme(key)
      scheme = schemes[key]
      flow   = scheme["flow"]
      flow   = FLOWS.fetch(flow, flow)
      data   = scheme.select { |detail| DETAILS.include? detail }

      schemes[key] = { "type" => "oauth2", "flows" => { flow => data } }
    end

    FLOWS = {
      "application" => "clientCredentials",
      "accessCode"  => "authorizationCode",
    }.freeze

    DETAILS = %w[authorizationUrl tokenUrl scopes]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
open_api-loader-0.0.1 lib/open_api/loader/translator/convert_security_schemes.rb