Module: SwaggerDocsRails::SecuritySchemeBuilder

Defined in:
lib/swagger_docs_rails/security_scheme_builder.rb

Class Method Summary collapse

Class Method Details

.api_key_scheme(scheme) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/swagger_docs_rails/security_scheme_builder.rb', line 46

def api_key_scheme(scheme)
  local = scheme[:in].presence || scheme[:location].presence || "header"
  nome_param = scheme[:param_name].presence || scheme[:header_name].presence ||
               scheme[:name_param].presence
  if nome_param.nil? || nome_param.to_s.strip.empty?
    raise ArgumentError, "security_schemes api_key: informe :param_name ou :header_name"
  end

  {
    type: "apiKey",
    in: local.to_s,
    name: nome_param,
    description: scheme[:description].presence || "Chave de API"
  }.compact
end

.build_all(schemes) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/swagger_docs_rails/security_scheme_builder.rb', line 20

def build_all(schemes)
  Array(schemes).each_with_object({}) do |scheme, resultado|
    nome = scheme[:name] || scheme["name"]
    raise ArgumentError, "security_schemes: :name é obrigatório" if nome.nil? || nome.to_s.strip.empty?

    resultado[nome.to_s] = to_openapi(scheme)
  end
end

.build_global_security(requirements) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/swagger_docs_rails/security_scheme_builder.rb', line 29

def build_global_security(requirements)
  Array(requirements).map do |requisito|
    requisito.each_with_object({}) do |(nome, escopos), hash|
      hash[nome.to_s] = Array(escopos)
    end
  end
end

.http_scheme(scheme) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/swagger_docs_rails/security_scheme_builder.rb', line 37

def http_scheme(scheme)
  {
    type: "http",
    scheme: scheme[:scheme].presence || "bearer",
    bearerFormat: scheme[:bearer_format].presence || scheme[:bearerFormat].presence || "JWT",
    description: scheme[:description].presence || "Token Bearer"
  }.compact
end

.to_openapi(scheme) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/swagger_docs_rails/security_scheme_builder.rb', line 7

def to_openapi(scheme)
  tipo = scheme[:type].to_s.downcase.tr("-", "_").to_sym

  case tipo
  when :http, :bearer
    http_scheme(scheme)
  when :api_key, :apikey
    api_key_scheme(scheme)
  else
    raise ArgumentError, "Tipo de segurança não suportado: #{scheme[:type]} (use :http ou :api_key)"
  end
end