|
| 1 | +package com.foo.rest.examples.spring.openapi.v3.httporacle.invalidallow.auth |
| 2 | + |
| 3 | +import org.springframework.boot.SpringApplication |
| 4 | +import org.springframework.boot.autoconfigure.SpringBootApplication |
| 5 | +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration |
| 6 | +import org.springframework.http.ResponseEntity |
| 7 | +import org.springframework.web.bind.annotation.GetMapping |
| 8 | +import org.springframework.web.bind.annotation.PathVariable |
| 9 | +import org.springframework.web.bind.annotation.PutMapping |
| 10 | +import org.springframework.web.bind.annotation.RequestHeader |
| 11 | +import org.springframework.web.bind.annotation.RequestMapping |
| 12 | +import org.springframework.web.bind.annotation.RequestMethod |
| 13 | +import org.springframework.web.bind.annotation.RestController |
| 14 | + |
| 15 | + |
| 16 | +@SpringBootApplication(exclude = [SecurityAutoConfiguration::class]) |
| 17 | +@RequestMapping(path = ["/api"]) |
| 18 | +@RestController |
| 19 | +open class HttpInvalidAllowAuthApplication { |
| 20 | + |
| 21 | + companion object { |
| 22 | + @JvmStatic |
| 23 | + fun main(args: Array<String>) { |
| 24 | + SpringApplication.run(HttpInvalidAllowAuthApplication::class.java, *args) |
| 25 | + } |
| 26 | + |
| 27 | + val USERS = setOf("FOO", "BAR") |
| 28 | + |
| 29 | + private val products = mutableMapOf<Int, String>() |
| 30 | + private val orders = mutableMapOf<Int, String>() |
| 31 | + |
| 32 | + fun reset() { |
| 33 | + products.clear() |
| 34 | + orders.clear() |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private fun isValidUser(auth: String?) = auth != null && USERS.contains(auth) |
| 39 | + |
| 40 | + // OPTIONS is gated behind auth: without a valid user it returns 401, so the Allow |
| 41 | + // header can only be read after the oracle retries with an authenticated user. |
| 42 | + // The Allow lists DELETE, which is not declared in the schema (extra verb) -> fault. |
| 43 | + @RequestMapping(path = ["/products/{id}"], method = [RequestMethod.OPTIONS]) |
| 44 | + open fun optionsProduct( |
| 45 | + @RequestHeader(value = "Authorization", required = false) auth: String? |
| 46 | + ): ResponseEntity<Any> { |
| 47 | + if (!isValidUser(auth)) return ResponseEntity.status(401).build() |
| 48 | + return ResponseEntity.status(200).header("Allow", "GET,PUT,DELETE,HEAD,OPTIONS").build() |
| 49 | + } |
| 50 | + |
| 51 | + @GetMapping(path = ["/products/{id}"]) |
| 52 | + open fun getProduct( |
| 53 | + @RequestHeader(value = "Authorization", required = false) auth: String?, |
| 54 | + @PathVariable("id") id: Int |
| 55 | + ): ResponseEntity<String> { |
| 56 | + if (!isValidUser(auth)) return ResponseEntity.status(401).build() |
| 57 | + val value = products[id] ?: return ResponseEntity.status(404).build() |
| 58 | + return ResponseEntity.status(200).body(value) |
| 59 | + } |
| 60 | + |
| 61 | + @PutMapping(path = ["/products/{id}"]) |
| 62 | + open fun putProduct( |
| 63 | + @RequestHeader(value = "Authorization", required = false) auth: String?, |
| 64 | + @PathVariable("id") id: Int |
| 65 | + ): ResponseEntity<Any> { |
| 66 | + if (!isValidUser(auth)) return ResponseEntity.status(401).build() |
| 67 | + val isNew = !products.containsKey(id) |
| 68 | + products[id] = "$id" |
| 69 | + return ResponseEntity.status(if (isNew) 201 else 200).build() |
| 70 | + } |
| 71 | + |
| 72 | + // Clean resource: Allow matches the schema (ignoring HEAD/OPTIONS). |
| 73 | + @RequestMapping(path = ["/orders/{id}"], method = [RequestMethod.OPTIONS]) |
| 74 | + open fun optionsOrder( |
| 75 | + @RequestHeader(value = "Authorization", required = false) auth: String? |
| 76 | + ): ResponseEntity<Any> { |
| 77 | + if (!isValidUser(auth)) return ResponseEntity.status(401).build() |
| 78 | + return ResponseEntity.status(200).header("Allow", "GET,PUT,HEAD,OPTIONS").build() |
| 79 | + } |
| 80 | + |
| 81 | + @GetMapping(path = ["/orders/{id}"]) |
| 82 | + open fun getOrder( |
| 83 | + @RequestHeader(value = "Authorization", required = false) auth: String?, |
| 84 | + @PathVariable("id") id: Int |
| 85 | + ): ResponseEntity<String> { |
| 86 | + if (!isValidUser(auth)) return ResponseEntity.status(401).build() |
| 87 | + val value = orders[id] ?: return ResponseEntity.status(404).build() |
| 88 | + return ResponseEntity.status(200).body(value) |
| 89 | + } |
| 90 | + |
| 91 | + @PutMapping(path = ["/orders/{id}"]) |
| 92 | + open fun putOrder( |
| 93 | + @RequestHeader(value = "Authorization", required = false) auth: String?, |
| 94 | + @PathVariable("id") id: Int |
| 95 | + ): ResponseEntity<Any> { |
| 96 | + if (!isValidUser(auth)) return ResponseEntity.status(401).build() |
| 97 | + val isNew = !orders.containsKey(id) |
| 98 | + orders[id] = "$id" |
| 99 | + return ResponseEntity.status(if (isNew) 201 else 200).build() |
| 100 | + } |
| 101 | +} |
0 commit comments