/
OAuth2.kt
421 lines (384 loc) · 14.9 KB
/
OAuth2.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package io.ktor.server.auth
import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.http.auth.*
import io.ktor.http.content.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.util.*
import io.ktor.util.pipeline.*
import kotlinx.coroutines.*
import org.json.simple.*
import org.slf4j.*
import java.io.*
import java.net.*
private val Logger: Logger = LoggerFactory.getLogger("io.ktor.auth.oauth")
internal suspend fun PipelineContext<Unit, ApplicationCall>.oauth2(
client: HttpClient,
dispatcher: CoroutineDispatcher,
providerLookup: ApplicationCall.() -> OAuthServerSettings?,
urlProvider: ApplicationCall.(OAuthServerSettings) -> String
) {
val provider = call.providerLookup()
if (provider is OAuthServerSettings.OAuth2ServerSettings) {
val token = call.oauth2HandleCallback()
val callbackRedirectUrl = call.urlProvider(provider)
if (token == null) {
call.redirectAuthenticateOAuth2(
provider,
callbackRedirectUrl,
state = provider.nonceManager.newNonce(),
scopes = provider.defaultScopes,
interceptor = provider.authorizeUrlInterceptor
)
} else {
withContext(dispatcher) {
try {
val accessToken = oauth2RequestAccessToken(client, provider, callbackRedirectUrl, token)
call.authentication.principal(accessToken)
} catch (cause: OAuth2Exception.InvalidGrant) {
Logger.trace("Redirected to OAuth2 server due to error invalid_grant: {}", cause.message)
call.redirectAuthenticateOAuth2(
provider,
callbackRedirectUrl,
state = provider.nonceManager.newNonce(),
scopes = provider.defaultScopes,
interceptor = provider.authorizeUrlInterceptor
)
}
}
}
}
}
internal suspend fun ApplicationCall.oauth2HandleCallback(): OAuthCallback.TokenSingle? {
val params = when (request.contentType()) {
ContentType.Application.FormUrlEncoded -> receiveParameters()
else -> parameters
}
val code = params[OAuth2RequestParameters.Code]
val state = params[OAuth2RequestParameters.State]
return when {
code != null && state != null -> OAuthCallback.TokenSingle(code, state)
else -> null
}
}
internal suspend fun ApplicationCall.redirectAuthenticateOAuth2(
settings: OAuthServerSettings.OAuth2ServerSettings,
callbackRedirectUrl: String,
state: String,
extraParameters: List<Pair<String, String>> = emptyList(),
scopes: List<String> = emptyList(),
interceptor: URLBuilder.() -> Unit
) {
redirectAuthenticateOAuth2(
authenticateUrl = settings.authorizeUrl,
callbackRedirectUrl = callbackRedirectUrl,
clientId = settings.clientId,
state = state,
scopes = scopes,
parameters = extraParameters,
interceptor = interceptor
)
}
internal suspend fun oauth2RequestAccessToken(
client: HttpClient,
settings: OAuthServerSettings.OAuth2ServerSettings,
usedRedirectUrl: String,
callbackResponse: OAuthCallback.TokenSingle,
configure: (HttpRequestBuilder.() -> Unit)? = null
): OAuthAccessTokenResponse.OAuth2 {
val interceptor: HttpRequestBuilder.() -> Unit = when (configure) {
null -> settings.accessTokenInterceptor
else -> fun HttpRequestBuilder.() {
settings.accessTokenInterceptor(this)
configure()
}
}
return oauth2RequestAccessToken(
client,
settings.requestMethod,
usedRedirectUrl,
settings.accessTokenUrl,
settings.clientId,
settings.clientSecret,
callbackResponse.state,
callbackResponse.token,
settings.extraTokenParameters,
interceptor,
settings.accessTokenRequiresBasicAuth,
settings.nonceManager,
settings.passParamsInURL
)
}
private suspend fun ApplicationCall.redirectAuthenticateOAuth2(
authenticateUrl: String,
callbackRedirectUrl: String,
clientId: String,
state: String,
scopes: List<String>,
parameters: List<Pair<String, String>>,
interceptor: URLBuilder.() -> Unit
) {
val url = URLBuilder()
url.takeFrom(URI(authenticateUrl))
url.parameters.apply {
append(OAuth2RequestParameters.ClientId, clientId)
append(OAuth2RequestParameters.RedirectUri, callbackRedirectUrl)
if (scopes.isNotEmpty()) {
append(OAuth2RequestParameters.Scope, scopes.joinToString(" "))
}
append(OAuth2RequestParameters.State, state)
append(OAuth2RequestParameters.ResponseType, "code")
parameters.forEach { (k, v) -> append(k, v) }
}
interceptor(url)
return respondRedirect(url.buildString())
}
private suspend fun oauth2RequestAccessToken(
client: HttpClient,
method: HttpMethod,
usedRedirectUrl: String?,
baseUrl: String,
clientId: String,
clientSecret: String,
state: String?,
code: String?,
extraParameters: List<Pair<String, String>> = emptyList(),
configure: HttpRequestBuilder.() -> Unit = {},
useBasicAuth: Boolean = false,
nonceManager: NonceManager,
passParamsInURL: Boolean = false,
grantType: String = OAuthGrantTypes.AuthorizationCode
): OAuthAccessTokenResponse.OAuth2 {
if (!nonceManager.verifyNonce(state.orEmpty())) {
throw OAuth2Exception.InvalidNonce()
}
val request = HttpRequestBuilder()
request.url.takeFrom(URI(baseUrl))
val urlParameters = ParametersBuilder().apply {
append(OAuth2RequestParameters.ClientId, clientId)
append(OAuth2RequestParameters.ClientSecret, clientSecret)
append(OAuth2RequestParameters.GrantType, grantType)
if (state != null) {
append(OAuth2RequestParameters.State, state)
}
if (code != null) {
append(OAuth2RequestParameters.Code, code)
}
if (usedRedirectUrl != null) {
append(OAuth2RequestParameters.RedirectUri, usedRedirectUrl)
}
extraParameters.forEach { (k, v) -> append(k, v) }
}.build()
when (method) {
HttpMethod.Get -> request.url.parameters.appendAll(urlParameters)
HttpMethod.Post -> {
if (passParamsInURL) {
request.url.parameters.appendAll(urlParameters)
} else {
request.setBody(
TextContent(
urlParameters.formUrlEncode(),
ContentType.Application.FormUrlEncoded
)
)
}
}
else -> throw UnsupportedOperationException("Method $method is not supported. Use GET or POST")
}
request.apply {
this.method = method
header(
HttpHeaders.Accept,
listOf(ContentType.Application.FormUrlEncoded, ContentType.Application.Json).joinToString(",")
)
if (useBasicAuth) {
header(
HttpHeaders.Authorization,
HttpAuthHeader.Single(
AuthScheme.Basic,
"$clientId:$clientSecret".toByteArray(Charsets.ISO_8859_1).encodeBase64()
).render()
)
}
configure()
}
val response = client.request(request)
val body = response.bodyAsText()
val (contentType, content) = try {
if (response.status == HttpStatusCode.NotFound) {
throw IOException("Access token query failed with http status 404 for the page $baseUrl")
}
val contentType = response.headers[HttpHeaders.ContentType]?.let { ContentType.parse(it) } ?: ContentType.Any
Pair(contentType, body)
} catch (ioe: IOException) {
throw ioe
} catch (t: Throwable) {
throw IOException("Failed to acquire request token due to wrong content: $body", t)
}
val contentDecodeResult = Result.runCatching { decodeContent(content, contentType) }
val errorCode = contentDecodeResult.map { it[OAuth2ResponseParameters.Error] }
// try error code first
errorCode.getOrNull()?.let {
throwOAuthError(it, contentDecodeResult.getOrThrow())
}
// ensure status code is successful
if (!response.status.isSuccess()) {
throw IOException("Access token query failed with http status ${response.status} for the page $baseUrl")
}
// will fail if content decode failed but status is OK
val contentDecoded = contentDecodeResult.getOrThrow()
// finally, extract access token
return OAuthAccessTokenResponse.OAuth2(
accessToken = contentDecoded[OAuth2ResponseParameters.AccessToken]
?: throw OAuth2Exception.MissingAccessToken(),
tokenType = contentDecoded[OAuth2ResponseParameters.TokenType] ?: "",
state = state,
expiresIn = contentDecoded[OAuth2ResponseParameters.ExpiresIn]?.toLong() ?: 0L,
refreshToken = contentDecoded[OAuth2ResponseParameters.RefreshToken],
extraParameters = contentDecoded
)
}
private fun decodeContent(content: String, contentType: ContentType): Parameters = when {
contentType.match(ContentType.Application.FormUrlEncoded) -> content.parseUrlEncodedParameters()
contentType.match(ContentType.Application.Json) -> Parameters.build {
(JSONValue.parseWithException(content) as JSONObject).forEach {
append(it.key.toString(), it.value.toString())
}
} // TODO better json handling
// TODO text/xml
else -> {
// some servers may respond with wrong content type so we have to try to guess
when {
content.startsWith("{") && content.trim().endsWith("}") -> decodeContent(
content.trim(),
ContentType.Application.Json
)
content.matches("([a-zA-Z\\d_-]+=[^=&]+&?)+".toRegex()) -> decodeContent(
content,
ContentType.Application.FormUrlEncoded
) // TODO too risky, isn't it?
else -> throw IOException("unsupported content type $contentType")
}
}
}
/**
* Implements Resource Owner Password Credentials Grant.
*
* Takes [UserPasswordCredential] and validates it using OAuth2 sequence, provides [OAuthAccessTokenResponse.OAuth2] if succeeds.
*/
public suspend fun verifyWithOAuth2(
credential: UserPasswordCredential,
client: HttpClient,
settings: OAuthServerSettings.OAuth2ServerSettings
): OAuthAccessTokenResponse.OAuth2 {
return oauth2RequestAccessToken(
client, HttpMethod.Post,
usedRedirectUrl = null,
baseUrl = settings.accessTokenUrl,
clientId = settings.clientId,
clientSecret = settings.clientSecret,
code = null,
state = null,
configure = settings.accessTokenInterceptor,
extraParameters = listOf(
OAuth2RequestParameters.UserName to credential.name,
OAuth2RequestParameters.Password to credential.password
),
useBasicAuth = true,
nonceManager = settings.nonceManager,
passParamsInURL = settings.passParamsInURL,
grantType = OAuthGrantTypes.Password
)
}
/**
* List of OAuth2 request parameters for both peers.
*/
@Suppress("KDocMissingDocumentation")
public object OAuth2RequestParameters {
public const val ClientId: String = "client_id"
public const val Scope: String = "scope"
public const val ClientSecret: String = "client_secret"
public const val GrantType: String = "grant_type"
public const val Code: String = "code"
public const val State: String = "state"
public const val RedirectUri: String = "redirect_uri"
public const val ResponseType: String = "response_type"
public const val UserName: String = "username"
public const val Password: String = "password"
}
/**
* List of OAuth2 server response parameters.
*/
@Suppress("KDocMissingDocumentation")
public object OAuth2ResponseParameters {
public const val AccessToken: String = "access_token"
public const val TokenType: String = "token_type"
public const val ExpiresIn: String = "expires_in"
public const val RefreshToken: String = "refresh_token"
public const val Error: String = "error"
public const val ErrorDescription: String = "error_description"
}
private fun throwOAuthError(errorCode: String, parameters: Parameters): Nothing {
val errorDescription = parameters.get("error_description") ?: "OAuth2 Server responded with $errorCode"
throw when (errorCode) {
"invalid_grant" -> OAuth2Exception.InvalidGrant(errorDescription)
else -> OAuth2Exception.UnknownException(errorDescription, errorCode)
}
}
/**
* Represents an error during communicating to OAuth2 server.
* @property errorCode OAuth2 server replied with
*/
public sealed class OAuth2Exception(message: String, public val errorCode: String?) : Exception(message) {
/**
* Thrown when OAuth2 server responds with the "invalid_grant" error.
*/
public class InvalidGrant(message: String) : OAuth2Exception(message, "invalid_grant")
/**
* Thrown when a nonce verification failed.
*/
public class InvalidNonce : OAuth2Exception("Nonce verification failed", null)
/**
* Thrown when an OAuth2 server responds with a successful HTTP status and expected content type that was successfully
* decoded but the response doesn't contain a error code nor access token.
*/
public class MissingAccessToken : OAuth2Exception(
"OAuth2 server response is OK neither error nor access token provided",
null
)
/**
* Thrown when an OAuth2 server responds with the "unsupported_grant_type" error.
* @param grantType that was passed to the server
*/
@OptIn(ExperimentalCoroutinesApi::class)
public class UnsupportedGrantType(public val grantType: String) :
OAuth2Exception(
"OAuth2 server doesn't support grant type $grantType",
"unsupported_grant_type"
),
CopyableThrowable<UnsupportedGrantType> {
override fun createCopy(): UnsupportedGrantType = UnsupportedGrantType(grantType).also {
it.initCause(this)
}
}
/**
* Thrown when an OAuth2 server responds with [errorCode].
* @param errorCode the OAuth2 server replied with
*/
@OptIn(ExperimentalCoroutinesApi::class)
public class UnknownException(
private val details: String,
errorCode: String
) : OAuth2Exception("$details (error code = $errorCode)", errorCode), CopyableThrowable<UnknownException> {
override fun createCopy(): UnknownException = UnknownException(details, errorCode!!).also {
it.initCause(this)
}
}
}