Set default value for requireAuthentication (#155)

* Set the default value of requireAuthentication based on the security requirements

* Improve requireAuthentication code readability
This commit is contained in:
Niels van Velzen 2020-12-08 20:26:48 +01:00 committed by GitHub
parent 2c4cdf5630
commit 280ce75590
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 1 deletions

View File

@ -2,6 +2,7 @@ root = true
[*]
insert_final_newline = true
end_of_line = lf
[{*.kts, *.kt}]
charset = utf-8

View File

@ -54,7 +54,7 @@ class OperationUrlBuilder(
.forEach { parameter -> addParameter(buildParameter(parameter)) }
ParameterSpec.builder("includeCredentials", Boolean::class).apply {
defaultValue("%L", "false")
defaultValue("%L", data.requireAuthentication)
addKdoc("%L", Strings.INCLUDE_CREDENTIALS_DESCRIPTION)
}.build().let(::addParameter)

View File

@ -8,6 +8,7 @@ import net.pearx.kasechange.toCamelCase
import org.jellyfin.openapi.builder.Builder
import org.jellyfin.openapi.builder.api.ApiNameBuilder
import org.jellyfin.openapi.constants.MimeType
import org.jellyfin.openapi.constants.Security
import org.jellyfin.openapi.constants.Strings
import org.jellyfin.openapi.hooks.ApiTypePath
import org.jellyfin.openapi.model.ApiService
@ -73,12 +74,19 @@ class OpenApiApiServicesBuilder(
if (returnType == Unit::class.asTypeName() && "200" in operation.responses)
println("Missing return-type for operation $operationName (status-codes: ${operation.responses.keys})")
val requireAuthentication = operation.security
?.firstOrNull { requirement -> requirement.containsKey(Security.SECURITY_SCHEME) }
?.get(Security.SECURITY_SCHEME)
?.any(Security.AUTHENTICATION_POLICIES::contains)
?: false
operations[serviceName]!! += ApiServiceOperation(
name = operationName,
description = operation.description ?: operation.summary,
deprecated = operation.deprecated == true,
pathTemplate = path,
method = method,
requireAuthentication = requireAuthentication,
returnType = returnType,
pathParameters = pathParameters,
queryParameters = queryParameters,

View File

@ -0,0 +1,6 @@
package org.jellyfin.openapi.constants
object Security {
const val SECURITY_SCHEME = "CustomAuthentication"
val AUTHENTICATION_POLICIES = arrayOf("DefaultAuthorization", "RequiresElevation")
}

View File

@ -8,6 +8,7 @@ data class ApiServiceOperation(
val deprecated: Boolean,
val pathTemplate: String,
val method: HttpMethod,
val requireAuthentication: Boolean,
val returnType: TypeName,
val pathParameters: Collection<ApiServiceOperationParameter> = emptyList(),
val queryParameters: Collection<ApiServiceOperationParameter> = emptyList(),