Skip to content

Commit 750371c

Browse files
authored
feat(kotlin-client): support dynamic auth credentials for okhttp client (#23835) (#23836)
* feat(kotlin-client): support dynamic auth credentials for okhttp client (#23835) * fix(Kotlin): make the credential providers assignable and fix docs (#23822)
1 parent 265dc32 commit 750371c

61 files changed

Lines changed: 1330 additions & 433 deletions

File tree

  • modules/openapi-generator/src/main/resources/kotlin-client
  • samples/client
    • echo_api
    • others
      • kotlin-integer-enum/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-jvm-okhttp-non-ascii-headers
      • kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-jvm-okhttp-path-comments
    • petstore
      • kotlin-allOf-discriminator-kotlinx-serialization/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-allOf-discriminator/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-array-integer-enum/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-array-nullable-items/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-explicit
      • kotlin-gson
      • kotlin-jackson
      • kotlin-json-request-string
      • kotlin-jvm-ktor-gson/docs
      • kotlin-jvm-ktor-jackson/docs
      • kotlin-jvm-ktor-kotlinx_serialization/docs
      • kotlin-jvm-okhttp4-coroutines
      • kotlin-jvm-spring-2-webclient/docs
      • kotlin-jvm-spring-3-restclient/docs
      • kotlin-jvm-spring-3-webclient/docs
      • kotlin-jvm-vertx-gson/docs
      • kotlin-jvm-vertx-jackson-coroutines/docs
      • kotlin-jvm-vertx-jackson/docs
      • kotlin-jvm-vertx-moshi/docs
      • kotlin-kotlinx-datetime
      • kotlin-modelMutable
      • kotlin-moshi-codegen
      • kotlin-multiplatform-kotlinx-datetime/docs
      • kotlin-multiplatform/docs
      • kotlin-name-parameter-mappings/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-nonpublic
      • kotlin-nullable
      • kotlin-string
      • kotlin-threetenbp
      • kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-generator/src/main/resources/kotlin-client/api_doc.mustache

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,42 @@ Configure {{name}}:
6868
{{/isApiKey}}
6969
{{#isBasic}}
7070
{{#isBasicBasic}}
71-
Configure {{name}}:
72-
ApiClient.username = ""
73-
ApiClient.password = ""
71+
Configure {{name}} statically:
72+
```kotlin
73+
ApiClient.username = ""
74+
ApiClient.password = ""
75+
```
76+
{{#jvm-okhttp}}
77+
Configure {{name}} dynamically:
78+
```kotlin
79+
apiInstance.userCredentialProvider = { "user" to "pass" }
80+
```
81+
{{/jvm-okhttp}}
7482
{{/isBasicBasic}}
7583
{{#isBasicBearer}}
76-
Configure {{name}}:
77-
ApiClient.accessToken = ""
84+
Configure {{name}} statically:
85+
```kotlin
86+
ApiClient.accessToken = ""
87+
```
88+
{{#jvm-okhttp}}
89+
Configure {{name}} dynamically:
90+
```kotlin
91+
apiInstance.accessTokenProvider = { "" }
92+
```
93+
{{/jvm-okhttp}}
7894
{{/isBasicBearer}}
7995
{{/isBasic}}
8096
{{#isOAuth}}
81-
Configure {{name}}:
82-
ApiClient.accessToken = ""
97+
Configure {{name}} statically:
98+
```kotlin
99+
ApiClient.accessToken = ""
100+
```
101+
{{#jvm-okhttp}}
102+
Configure {{name}} dynamically:
103+
```kotlin
104+
apiInstance.accessTokenProvider = { "" }
105+
```
106+
{{/jvm-okhttp}}
83107
{{/isOAuth}}
84108
{{/authMethods}}
85109

@@ -89,4 +113,4 @@ Configure {{name}}:
89113
- **Accept**: {{#produces}}{{{mediaType}}}{{^-last}}, {{/-last}}{{/produces}}{{^produces}}Not defined{{/produces}}
90114

91115
{{/operation}}
92-
{{/operations}}
116+
{{/operations}}

modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ import com.squareup.moshi.adapter
148148
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val builder: OkHttpClient.Builder = OkHttpClient.Builder()
149149
}
150150

151+
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}var userCredentialsProvider: () -> Pair<String?, String?> = { username to password }
152+
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}var accessTokenProvider: () -> String? = { accessToken }
153+
151154
/**
152155
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
153156
*
@@ -460,25 +463,26 @@ import com.squareup.moshi.adapter
460463
{{#isBasic}}
461464
{{#isBasicBasic}}
462465
if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) {
463-
username?.let { username ->
464-
password?.let { password ->
465-
requestConfig.headers[AUTHORIZATION] = okhttp3.Credentials.basic(username, password)
466+
val (user, passw) = userCredentialsProvider()
467+
user?.let { user ->
468+
passw?.let { passw ->
469+
requestConfig.headers[AUTHORIZATION] = okhttp3.Credentials.basic(user, passw)
466470
}
467471
}
468472
}
469473
{{/isBasicBasic}}
470474
{{#isBasicBearer}}
471475
if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) {
472-
accessToken?.let { accessToken ->
473-
requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken"
476+
accessTokenProvider()?.let { token ->
477+
requestConfig.headers[AUTHORIZATION] = "Bearer $token"
474478
}
475479
}
476480
{{/isBasicBearer}}
477481
{{/isBasic}}
478482
{{#isOAuth}}
479483
if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) {
480-
accessToken?.let { accessToken ->
481-
requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken "
484+
accessTokenProvider()?.let { token ->
485+
requestConfig.headers[AUTHORIZATION] = "Bearer $token "
482486
}
483487
}
484488
{{/isOAuth}}

samples/client/echo_api/kotlin-jvm-okhttp/docs/AuthApi.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,15 @@ This endpoint does not need any parameter.
4545
### Authorization
4646

4747

48-
Configure http_auth:
49-
ApiClient.username = ""
50-
ApiClient.password = ""
48+
Configure http_auth statically:
49+
```kotlin
50+
ApiClient.username = ""
51+
ApiClient.password = ""
52+
```
53+
Configure http_auth dynamically:
54+
```kotlin
55+
apiInstance.userCredentialProvider = { "user" to "pass" }
56+
```
5157

5258
### HTTP request headers
5359

@@ -91,8 +97,14 @@ This endpoint does not need any parameter.
9197
### Authorization
9298

9399

94-
Configure http_bearer_auth:
95-
ApiClient.accessToken = ""
100+
Configure http_bearer_auth statically:
101+
```kotlin
102+
ApiClient.accessToken = ""
103+
```
104+
Configure http_bearer_auth dynamically:
105+
```kotlin
106+
apiInstance.accessTokenProvider = { "" }
107+
```
96108

97109
### HTTP request headers
98110

samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
119119
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
120120
}
121121

122+
var userCredentialsProvider: () -> Pair<String?, String?> = { username to password }
123+
var accessTokenProvider: () -> String? = { accessToken }
124+
122125
/**
123126
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
124127
*
@@ -357,15 +360,16 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
357360

358361
protected fun <T> updateAuthParams(requestConfig: RequestConfig<T>) {
359362
if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) {
360-
username?.let { username ->
361-
password?.let { password ->
362-
requestConfig.headers[AUTHORIZATION] = okhttp3.Credentials.basic(username, password)
363+
val (user, passw) = userCredentialsProvider()
364+
user?.let { user ->
365+
passw?.let { passw ->
366+
requestConfig.headers[AUTHORIZATION] = okhttp3.Credentials.basic(user, passw)
363367
}
364368
}
365369
}
366370
if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) {
367-
accessToken?.let { accessToken ->
368-
requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken"
371+
accessTokenProvider()?.let { token ->
372+
requestConfig.headers[AUTHORIZATION] = "Bearer $token"
369373
}
370374
}
371375
}

samples/client/echo_api/kotlin-jvm-spring-3-restclient/docs/AuthApi.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ This endpoint does not need any parameter.
4545
### Authorization
4646

4747

48-
Configure http_auth:
49-
ApiClient.username = ""
50-
ApiClient.password = ""
48+
Configure http_auth statically:
49+
```kotlin
50+
ApiClient.username = ""
51+
ApiClient.password = ""
52+
```
5153

5254
### HTTP request headers
5355

@@ -91,8 +93,10 @@ This endpoint does not need any parameter.
9193
### Authorization
9294

9395

94-
Configure http_bearer_auth:
95-
ApiClient.accessToken = ""
96+
Configure http_bearer_auth statically:
97+
```kotlin
98+
ApiClient.accessToken = ""
99+
```
96100

97101
### HTTP request headers
98102

samples/client/echo_api/kotlin-jvm-spring-3-webclient/docs/AuthApi.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ This endpoint does not need any parameter.
4545
### Authorization
4646

4747

48-
Configure http_auth:
49-
ApiClient.username = ""
50-
ApiClient.password = ""
48+
Configure http_auth statically:
49+
```kotlin
50+
ApiClient.username = ""
51+
ApiClient.password = ""
52+
```
5153

5254
### HTTP request headers
5355

@@ -91,8 +93,10 @@ This endpoint does not need any parameter.
9193
### Authorization
9294

9395

94-
Configure http_bearer_auth:
95-
ApiClient.accessToken = ""
96+
Configure http_bearer_auth statically:
97+
```kotlin
98+
ApiClient.accessToken = ""
99+
```
96100

97101
### HTTP request headers
98102

samples/client/others/kotlin-integer-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
119119
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
120120
}
121121

122+
var userCredentialsProvider: () -> Pair<String?, String?> = { username to password }
123+
var accessTokenProvider: () -> String? = { accessToken }
124+
122125
/**
123126
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
124127
*

samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/docs/PetApi.md

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,14 @@ try {
5454
### Authorization
5555

5656

57-
Configure petstore_auth:
58-
ApiClient.accessToken = ""
57+
Configure petstore_auth statically:
58+
```kotlin
59+
ApiClient.accessToken = ""
60+
```
61+
Configure petstore_auth dynamically:
62+
```kotlin
63+
apiInstance.accessTokenProvider = { "" }
64+
```
5965

6066
### HTTP request headers
6167

@@ -103,8 +109,14 @@ null (empty response body)
103109
### Authorization
104110

105111

106-
Configure petstore_auth:
107-
ApiClient.accessToken = ""
112+
Configure petstore_auth statically:
113+
```kotlin
114+
ApiClient.accessToken = ""
115+
```
116+
Configure petstore_auth dynamically:
117+
```kotlin
118+
apiInstance.accessTokenProvider = { "" }
119+
```
108120

109121
### HTTP request headers
110122

@@ -151,8 +163,14 @@ try {
151163
### Authorization
152164

153165

154-
Configure petstore_auth:
155-
ApiClient.accessToken = ""
166+
Configure petstore_auth statically:
167+
```kotlin
168+
ApiClient.accessToken = ""
169+
```
170+
Configure petstore_auth dynamically:
171+
```kotlin
172+
apiInstance.accessTokenProvider = { "" }
173+
```
156174

157175
### HTTP request headers
158176

@@ -199,8 +217,14 @@ try {
199217
### Authorization
200218

201219

202-
Configure petstore_auth:
203-
ApiClient.accessToken = ""
220+
Configure petstore_auth statically:
221+
```kotlin
222+
ApiClient.accessToken = ""
223+
```
224+
Configure petstore_auth dynamically:
225+
```kotlin
226+
apiInstance.accessTokenProvider = { "" }
227+
```
204228

205229
### HTTP request headers
206230

@@ -296,8 +320,14 @@ try {
296320
### Authorization
297321

298322

299-
Configure petstore_auth:
300-
ApiClient.accessToken = ""
323+
Configure petstore_auth statically:
324+
```kotlin
325+
ApiClient.accessToken = ""
326+
```
327+
Configure petstore_auth dynamically:
328+
```kotlin
329+
apiInstance.accessTokenProvider = { "" }
330+
```
301331

302332
### HTTP request headers
303333

@@ -347,8 +377,14 @@ null (empty response body)
347377
### Authorization
348378

349379

350-
Configure petstore_auth:
351-
ApiClient.accessToken = ""
380+
Configure petstore_auth statically:
381+
```kotlin
382+
ApiClient.accessToken = ""
383+
```
384+
Configure petstore_auth dynamically:
385+
```kotlin
386+
apiInstance.accessTokenProvider = { "" }
387+
```
352388

353389
### HTTP request headers
354390

@@ -399,8 +435,14 @@ try {
399435
### Authorization
400436

401437

402-
Configure petstore_auth:
403-
ApiClient.accessToken = ""
438+
Configure petstore_auth statically:
439+
```kotlin
440+
ApiClient.accessToken = ""
441+
```
442+
Configure petstore_auth dynamically:
443+
```kotlin
444+
apiInstance.accessTokenProvider = { "" }
445+
```
404446

405447
### HTTP request headers
406448

samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
119119
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
120120
}
121121

122+
var userCredentialsProvider: () -> Pair<String?, String?> = { username to password }
123+
var accessTokenProvider: () -> String? = { accessToken }
124+
122125
/**
123126
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
124127
*
@@ -357,8 +360,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
357360

358361
protected fun <T> updateAuthParams(requestConfig: RequestConfig<T>) {
359362
if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) {
360-
accessToken?.let { accessToken ->
361-
requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken "
363+
accessTokenProvider()?.let { token ->
364+
requestConfig.headers[AUTHORIZATION] = "Bearer $token "
362365
}
363366
}
364367
if (requestConfig.headers["api_key"].isNullOrEmpty()) {

samples/client/others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
119119
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
120120
}
121121

122+
var userCredentialsProvider: () -> Pair<String?, String?> = { username to password }
123+
var accessTokenProvider: () -> String? = { accessToken }
124+
122125
/**
123126
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
124127
*

0 commit comments

Comments
 (0)