-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add security support for codegen (#3094)
- Loading branch information
1 parent
5c73922
commit 05f9b2b
Showing
9 changed files
with
327 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...gen/core/src/main/scala/sttp/tapir/codegen/openapi/models/OpenapiSecuritySchemeType.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package sttp.tapir.codegen.openapi.models | ||
|
||
sealed trait OpenapiSecuritySchemeType | ||
|
||
object OpenapiSecuritySchemeType { | ||
case object OpenapiSecuritySchemeBearerType extends OpenapiSecuritySchemeType | ||
case object OpenapiSecuritySchemeBasicType extends OpenapiSecuritySchemeType | ||
case class OpenapiSecuritySchemeApiKeyType(in: String, name: String) extends OpenapiSecuritySchemeType | ||
|
||
import io.circe._ | ||
import cats.implicits._ | ||
|
||
private implicit val BearerTypeDecoder: Decoder[OpenapiSecuritySchemeBearerType.type] = { (c: HCursor) => | ||
for { | ||
_ <- c.get[String]("type").ensure(DecodingFailure("Given type is not http!", c.history))(_ == "http") | ||
_ <- c.get[String]("scheme").ensure(DecodingFailure("Given scheme is not bearer!", c.history))(_ == "bearer") | ||
} yield { | ||
OpenapiSecuritySchemeBearerType | ||
} | ||
} | ||
|
||
private implicit val BasicTypeDecoder: Decoder[OpenapiSecuritySchemeBasicType.type] = { (c: HCursor) => | ||
for { | ||
_ <- c.get[String]("type").ensure(DecodingFailure("Given type is not http!", c.history))(_ == "http") | ||
_ <- c.get[String]("scheme").ensure(DecodingFailure("Given scheme is not basic!", c.history))(_ == "basic") | ||
} yield { | ||
OpenapiSecuritySchemeBasicType | ||
} | ||
} | ||
|
||
private val ApiKeyInOptions = List("header", "query", "cookie") | ||
|
||
private implicit val ApiKeyDecoder: Decoder[OpenapiSecuritySchemeApiKeyType] = { (c: HCursor) => | ||
for { | ||
_ <- c.get[String]("type").ensure(DecodingFailure("Given type is not apiKey!", c.history))(_ == "apiKey") | ||
in <- c.get[String]("in").ensure(DecodingFailure("Invalid apiKey in value!", c.history))(ApiKeyInOptions.contains) | ||
name <- c.get[String]("name") | ||
} yield { | ||
OpenapiSecuritySchemeApiKeyType(in, name) | ||
} | ||
} | ||
|
||
implicit val OpenapiSecuritySchemeTypeDecoder: Decoder[OpenapiSecuritySchemeType] = | ||
List[Decoder[OpenapiSecuritySchemeType]]( | ||
Decoder[OpenapiSecuritySchemeBearerType.type].widen, | ||
Decoder[OpenapiSecuritySchemeBasicType.type].widen, | ||
Decoder[OpenapiSecuritySchemeApiKeyType].widen | ||
).reduceLeft(_ or _) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.