Class: Keiyaku::Emitter::Security

Inherits:
Object
  • Object
show all
Defined in:
lib/keiyaku/emitter/security.rb

Overview

What the document says a caller has to send, in the runtime's own vocabulary. A scheme nobody can implement is kept out of the table rather than out of the document: it only costs the operations that actually require it, and an operation that documents mutualTLS or an API key is one this client can still call.

The schemes arrive already resolved, because a $ref is the Emitter's business and this has no document to look one up in.

Instance Method Summary collapse

Constructor Details

#initialize(schemes:, default:, notes:) ⇒ Security

Returns a new instance of Security.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/keiyaku/emitter/security.rb', line 16

def initialize(schemes:, default:, notes:)
  @notes = notes
  @schemes = {}      # the document's name for a scheme => how the runtime is told to send it
  @unsupported = {}  # the document's name => what it is, for the message that refuses it

  schemes.each do |name, scheme|
    if (declaration = declaration_for(scheme))
      @schemes[name] = declaration
    else
      @unsupported[name] = [scheme["type"], scheme["scheme"]].compact.join(" ")
    end
  end

  @default = requirement_for(default)
  # What an operation that says nothing about security gets. An
  # alternative naming a scheme nothing can send is not one of them, and
  # the operations that are left with none are refused one by one.
  @usable = (@default || []).select { satisfiable?(_1) }
end

Instance Method Details

#source_for(name, op) ⇒ Object

What the operation sends, as the DSL spells it — or nil where that is the client's default, which is declared once at the top of the class and does not need repeating on the method.



39
40
41
42
# File 'lib/keiyaku/emitter/security.rb', line 39

def source_for(name, op)
  alternatives = for_operation(name, op)
  requirement_source(alternatives) unless alternatives == @usable
end

#tableObject

The schemes the whole document has, and the requirement that holds wherever an operation does not state one of its own. Both belong to the client rather than to any of its methods.



47
48
49
50
51
52
53
# File 'lib/keiyaku/emitter/security.rb', line 47

def table
  return "" if @schemes.empty?

  declarations = @schemes.map { |name, declaration| "#{scheme_key(name)} #{declaration}" }
  default = @usable.empty? ? "" : ", default: #{requirement_source(@usable)}"
  "    security({ #{declarations.join(", ")} }#{default})\n"
end