-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathShared.kt
More file actions
78 lines (71 loc) · 2.86 KB
/
Copy pathShared.kt
File metadata and controls
78 lines (71 loc) · 2.86 KB
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
package com.descope.internal.routes
import androidx.core.net.toUri
import com.descope.internal.http.DescopeClient
import com.descope.internal.http.JwtServerResponse
import com.descope.internal.http.MaskedAddressServerResponse
import com.descope.internal.http.UserResponse
import com.descope.internal.others.with
import com.descope.sdk.DescopeLogger
import com.descope.session.Token
import com.descope.types.AuthenticationResponse
import com.descope.types.DeliveryMethod
import com.descope.types.DescopeException
import com.descope.types.DescopeUser
import com.descope.types.RefreshResponse
internal interface Route {
val client: DescopeClient
fun log(level: DescopeLogger.Level, message: String, vararg values: Any) {
client.config.logger?.log(level, message, *values)
}
}
internal fun UserResponse.convert(): DescopeUser = DescopeUser(
userId = userId,
loginIds = loginIds,
createdAt = createdTime,
name = name,
picture = picture?.run { this.toUri() },
email = email,
isVerifiedEmail = verifiedEmail,
phone = phone,
isVerifiedPhone = verifiedPhone,
customAttributes = customAttributes,
givenName = givenName,
middleName = middleName,
familyName = familyName,
status = DescopeUser.Status.deserialize(status),
authentication = DescopeUser.Authentication(
passkey = webauthn,
password = password,
totp = totp,
oauth = oauthProviders.keys.toSet(),
sso = saml,
scim = scim,
),
authorization = DescopeUser.Authorization(
roles = roleNames.toSet(),
ssoAppIds = ssoAppIds.toSet(),
),
isUpdateRequired = false
)
internal fun JwtServerResponse.convert(): AuthenticationResponse {
val sessionJwt = sessionJwt ?: throw DescopeException.decodeError.with(message = "Missing session JWT")
val refreshJwt = refreshJwt ?: throw DescopeException.decodeError.with(message = "Missing refresh JWT")
val user = user ?: throw DescopeException.decodeError.with(message = "Missing user details")
return AuthenticationResponse(
refreshToken = Token(refreshJwt),
sessionToken = Token(sessionJwt),
user = user.convert(),
isFirstAuthentication = firstSeen,
)
}
internal fun JwtServerResponse.toRefreshResponse(): RefreshResponse {
val sessionJwt = sessionJwt ?: throw DescopeException.decodeError.with(message = "Missing session JWT")
return RefreshResponse(
refreshToken = refreshJwt?.run { Token(this) },
sessionToken = Token(sessionJwt),
)
}
internal fun MaskedAddressServerResponse.convert(method: DeliveryMethod) = when (method) {
DeliveryMethod.Email -> maskedEmail ?: throw DescopeException.decodeError.with(message = "masked email not received")
DeliveryMethod.Sms, DeliveryMethod.Whatsapp -> maskedPhone ?: throw DescopeException.decodeError.with(message = "masked phone not received")
}