[Kotlin-client] Authentication support (#3722)

* Handle and document authorization for kotlin client

* Regenerate clients and tests

* Remove testing files
This commit is contained in:
Armand Mégrot
2019-08-22 15:32:07 +02:00
committed by William Cheng
parent 62ca0c78ff
commit 4898ffa4e9
80 changed files with 1012 additions and 370 deletions

View File

@@ -54,7 +54,29 @@ Name | Type | Description | Notes
### Authorization
{{^authMethods}}No authorization required{{/authMethods}}{{#authMethods}}[{{name}}](../README.md#{{name}}){{^-last}}, {{/-last}}{{/authMethods}}
{{^authMethods}}No authorization required{{/authMethods}}
{{#authMethods}}
{{#isApiKey}}
Configure {{name}}:
ApiClient.apiKey["{{keyParamName}}"] = ""
ApiClient.apiKeyPrefix["{{keyParamName}}"] = ""
{{/isApiKey}}
{{#isBasic}}
{{^isBasicBearer}}
Configure {{name}}:
ApiClient.username = ""
ApiClient.password = ""
{{/isBasicBearer}}
{{#isBasicBearer}}
Configure {{name}}:
ApiClient.accessToken = ""
{{/isBasicBearer}}
{{/isBasic}}
{{#isOAuth}}
Configure {{name}}:
ApiClient.accessToken = ""
{{/isOAuth}}
{{/authMethods}}
### HTTP request headers

View File

@@ -15,11 +15,18 @@ open class ApiClient(val baseUrl: String) {
companion object {
protected const val ContentType = "Content-Type"
protected const val Accept = "Accept"
protected const val Authorization = "Authorization"
protected const val JsonMediaType = "application/json"
protected const val FormDataMediaType = "multipart/form-data"
protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded"
protected const val XmlMediaType = "application/xml"
val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
var username: String? = null
var password: String? = null
var accessToken: String? = null
@JvmStatic
val client: OkHttpClient by lazy {
builder.build()
@@ -65,8 +72,61 @@ open class ApiClient(val baseUrl: String) {
}
}
{{#hasAuthMethods}}
protected fun updateAuthParams(requestConfig: RequestConfig) {
{{#authMethods}}
{{#isApiKey}}
{{#isKeyInHeader}}
if (requestConfig.headers["{{keyParamName}}"].isNullOrEmpty()) {
{{/isKeyInHeader}}
{{#isKeyInQuery}}
if (requestConfig.query["{{keyParamName}}"].isNullOrEmpty()) {
{{/isKeyInQuery}}
if (apiKeyPrefix["{{keyParamName}}"] != null) {
{{#isKeyInHeader}}
requestConfig.headers["{{keyParamName}}"] = apiKeyPrefix["{{keyParamName}}"] + " " + apiKey["{{keyParamName}}"]
{{/isKeyInHeader}}
{{#isKeyInQuery}}
requestConfig.query["{{keyParamName}}"] = apiKeyPrefix["{{keyParamName}}"] + " " + apiKey["{{keyParamName}}"]
{{/isKeyInQuery}}
} else {
{{#isKeyInHeader}}
requestConfig.headers["{{keyParamName}}"] = apiKey["{{keyParamName}}"]
{{/isKeyInHeader}}
{{#isKeyInQuery}}
requestConfig.query["{{keyParamName}}"] = apiKey["{{keyParamName}}"]
{{/isKeyInQuery}}
}
}
{{/isApiKey}}
{{#isBasic}}
{{^isBasicBearer}}
if (requestConfig.headers[Authorization].isNullOrEmpty()) {
requestConfig.headers[Authorization] = Credentials.basic(username, password)
}
{{/isBasicBearer}}
{{#isBasicBearer}}
if (requestConfig.headers[Authorization].isNullOrEmpty()) {
requestConfig.headers[Authorization] = "Bearer " + accessToken
}
{{/isBasicBearer}}
{{/isBasic}}
{{#isOAuth}}
if (requestConfig.headers[Authorization].isNullOrEmpty()) {
requestConfig.headers[Authorization] = "Bearer " + accessToken
}
{{/isOAuth}}
{{/authMethods}}
}
{{/hasAuthMethods}}
protected inline fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.")
{{#hasAuthMethods}}
// take authMethod from operation
updateAuthParams(requestConfig)
{{/hasAuthMethods}}
val url = httpUrl.newBuilder()
.addPathSegments(requestConfig.path.trimStart('/'))