@@ -5,13 +5,15 @@ import androidx.core.content.edit
55import androidx.core.net.toUri
66import androidx.security.crypto.EncryptedSharedPreferences
77import androidx.security.crypto.MasterKeys
8+ import com.descope.internal.others.booleanOrNull
89import com.descope.internal.others.debug
910import com.descope.internal.others.error
1011import com.descope.internal.others.optionalMap
1112import com.descope.internal.others.stringOrEmptyAsNull
1213import com.descope.internal.others.toJsonArray
1314import com.descope.internal.others.toJsonObject
1415import com.descope.internal.others.toStringList
16+ import com.descope.internal.others.toStringSet
1517import com.descope.internal.others.tryOrNull
1618import com.descope.sdk.DescopeLogger
1719import com.descope.types.DescopeUser
@@ -128,7 +130,7 @@ class SessionStorage(context: Context, private val projectId: String, logger: De
128130 get() = JSONObject ().apply {
129131 put(" sessionJwt" , sessionJwt)
130132 put(" refreshJwt" , refreshJwt)
131- put(" user" , user.toJson ())
133+ put(" user" , user.serialize ())
132134 }.toString()
133135
134136 companion object {
@@ -171,7 +173,25 @@ class EncryptedSharedPrefs(name: String, context: Context) : SessionStorage.Stor
171173
172174// Serialization
173175
174- private fun deserializeDescopeUser (json : JSONObject ): DescopeUser = json.run {
176+ internal fun deserializeDescopeUser (json : JSONObject ): DescopeUser = json.run {
177+ val authentication = optJSONObject(" authentication" ).let { json ->
178+ DescopeUser .Authentication (
179+ password = json?.optBoolean(" password" ) ? : false ,
180+ passkey = json?.optBoolean(" passkey" ) ? : false ,
181+ totp = json?.optBoolean(" totp" ) ? : false ,
182+ oauth = json?.optJSONArray(" oauth" )?.toStringSet() ? : emptySet(),
183+ sso = json?.optBoolean(" sso" ) ? : false ,
184+ scim = json?.optBoolean(" scim" ) ? : false ,
185+ )
186+ }
187+
188+ val authorization = optJSONObject(" authorization" ).let { json ->
189+ DescopeUser .Authorization (
190+ roles = json?.optJSONArray(" roles" )?.toStringSet() ? : emptySet(),
191+ ssoAppIds = json?.optJSONArray(" ssoAppIds" )?.toStringSet() ? : emptySet(),
192+ )
193+ }
194+
175195 DescopeUser (
176196 userId = getString(" userId" ),
177197 loginIds = getJSONArray(" loginIds" ).toStringList(),
@@ -186,10 +206,14 @@ private fun deserializeDescopeUser(json: JSONObject): DescopeUser = json.run {
186206 givenName = stringOrEmptyAsNull(" givenName" ),
187207 middleName = stringOrEmptyAsNull(" middleName" ),
188208 familyName = stringOrEmptyAsNull(" familyName" ),
209+ status = DescopeUser .Status .deserialize(stringOrEmptyAsNull(" status" ) ? : " enabled" ),
210+ authentication = authentication,
211+ authorization = authorization,
212+ isUpdateRequired = booleanOrNull(" isUpdateRequired" ) ? : true , // if the flag doesn't exist we've got old data without the new fields
189213 )
190214}
191215
192- private fun DescopeUser.toJson () = JSONObject ().apply {
216+ internal fun DescopeUser.serialize () = JSONObject ().apply {
193217 put(" userId" , userId)
194218 put(" loginIds" , loginIds.toJsonArray())
195219 put(" createdAt" , createdAt)
@@ -203,8 +227,25 @@ private fun DescopeUser.toJson() = JSONObject().apply {
203227 put(" givenName" , givenName)
204228 put(" middleName" , middleName)
205229 put(" familyName" , familyName)
230+ put(" authentication" , authentication.toJsonObject())
231+ put(" authorization" , authorization.toJsonObject())
232+ put(" status" , status.serialize())
233+ put(" isUpdateRequired" , isUpdateRequired)
206234}
207235
236+ private fun DescopeUser.Authentication.toJsonObject () = JSONObject ().apply {
237+ put(" password" , password)
238+ put(" passkey" , passkey)
239+ put(" totp" , totp)
240+ put(" oauth" , oauth.toJsonArray())
241+ put(" sso" , sso)
242+ put(" scim" , scim)
243+ }
244+
245+ private fun DescopeUser.Authorization.toJsonObject () = JSONObject ().apply {
246+ put(" roles" , roles.toJsonArray())
247+ put(" ssoAppIds" , ssoAppIds.toJsonArray())
248+ }
208249private fun createEncryptedStore (context : Context , projectId : String , logger : DescopeLogger ? ): SessionStorage .Store {
209250 try {
210251 val storage = EncryptedSharedPrefs (projectId, context)
0 commit comments