All Downloads are FREE. Search and download functionalities are using the official Maven repository.

v2.kotlin-server.libraries.ktor.api.mustache Maven / Gradle / Ivy

The newest version!
{{>licenseInfo}}
package {{apiPackage}}

import com.google.gson.Gson
import io.ktor.application.call
import io.ktor.auth.UserIdPrincipal
import io.ktor.auth.authentication
import io.ktor.auth.basicAuthentication
import io.ktor.auth.oauth
import io.ktor.auth.Principal
import io.ktor.auth.OAuthAccessTokenResponse
import io.ktor.auth.OAuthServerSettings
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.locations.*
import io.ktor.response.respond
import io.ktor.response.respondText
import io.ktor.routing.*

import kotlinx.coroutines.experimental.asCoroutineDispatcher

import {{packageName}}.ApplicationAuthProviders
import {{packageName}}.Paths
import {{packageName}}.ApplicationExecutors
import {{packageName}}.HTTP.client
import {{packageName}}.infrastructure.ApiPrincipal
import {{packageName}}.infrastructure.apiKeyAuth

// ktor 0.9.x is missing io.ktor.locations.DELETE, this adds it.
// see https://github.com/ktorio/ktor/issues/288
import {{packageName}}.delete

{{#imports}}import {{import}}
{{/imports}}

{{#operations}}
fun Route.{{classname}}() {
    val gson = Gson()
    val empty = mutableMapOf()
{{#operation}}
    {{#bodyAllowed}}

    route("{{path}}") {
        {{lower httpMethod}} {
            {{>libraries/ktor/_api_body}}
        }
    }
    {{/bodyAllowed}}
    {{^bodyAllowed}}

    {{! NOTE: Locations can be used on routes without body parameters.}}
    {{lower httpMethod}} {  it: Paths.{{operationId}} ->
        {{>libraries/ktor/_api_body}}
    }
    {{/bodyAllowed}}
    {{! THis looks a little weird, but it's completely valid Kotlin code, and simplifies templated route logic above. }}
    {{#has this 'auth-methods'}}.apply {
      // TODO: ktor doesn't allow different authentication registrations for endpoints sharing the same path but different methods.
      //       It could be the authentication block is being abused here. Until this is resolved, swallow duplicate exceptions.

        try {
            authentication {
                {{#authMethods}}
                {{#is this 'basic'}}
                basicAuthentication("{{{name}}}") { credentials ->
                    // TODO: "Apply your basic authentication functionality."
                    // Accessible in-method via call.principal()
                    if (credentials.name == "Swagger" && "Codegen" == credentials.password) {
                         UserIdPrincipal(credentials.name)
                    } else {
                        null
                    }
                }
                {{/is}}
                {{#is this 'api-key'}}
                // "Implement API key auth ({{{name}}}) for parameter name '{{{keyParamName}}}'."
                apiKeyAuth("{{{keyParamName}}}", {{#is this 'key-in-query'}}"query"{{/is}}{{#is this 'key-in-header'}}"header"{{/is}}) {
                    // TODO: "Verify key here , accessible as it.value"
                    if (it.value == "keyboardcat") {
                         ApiPrincipal(it)
                    } else {
                        null
                    }
                }
                {{/is}}
                {{#is this 'oauth'}}
                {{#bodyAllowed}}
                oauth(client, ApplicationExecutors.asCoroutineDispatcher(), { ApplicationAuthProviders["{{{name}}}"] }, {
                    // TODO: define a callback url here.
                    "/"
                })
                {{/bodyAllowed}}
                {{^bodyAllowed}}
                oauthAtLocation(client, ApplicationExecutors.asCoroutineDispatcher(),
                        providerLookup = { ApplicationAuthProviders["{{{name}}}"] },
                        urlProvider = { currentLocation, provider ->
                           // TODO: define a callback url here.
                           "/"
                        })
                {{/bodyAllowed}}
                {{/is}}
                {{/authMethods}}
            }
        } catch(e: io.ktor.application.DuplicateApplicationFeatureException){
            application.environment.log.warn("authentication block for '{{path}}' is duplicated in code. " +
            "Generated endpoints may need to be merged under a 'route' entry.")
        }
    }
    {{/has}}
    {{^notHas this 'auth-methods'}}

    {{/notHas}}
{{/operation}}
}
{{/operations}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy