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

io.javalin.openapi.Security.kt Maven / Gradle / Ivy

package io.javalin.openapi

import com.fasterxml.jackson.annotation.JsonIgnore

data class Security @JvmOverloads constructor(
    val name: String,
    val scopes: MutableList = mutableListOf()
) {

    fun withScope(scope: String): Security = also {
        scopes.add(scope)
    }

}

interface SecurityScheme {
    val type: String
}

abstract class HttpAuth(val scheme: String) : SecurityScheme {
    override val type: String = "http"
}

class BasicAuth : HttpAuth(scheme = "basic")

class BearerAuth : HttpAuth(scheme = "bearer")

open class ApiKeyAuth(
    open val `in`: String = "header",
    open val name: String = "X-API-Key"
) : SecurityScheme {
    override val type: String = "apiKey"
}

class CookieAuth @JvmOverloads constructor(
    override val name: String,
    override val `in`: String = "cookie"
) : ApiKeyAuth()

class OpenID (val openIdConnectUrl: String) : SecurityScheme {
    override val type: String = "openIdConnect"
}

class OAuth2 @JvmOverloads constructor(
    val description: String,
    val flows: MutableMap> = mutableMapOf(),
) : SecurityScheme {
    override val type: String = "oauth2"

    fun withFlow(flow: OAuth2Flow<*>): OAuth2 = also {
        flows[flow.flowType] = flow
    }
}

interface OAuth2Flow> {
    @get:JsonIgnore
    val flowType: String
    val scopes: MutableMap

    @Suppress("UNCHECKED_CAST")
    fun withScope(scope: String, description: String): I = also {
        scopes[scope] = description
    } as I
}

class AuthorizationCodeFlow @JvmOverloads constructor(
    val authorizationUrl: String,
    val tokenUrl: String,
    override val scopes: MutableMap = mutableMapOf()
) : OAuth2Flow {
    override val flowType: String = "authorizationCode"
}

class ImplicitFlow @JvmOverloads constructor(
    val authorizationUrl: String,
    override val scopes: MutableMap = mutableMapOf()
) : OAuth2Flow {
    override val flowType: String = "implicit"
}

class PasswordFlow @JvmOverloads constructor(
    val tokenUrl: String,
    override val scopes: MutableMap = mutableMapOf()
) : OAuth2Flow {
    override val flowType: String = "password"
}

class ClientCredentials @JvmOverloads constructor(
    val tokenUrl: String,
    override val scopes: MutableMap = mutableMapOf()
) : OAuth2Flow {
    override val flowType: String = "clientCredentials "
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy