Class: OpenAPISourceTools::SecuritySchemesTask

Inherits:
Object
  • Object
show all
Includes:
TaskInterface
Defined in:
lib/openapi/sourcetools/securityschemes.rb

Overview

Helper class for dealing with securitySchemes. Adds :security key to each operation object and to root. They contain the used securitySchemes in use and the security schemes in effect for that operation.

Constant Summary collapse

COMPONENTS =
'#/components/'

Instance Method Summary collapse

Methods included from TaskInterface

#executable, #output_name, #system

Instance Method Details

#convert_security_schemes(doc) ⇒ Object



163
164
165
166
167
168
169
170
171
172
# File 'lib/openapi/sourcetools/securityschemes.rb', line 163

def convert_security_schemes(doc)
  ss = doc.dig('components', 'securitySchemes')
  return nil if ss.nil?
  # Should create unique objects. Different name may lead to same object.
  out = {}
  ss.each do |name, security_scheme|
    out[name] = SecuritySchemeInfo.new(security_scheme)
  end
  out
end

#default_configurationObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/openapi/sourcetools/securityschemes.rb', line 103

def default_configuration
  # type apiKey, name, in.
  # type http, scheme, bearerFormat.
  # type mutualTLS, beyond the scope of code templates.
  # type oauth2, flows object.
  # type openIdConnect, openIdConnectUrl. More for login?
  YAML.safe_load(%q(---
security_schemes:
- scheme:
type: apiKey
in: header
  output:
headers:
  '<name>': string
- scheme:
type: apiKey
in: query
  output:
parameters:
  '<name>': string
- scheme:
type: apiKey
in: query
  output:
query_parameters:
  '<name>': string
- scheme:
type: apiKey
in: cookie
  output:
cookies:
 '<name>': true
- scheme:
type: http
scheme: basic
  output:
headers:
  Authorization: string
- scheme:
type: http
scheme: bearer
  output:
headers:
  Authorization: string
- scheme:
type: mutualTLS
  output: {}
- scheme:
type: oauth2
  output:
headers:
  Authorization: string
- scheme:
type: openIdConnect
  output:
headers:
  Authorization: string
))
end

#discardObject



262
263
264
# File 'lib/openapi/sourcetools/securityschemes.rb', line 262

def discard
  true
end

#generate(_context_binding) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
# File 'lib/openapi/sourcetools/securityschemes.rb', line 250

def generate(_context_binding)
  # Get security_schemes from config, append defaults to it.
  scheme_templates = gen.configuration['security_schemes'] || []
  scheme_templates.concat(default_configuration['security_schemes'])
  simple_schemes = convert_security_schemes(Gen.doc)
  # For all operation objects, add :security array with all used schemes.
  merged_schemes = operation_object_security(Gen.doc, simple_schemes || {})
  return merged_schemes if merged_schemes.is_a?(Integer)
  Gen.doc[:securitySchemes] = simple_schemes unless simple_schemes.nil?
  Gen.doc[:securitySchemes_merged] = merged_schemes unless merged_schemes.empty?
end

#operation_object_security(doc, schemes) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/openapi/sourcetools/securityschemes.rb', line 174

def operation_object_security(doc, schemes)
  # Find all operation objects and security in effect.
  all_ops = []
  seen_secs = Set.new
  root_sec = doc['security'] || []
  not_method = %w[parameters servers summary description]
  doc['paths'].each_value do |path_item|
    path_sec = path_item['security'] || root_sec
    path_item.each do |method, op|
      next if not_method.include?(method)
      op_sec = op['security'] || path_sec
      all_ops.push([ op, op_sec ])
      op_sec.each do |security_requirement|
        names = security_requirement.keys
        seen_secs.merge(names)
        if names.empty? && !schemes.key?('')
          schemes[''] = SecuritySchemeInfo.new
        end
      end
    end
  end
  # Check that all seen secs names have a scheme. Report all in one place.
  missing = false
  seen_secs.to_a.sort!.each do |name|
    unless schemes.key?(name)
      missing = true
      warn("#/components/securitySchemes is missing: #{name}")
    end
  end
  return 1 if missing
  # Now we know all individual parts are available.
  # Map security arrays of objects to arrays of arrays.
  all_scopeds = [] # For having just one instance for unique data combination.
  all_ops.each do |pair|
    sec = pair.second.map do |sec_req|
      keys = sec_req.keys.sort!
      values = keys.map { |name| sec_req[name].sort! }
      if keys.empty?
        keys = [ '' ]
        values = [ [] ]
      end
      s3is = []
      keys.size.times do |idx|
        name = keys[idx]
        scopes = values[idx]
        s3i = ScopedSecuritySchemeInfo.new(schemes[name], scopes)
        idx = all_scopeds.index(s3i)
        if idx.nil?
          all_scopeds.push(s3i)
        else
          s3i = all_scopeds(idx) # Use the same instance everywhere.
        end
        s3is.push(s3i)
      end
      s3is
    end
    pair.first[:security] = sec # Arrays of ScopedSecuritySchemeInfo.
    # When individual objects are not needed, provide merged together items.
    all_merged = []
    pair.first[:security_merged] = sec.map do |s3is| # ScopedSecuritySchemeInfos.
      m = s3is.first
      s3is[1..].each do |s3i|
        m = m.merge(s3i)
      end
      idx = all_merged.index(m)
      if idx.nil?
        all_merged.push(m)
      else
        m = all_merged[idx] # Use the same instance everywhere.
      end
      m
    end
  end
  all_merged.map(&:ssi).uniq.sort!
end