-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ktor2 library option to kotlin server generator
- Loading branch information
Showing
46 changed files
with
2,099 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
generatorName: kotlin-server | ||
outputDir: samples/server/petstore/kotlin-server/ktor2 | ||
library: ktor2 | ||
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml | ||
templateDir: modules/openapi-generator/src/main/resources/kotlin-server | ||
additionalProperties: | ||
hideGenerationTimestamp: "true" | ||
serializableModel: "true" |
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
102 changes: 102 additions & 0 deletions
102
...openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/ApiKeyAuth.kt.mustache
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,102 @@ | ||
package org.openapitools.server.infrastructure | ||
|
||
import io.ktor.http.auth.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.auth.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
|
||
enum class ApiKeyLocation(val location: String) { | ||
QUERY("query"), | ||
HEADER("header") | ||
} | ||
|
||
data class ApiKeyCredential(val value: String) : Credential | ||
data class ApiPrincipal(val apiKeyCredential: ApiKeyCredential?) : Principal | ||
|
||
/** | ||
* Represents an Api Key authentication provider | ||
*/ | ||
class ApiKeyAuthenticationProvider(configuration: Configuration) : AuthenticationProvider(configuration) { | ||
private val authenticationFunction = configuration.authenticationFunction | ||
private val apiKeyName: String = configuration.apiKeyName | ||
private val apiKeyLocation: ApiKeyLocation = configuration.apiKeyLocation | ||
override suspend fun onAuthenticate(context: AuthenticationContext) { | ||
val call = context.call | ||
val credentials = call.request.apiKeyAuthenticationCredentials(apiKeyName, apiKeyLocation) | ||
val principal = credentials?.let { authenticationFunction.invoke(call, it) } | ||
|
||
val cause = when { | ||
credentials == null -> AuthenticationFailedCause.NoCredentials | ||
principal == null -> AuthenticationFailedCause.InvalidCredentials | ||
else -> null | ||
} | ||
|
||
if (cause != null) { | ||
context.challenge(apiKeyName, cause) { challenge, call -> | ||
call.respond( | ||
UnauthorizedResponse( | ||
HttpAuthHeader.Parameterized( | ||
"API_KEY", | ||
mapOf("key" to apiKeyName), | ||
HeaderValueEncoding.QUOTED_ALWAYS | ||
) | ||
) | ||
) | ||
challenge.complete() | ||
} | ||
} | ||
|
||
if (principal != null) { | ||
context.principal(principal) | ||
} | ||
} | ||
|
||
class Configuration internal constructor(name: String?) : Config(name) { | ||
internal var authenticationFunction: suspend ApplicationCall.(ApiKeyCredential) -> Principal? = { | ||
throw NotImplementedError( | ||
"Api Key auth validate function is not specified. Use apiKeyAuth { validate { ... } } to fix." | ||
) | ||
} | ||
|
||
var apiKeyName: String = "" | ||
|
||
var apiKeyLocation: ApiKeyLocation = ApiKeyLocation.QUERY | ||
|
||
/** | ||
* Sets a validation function that will check given [ApiKeyCredential] instance and return [Principal], | ||
* or null if credential does not correspond to an authenticated principal | ||
*/ | ||
fun validate(body: suspend ApplicationCall.(ApiKeyCredential) -> Principal?) { | ||
authenticationFunction = body | ||
} | ||
} | ||
} | ||
|
||
fun AuthenticationConfig.apiKeyAuth( | ||
name: String? = null, | ||
configure: ApiKeyAuthenticationProvider.Configuration.() -> Unit | ||
) { | ||
val configuration = ApiKeyAuthenticationProvider.Configuration(name).apply(configure) | ||
val provider = ApiKeyAuthenticationProvider(configuration) | ||
register(provider) | ||
} | ||
|
||
fun ApplicationRequest.apiKeyAuthenticationCredentials( | ||
apiKeyName: String, | ||
apiKeyLocation: ApiKeyLocation | ||
): ApiKeyCredential? { | ||
val value: String? = when (apiKeyLocation) { | ||
ApiKeyLocation.QUERY -> this.queryParameters[apiKeyName] | ||
ApiKeyLocation.HEADER -> this.headers[apiKeyName] | ||
} | ||
return when (value) { | ||
null -> null | ||
else -> ApiKeyCredential(value) | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
...es/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/AppMain.kt.mustache
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,139 @@ | ||
package {{packageName}} | ||
|
||
import io.ktor.server.application.* | ||
import io.ktor.serialization.gson.* | ||
import io.ktor.http.* | ||
{{#featureResources}} | ||
import io.ktor.server.resources.* | ||
{{/featureResources}} | ||
{{#featureCORS}} | ||
import io.ktor.server.plugins.cors.routing.* | ||
{{/featureCORS}} | ||
{{#featureAutoHead}} | ||
import io.ktor.server.plugins.autohead.* | ||
{{/featureAutoHead}} | ||
{{#featureConditionalHeaders}} | ||
import io.ktor.server.plugins.conditionalheaders.* | ||
{{/featureConditionalHeaders}} | ||
{{#featureCompression}} | ||
import io.ktor.server.plugins.compression.* | ||
{{/featureCompression}} | ||
import io.ktor.server.plugins.contentnegotiation.* | ||
import io.ktor.server.plugins.defaultheaders.* | ||
{{#featureHSTS}} | ||
import io.ktor.server.plugins.hsts.* | ||
{{/featureHSTS}} | ||
{{#featureMetrics}} | ||
import com.codahale.metrics.Slf4jReporter | ||
import io.ktor.server.metrics.dropwizard.* | ||
import java.util.concurrent.TimeUnit | ||
{{/featureMetrics}} | ||
import io.ktor.server.routing.* | ||
{{#hasAuthMethods}} | ||
import com.typesafe.config.ConfigFactory | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.engine.apache.Apache | ||
import io.ktor.server.config.HoconApplicationConfig | ||
import io.ktor.server.auth.* | ||
import org.openapitools.server.infrastructure.* | ||
{{/hasAuthMethods}} | ||
{{#generateApis}}{{#apiInfo}}{{#apis}}import {{apiPackage}}.{{classname}} | ||
{{/apis}}{{/apiInfo}}{{/generateApis}} | ||
|
||
{{#hasAuthMethods}} | ||
internal val settings = HoconApplicationConfig(ConfigFactory.defaultApplication(HTTP::class.java.classLoader)) | ||
|
||
object HTTP { | ||
val client = HttpClient(Apache) | ||
} | ||
{{/hasAuthMethods}} | ||
|
||
fun Application.main() { | ||
install(DefaultHeaders) | ||
{{#featureMetrics}} | ||
install(DropwizardMetrics) { | ||
val reporter = Slf4jReporter.forRegistry(registry) | ||
.outputTo([email protected]) | ||
.convertRatesTo(TimeUnit.SECONDS) | ||
.convertDurationsTo(TimeUnit.MILLISECONDS) | ||
.build() | ||
reporter.start(10, TimeUnit.SECONDS) | ||
} | ||
{{/featureMetrics}} | ||
{{#generateApis}} | ||
install(ContentNegotiation) { | ||
register(ContentType.Application.Json, GsonConverter()) | ||
} | ||
{{#featureAutoHead}} | ||
install(AutoHeadResponse) // see https://ktor.io/docs/autoheadresponse.html | ||
{{/featureAutoHead}} | ||
{{#featureConditionalHeaders}} | ||
install(ConditionalHeaders) // see https://ktor.io/docs/conditional-headers.html | ||
{{/featureConditionalHeaders}} | ||
{{#featureCompression}} | ||
install(Compression, ApplicationCompressionConfiguration()) // see https://ktor.io/docs/compression.html | ||
{{/featureCompression}} | ||
{{#featureCORS}} | ||
install(CORS, ApplicationCORSConfiguration()) // see https://ktor.io/docs/cors.html | ||
{{/featureCORS}} | ||
{{#featureHSTS}} | ||
install(HSTS, ApplicationHstsConfiguration()) // see https://ktor.io/docs/hsts.html | ||
{{/featureHSTS}} | ||
{{#featureResources}} | ||
install(Resources) | ||
{{/featureResources}} | ||
{{#hasAuthMethods}} | ||
install(Authentication) { | ||
{{#authMethods}} | ||
{{#isBasicBasic}} | ||
basic("{{{name}}}") { | ||
validate { credentials -> | ||
// TODO: "Apply your basic authentication functionality." | ||
// Accessible in-method via call.principal<UserIdPrincipal>() | ||
if (credentials.name == "Swagger" && "Codegen" == credentials.password) { | ||
UserIdPrincipal(credentials.name) | ||
} else { | ||
null | ||
} | ||
} | ||
{{/isBasicBasic}} | ||
{{#isApiKey}} | ||
// "Implement API key auth ({{{name}}}) for parameter name '{{{keyParamName}}}'." | ||
apiKeyAuth("{{{name}}}") { | ||
validate { apikeyCredential: ApiKeyCredential -> | ||
when { | ||
apikeyCredential.value == "keyboardcat" -> ApiPrincipal(apikeyCredential) | ||
else -> null | ||
} | ||
} | ||
} | ||
{{/isApiKey}} | ||
{{#isOAuth}} | ||
{{#bodyAllowed}} | ||
{{/bodyAllowed}} | ||
{{^bodyAllowed}} | ||
oauth("{{name}}") { | ||
client = HttpClient(Apache) | ||
providerLookup = { applicationAuthProvider([email protected]) } | ||
urlProvider = { _ -> | ||
// TODO: define a callback url here. | ||
"/" | ||
} | ||
} | ||
{{/bodyAllowed}} | ||
{{/isOAuth}} | ||
{{/authMethods}} | ||
} | ||
{{/hasAuthMethods}} | ||
install(Routing) { | ||
{{#apiInfo}} | ||
{{#apis}} | ||
{{#operations}} | ||
{{classname}}() | ||
{{/operations}} | ||
{{/apis}} | ||
{{/apiInfo}} | ||
} | ||
|
||
{{/generateApis}} | ||
} |
Oops, something went wrong.