-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathAuthImpl.kt
589 lines (532 loc) · 22.2 KB
/
AuthImpl.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
package io.github.jan.supabase.gotrue
import io.github.jan.supabase.SupabaseClient
import io.github.jan.supabase.annotations.SupabaseExperimental
import io.github.jan.supabase.annotations.SupabaseInternal
import io.github.jan.supabase.bodyOrNull
import io.github.jan.supabase.exceptions.BadRequestRestException
import io.github.jan.supabase.exceptions.RestException
import io.github.jan.supabase.exceptions.UnauthorizedRestException
import io.github.jan.supabase.exceptions.UnknownRestException
import io.github.jan.supabase.gotrue.admin.AdminApi
import io.github.jan.supabase.gotrue.admin.AdminApiImpl
import io.github.jan.supabase.gotrue.exception.AuthRestException
import io.github.jan.supabase.gotrue.exception.AuthSessionMissingException
import io.github.jan.supabase.gotrue.exception.AuthWeakPasswordException
import io.github.jan.supabase.gotrue.mfa.MfaApi
import io.github.jan.supabase.gotrue.mfa.MfaApiImpl
import io.github.jan.supabase.gotrue.providers.AuthProvider
import io.github.jan.supabase.gotrue.providers.ExternalAuthConfigDefaults
import io.github.jan.supabase.gotrue.providers.OAuthProvider
import io.github.jan.supabase.gotrue.providers.builtin.OTP
import io.github.jan.supabase.gotrue.providers.builtin.SSO
import io.github.jan.supabase.gotrue.user.UserInfo
import io.github.jan.supabase.gotrue.user.UserSession
import io.github.jan.supabase.gotrue.user.UserUpdateBuilder
import io.github.jan.supabase.logging.d
import io.github.jan.supabase.logging.e
import io.github.jan.supabase.logging.i
import io.github.jan.supabase.putJsonObject
import io.github.jan.supabase.safeBody
import io.github.jan.supabase.supabaseJson
import io.ktor.client.call.body
import io.ktor.client.request.parameter
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.bodyAsText
import io.ktor.client.statement.request
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.datetime.Clock
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonObjectBuilder
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.encodeToJsonElement
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.put
import kotlin.time.Duration.Companion.seconds
private const val SESSION_REFRESH_THRESHOLD = 0.8
@Suppress("MagicNumber") // see #631
private val SIGNOUT_IGNORE_CODES = listOf(401, 403, 404)
@PublishedApi
internal class AuthImpl(
override val supabaseClient: SupabaseClient,
override val config: AuthConfig
) : Auth {
private val _sessionStatus = MutableStateFlow<SessionStatus>(SessionStatus.LoadingFromStorage)
override val sessionStatus: StateFlow<SessionStatus> = _sessionStatus.asStateFlow()
internal val authScope = CoroutineScope(config.coroutineDispatcher)
override val sessionManager = config.sessionManager ?: createDefaultSessionManager()
override val codeVerifierCache = config.codeVerifierCache ?: createDefaultCodeVerifierCache()
@OptIn(SupabaseInternal::class)
internal val api = supabaseClient.authenticatedSupabaseApi(this)
override val admin: AdminApi = AdminApiImpl(this)
override val mfa: MfaApi = MfaApiImpl(this)
var sessionJob: Job? = null
override val isAutoRefreshRunning: Boolean
get() = sessionJob?.isActive == true
override val serializer = config.serializer ?: supabaseClient.defaultSerializer
override val apiVersion: Int
get() = Auth.API_VERSION
override val pluginKey: String
get() = Auth.key
init {
if(supabaseClient.accessToken != null) error("The Auth plugin is not available when using a custom access token provider. Please uninstall the Auth plugin.")
}
override fun init() {
setupPlatform()
if (config.autoLoadFromStorage) {
authScope.launch {
Auth.logger.i {
"Trying to load latest session from storage."
}
val successful = loadFromStorage()
if (successful) {
Auth.logger.i {
"Successfully loaded session from storage!"
}
} else {
Auth.logger.i {
"No session found."
}
_sessionStatus.value = SessionStatus.NotAuthenticated(false)
}
}
} else {
_sessionStatus.value = SessionStatus.NotAuthenticated(false)
}
}
override suspend fun <C, R, Provider : AuthProvider<C, R>> signInWith(
provider: Provider,
redirectUrl: String?,
config: (C.() -> Unit)?
) = provider.login(supabaseClient, {
importSession(it, source = SessionSource.SignIn(provider))
}, redirectUrl, config)
override suspend fun signInAnonymously(data: JsonObject?, captchaToken: String?) {
val response = api.postJson("signup", buildJsonObject {
data?.let { put("data", it) }
captchaToken?.let(::putCaptchaToken)
})
val session = response.safeBody<UserSession>()
importSession(session, source = SessionSource.AnonymousSignIn)
}
override suspend fun <C, R, Provider : AuthProvider<C, R>> signUpWith(
provider: Provider,
redirectUrl: String?,
config: (C.() -> Unit)?
): R? = provider.signUp(supabaseClient, {
importSession(it, source = SessionSource.SignUp(provider))
}, redirectUrl, config)
override suspend fun linkIdentity(
provider: OAuthProvider,
redirectUrl: String?,
config: ExternalAuthConfigDefaults.() -> Unit
): String? {
val automaticallyOpen = ExternalAuthConfigDefaults().apply(config).automaticallyOpenUrl
val fetchUrl: suspend (String?) -> String = { redirectTo: String? ->
val url = getOAuthUrl(provider, redirectTo, "user/identities/authorize", config)
val response = api.rawRequest(url) {
method = HttpMethod.Get
}
response.request.url.toString()
}
if(!automaticallyOpen) {
return fetchUrl(redirectUrl ?: "")
}
startExternalAuth(
redirectUrl = redirectUrl,
getUrl = {
fetchUrl(it)
},
onSessionSuccess = {
importSession(it, source = SessionSource.UserIdentitiesChanged(it))
}
)
return null
}
override suspend fun unlinkIdentity(identityId: String, updateLocalUser: Boolean) {
api.delete("user/identities/$identityId")
if (updateLocalUser) {
val session = currentSessionOrNull() ?: return
val newUser = session.user?.copy(identities = session.user.identities?.filter { it.identityId != identityId })
val newSession = session.copy(user = newUser)
_sessionStatus.value = SessionStatus.Authenticated(newSession, SessionSource.UserIdentitiesChanged(session))
}
}
override suspend fun retrieveSSOUrl(
redirectUrl: String?,
config: SSO.Config.() -> Unit
): SSO.Result {
val createdConfig = SSO.Config().apply(config)
require((createdConfig.domain != null && createdConfig.domain!!.isNotBlank()) || (createdConfig.providerId != null && createdConfig.providerId!!.isNotBlank())) {
"Either domain or providerId must be set"
}
require(createdConfig.domain == null || createdConfig.providerId == null) {
"Either domain or providerId must be set, not both"
}
val codeChallenge: String? = preparePKCEIfEnabled()
return api.postJson("sso", buildJsonObject {
redirectUrl?.let { put("redirect_to", it) }
createdConfig.captchaToken?.let(::putCaptchaToken)
codeChallenge?.let(::putCodeChallenge)
createdConfig.domain?.let {
put("domain", it)
}
createdConfig.providerId?.let {
put("provider_id", it)
}
}).body()
}
override suspend fun updateUser(
updateCurrentUser: Boolean,
redirectUrl: String?,
config: UserUpdateBuilder.() -> Unit
): UserInfo {
val updateBuilder = UserUpdateBuilder(serializer = serializer).apply(config)
val codeChallenge = preparePKCEIfEnabled()
val body = buildJsonObject {
putJsonObject(supabaseJson.encodeToJsonElement(updateBuilder).jsonObject)
codeChallenge?.let(::putCodeChallenge)
}.toString()
val response = api.putJson("user", body) {
redirectUrl?.let { url.parameters.append("redirect_to", it) }
}
val userInfo = response.safeBody<UserInfo>()
if (updateCurrentUser && sessionStatus.value is SessionStatus.Authenticated) {
val newSession =
(sessionStatus.value as SessionStatus.Authenticated).session.copy(user = userInfo)
if (this.config.autoSaveToStorage) {
sessionManager.saveSession(newSession)
}
_sessionStatus.value = SessionStatus.Authenticated(newSession, SessionSource.UserChanged(newSession))
}
return userInfo
}
private suspend fun resend(type: String, body: JsonObjectBuilder.() -> Unit) {
api.postJson("resend", buildJsonObject {
put("type", type)
putJsonObject(buildJsonObject(body))
})
}
override suspend fun resendEmail(type: OtpType.Email, email: String, captchaToken: String?) =
resend(type.type) {
put("email", email)
captchaToken?.let(::putCaptchaToken)
}
override suspend fun resendPhone(
type: OtpType.Phone,
phone: String,
captchaToken: String?
) = resend(type.type) {
put("phone", phone)
captchaToken?.let(::putCaptchaToken)
}
override suspend fun resetPasswordForEmail(
email: String,
redirectUrl: String?,
captchaToken: String?
) {
require(email.isNotBlank()) {
"Email must not be blank"
}
val codeChallenge = preparePKCEIfEnabled()
val body = buildJsonObject {
put("email", email)
captchaToken?.let(::putCaptchaToken)
codeChallenge?.let(::putCodeChallenge)
}.toString()
api.postJson("recover", body) {
redirectUrl?.let { url.encodedParameters.append("redirect_to", it) }
}
}
override suspend fun reauthenticate() {
api.get("reauthenticate")
}
override suspend fun signOut(scope: SignOutScope) {
if (currentSessionOrNull() != null) {
try {
api.post("logout") {
parameter("scope", scope.name.lowercase())
}
} catch(e: RestException) {
if(e.statusCode in SIGNOUT_IGNORE_CODES) {
Auth.logger.d { "Received error code ${e.statusCode} while signing out user. This can happen if the user doesn't exist anymore or the JWT is invalid/expired. Proceeding to clean up local data..." }
} else throw e
}
Auth.logger.d { "Logged out session in Supabase" }
} else {
Auth.logger.i { "Skipping session logout as there is no session available. Proceeding to clean up local data..." }
}
if (scope != SignOutScope.OTHERS) {
clearSession()
}
Auth.logger.d { "Successfully logged out" }
}
private suspend fun verify(
type: String,
token: String?,
captchaToken: String?,
additionalData: JsonObjectBuilder.() -> Unit
) {
val body = buildJsonObject {
put("type", type)
token?.let { put("token", it) }
captchaToken?.let(::putCaptchaToken)
additionalData()
}
val response = api.postJson("verify", body)
val session = response.body<UserSession>()
importSession(session, source = SessionSource.SignIn(OTP))
}
override suspend fun verifyEmailOtp(
type: OtpType.Email,
email: String,
token: String,
captchaToken: String?
) = verify(type.type, token, captchaToken) {
put("email", email)
}
override suspend fun verifyEmailOtp(
type: OtpType.Email,
tokenHash: String,
captchaToken: String?
) = verify(type.type, null, captchaToken) {
put("token_hash", tokenHash)
}
override suspend fun verifyPhoneOtp(
type: OtpType.Phone,
phone: String,
token: String,
captchaToken: String?
) = verify(type.type, token, captchaToken) {
put("phone", phone)
}
override suspend fun retrieveUser(jwt: String): UserInfo {
val response = api.get("user") {
headers["Authorization"] = "Bearer $jwt"
}
val body = response.bodyAsText()
return supabaseJson.decodeFromString(body)
}
override suspend fun retrieveUserForCurrentSession(updateSession: Boolean): UserInfo {
val user = retrieveUser(currentAccessTokenOrNull() ?: error("No session found"))
if (updateSession) {
val session = currentSessionOrNull() ?: error("No session found")
val newStatus = SessionStatus.Authenticated(session.copy(user = user), SessionSource.UserChanged(currentSessionOrNull() ?: error("Session shouldn't be null")))
_sessionStatus.value = newStatus
if (config.autoSaveToStorage) sessionManager.saveSession(newStatus.session)
}
return user
}
override suspend fun exchangeCodeForSession(code: String, saveSession: Boolean): UserSession {
val codeVerifier = codeVerifierCache.loadCodeVerifier()
val session = api.postJson("token?grant_type=pkce", buildJsonObject {
put("auth_code", code)
put("code_verifier", codeVerifier)
}) {
headers.remove("Authorization")
}.safeBody<UserSession>()
codeVerifierCache.deleteCodeVerifier()
if (saveSession) {
importSession(session, source = SessionSource.External)
}
return session
}
override suspend fun refreshSession(refreshToken: String): UserSession {
Auth.logger.d {
"Refreshing session"
}
val body = buildJsonObject {
put("refresh_token", refreshToken)
}
val response = api.postJson("token?grant_type=refresh_token", body) {
headers.remove("Authorization")
}
return response.safeBody("GoTrue#refreshSession")
}
override suspend fun refreshCurrentSession() {
val newSession = refreshSession(
currentSessionOrNull()?.refreshToken
?: error("No refresh token found in current session")
)
importSession(newSession, source = SessionSource.Refresh(currentSessionOrNull() ?: error("No session found")))
}
override suspend fun importSession(
session: UserSession,
autoRefresh: Boolean,
source: SessionSource
) {
if (!autoRefresh) {
if (session.refreshToken.isNotBlank() && session.expiresIn != 0L && config.autoSaveToStorage) {
sessionManager.saveSession(session)
}
_sessionStatus.value = SessionStatus.Authenticated(session, source)
return
}
if (session.expiresAt <= Clock.System.now()) {
tryImportingSession(
{ handleExpiredSession(session, config.alwaysAutoRefresh) },
{ importSession(session) }
)
} else {
if (config.autoSaveToStorage) sessionManager.saveSession(session)
_sessionStatus.value = SessionStatus.Authenticated(session, source)
sessionJob?.cancel()
sessionJob = authScope.launch {
delayBeforeExpiry(session)
launch {
tryImportingSession(
{ handleExpiredSession(session) },
{ importSession(session, source = source) }
)
}
}
}
}
private suspend fun tryImportingSession(
importRefreshedSession: suspend () -> Unit,
retry: suspend () -> Unit
) {
try {
importRefreshedSession()
} catch (e: RestException) {
clearSession()
Auth.logger.e(e) { "Couldn't refresh session. The refresh token may have been revoked." }
} catch (e: Exception) {
Auth.logger.e(e) { "Couldn't reach supabase. Either the address doesn't exist or the network might not be on. Retrying in ${config.retryDelay}" }
_sessionStatus.value = SessionStatus.NetworkError
delay(config.retryDelay)
retry()
}
}
private suspend fun delayBeforeExpiry(session: UserSession) {
val timeAtBeginningOfSession = session.expiresAt - session.expiresIn.seconds
// 80% of the way to session.expiresAt
val targetRefreshTime = timeAtBeginningOfSession + (session.expiresIn.seconds * SESSION_REFRESH_THRESHOLD)
val delayDuration = targetRefreshTime - Clock.System.now()
// if the delayDuration is negative, delay() will not delay
delay(delayDuration)
}
private suspend fun handleExpiredSession(session: UserSession, autoRefresh: Boolean = true) {
Auth.logger.d {
"Session expired. Refreshing session..."
}
val newSession = refreshSession(session.refreshToken)
importSession(newSession, autoRefresh, SessionSource.Refresh(session))
}
override suspend fun startAutoRefreshForCurrentSession() =
importSession(currentSessionOrNull() ?: error("No session found"), true, (sessionStatus.value as SessionStatus.Authenticated).source)
override fun stopAutoRefreshForCurrentSession() {
sessionJob?.cancel()
sessionJob = null
}
override suspend fun loadFromStorage(autoRefresh: Boolean): Boolean {
val session = sessionManager.loadSession()
session?.let {
importSession(it, autoRefresh, SessionSource.Storage)
}
return session != null
}
override suspend fun close() {
authScope.cancel()
}
override suspend fun parseErrorResponse(response: HttpResponse): RestException {
val errorBody =
response.bodyOrNull<GoTrueErrorResponse>() ?: GoTrueErrorResponse("Unknown error", "")
checkErrorCodes(errorBody, response)?.let { return it }
return when (response.status) {
HttpStatusCode.Unauthorized -> UnauthorizedRestException(
errorBody.error ?: "Unauthorized",
response,
errorBody.description
)
HttpStatusCode.BadRequest -> BadRequestRestException(
errorBody.error ?: "Bad Request",
response,
errorBody.description
)
HttpStatusCode.UnprocessableEntity -> BadRequestRestException(
errorBody.error ?: "Unprocessable Entity",
response,
errorBody.description
)
else -> UnknownRestException(errorBody.error ?: "Unknown Error", response)
}
}
private fun checkErrorCodes(error: GoTrueErrorResponse, response: HttpResponse): RestException? {
return when (error.error) {
AuthWeakPasswordException.CODE -> AuthWeakPasswordException(error.description, response.status.value, error.weakPassword?.reasons ?: emptyList())
AuthSessionMissingException.CODE -> {
authScope.launch {
Auth.logger.e { "Received session not found api error. Clearing session..." }
clearSession()
}
AuthSessionMissingException(response.status.value)
}
else -> {
error.error?.let { AuthRestException(it, error.description, response.status.value) }
}
}
}
@OptIn(SupabaseExperimental::class)
override fun getOAuthUrl(
provider: OAuthProvider,
redirectUrl: String?,
url: String,
additionalConfig: ExternalAuthConfigDefaults.() -> Unit
): String {
val config = ExternalAuthConfigDefaults().apply(additionalConfig)
val codeChallenge = preparePKCEIfEnabled()
codeChallenge?.let {
config.queryParams["code_challenge"] = it
config.queryParams["code_challenge_method"] = PKCEConstants.CHALLENGE_METHOD
}
return resolveUrl(buildString {
append("$url?provider=${provider.name}&redirect_to=$redirectUrl")
if (config.scopes.isNotEmpty()) append("&scopes=${config.scopes.joinToString("+")}")
if (config.queryParams.isNotEmpty()) {
for ((key, value) in config.queryParams) {
append("&$key=$value")
}
}
})
}
override suspend fun clearSession() {
codeVerifierCache.deleteCodeVerifier()
sessionManager.deleteSession()
sessionJob?.cancel()
_sessionStatus.value = SessionStatus.NotAuthenticated(true)
sessionJob = null
}
override suspend fun awaitInitialization() {
sessionStatus.first { it !is SessionStatus.LoadingFromStorage }
}
fun resetLoadingState() {
_sessionStatus.value = SessionStatus.LoadingFromStorage
}
/**
* Prepares PKCE if enabled and returns the code challenge.
*/
private fun preparePKCEIfEnabled(): String? {
if (this.config.flowType != FlowType.PKCE) return null
val codeVerifier = generateCodeVerifier()
authScope.launch {
supabaseClient.auth.codeVerifierCache.saveCodeVerifier(codeVerifier)
}
return generateCodeChallenge(codeVerifier)
}
}
@SupabaseInternal
expect fun Auth.setupPlatform()
@SupabaseInternal
expect fun Auth.createDefaultSessionManager(): SessionManager
@SupabaseInternal
expect fun Auth.createDefaultCodeVerifierCache(): CodeVerifierCache