Skip to content

Commit

Permalink
Fix: Support for presentation_definition_uri in openId4VP
Browse files Browse the repository at this point in the history
  • Loading branch information
josmilan committed Oct 11, 2024
1 parent 91fafb8 commit c5150e0
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ data class PresentationRequest(
@SerializedName("request_uri") var requestUri: String? = null,
@SerializedName("response_uri") var responseUri: String? = null,
@SerializedName("presentation_definition") var presentationDefinition: Any? = null,
@SerializedName("presentation_definition_uri") var presentationDefinitionUri: String? = null,
@SerializedName("client_metadata") var clientMetaDetails: Any? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.ewc.eudi_wallet_oidc_android.models.CredentialResponse
import com.ewc.eudi_wallet_oidc_android.models.DIDDocument
import com.ewc.eudi_wallet_oidc_android.models.IssuerWellKnownConfiguration
import com.ewc.eudi_wallet_oidc_android.models.ParResponse
import com.ewc.eudi_wallet_oidc_android.models.PresentationDefinition
import com.ewc.eudi_wallet_oidc_android.models.TokenResponse
import com.ewc.eudi_wallet_oidc_android.models.v2.DeferredCredentialRequestV2
import okhttp3.ResponseBody
Expand Down Expand Up @@ -82,6 +83,9 @@ interface ApiService {
@GET
suspend fun getPresentationDefinitionFromRequestUri(@Url url: String): Response<ResponseBody>

@GET
suspend fun getPresentationDefinitionFromPresentationDefinitionUri(@Url url: String): Response<ResponseBody>

@FormUrlEncoded
@POST("")
suspend fun sendVPToken(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class VerificationService : VerificationServiceInterface {
val nonce = Uri.parse(data).getQueryParameter("nonce")
val presentationDefinition =
Uri.parse(data).getQueryParameter("presentation_definition")
val presentationDefinitionUri = Uri.parse(data).getQueryParameter("presentation_definition_uri")
val responseType = Uri.parse(data).getQueryParameter("response_type")
val scope = Uri.parse(data).getQueryParameter("scope")
val requestUri = Uri.parse(data).getQueryParameter("request_uri")
Expand All @@ -80,6 +81,23 @@ class VerificationService : VerificationServiceInterface {
redirectUri = redirectUri,
nonce = nonce,
presentationDefinition = presentationDefinition,
presentationDefinitionUri = presentationDefinitionUri,
responseMode = responseMode,
responseType = responseType,
scope = scope,
requestUri = requestUri,
responseUri = responseUri,
clientMetaDetails = clientMetadetails
)
} else if (presentationDefinitionUri != null) {
val resolvedPresentationDefinition = getPresentationDefinitionFromDefinitionUri(presentationDefinitionUri)
return PresentationRequest(
clientId = clientId,
state = state,
redirectUri = redirectUri,
nonce = nonce,
presentationDefinition = resolvedPresentationDefinition,
presentationDefinitionUri = presentationDefinitionUri,
responseMode = responseMode,
responseType = responseType,
scope = scope,
Expand All @@ -100,19 +118,31 @@ class VerificationService : VerificationServiceInterface {
responseString,
PresentationRequest::class.java
)
if (json.presentationDefinition == null && !json.presentationDefinitionUri.isNullOrBlank()){
val resolvedPresentationDefinition = getPresentationDefinitionFromDefinitionUri(json.presentationDefinitionUri)
json.presentationDefinition = resolvedPresentationDefinition
}
return json
}else{
if (isValidJWT(responseString?:"")) {
val json = gson.fromJson(
parseJWTForPayload(responseString?:"{}"),
PresentationRequest::class.java
)
if (json.presentationDefinition == null && !json.presentationDefinitionUri.isNullOrBlank()){
val resolvedPresentationDefinition = getPresentationDefinitionFromDefinitionUri(json.presentationDefinitionUri)
json.presentationDefinition = resolvedPresentationDefinition
}
return json
}else{
val json = gson.fromJson(
responseString?:"{}",
PresentationRequest::class.java
)
if (json.presentationDefinition == null && !json.presentationDefinitionUri.isNullOrBlank()){
val resolvedPresentationDefinition = getPresentationDefinitionFromDefinitionUri(json.presentationDefinitionUri)
json.presentationDefinition = resolvedPresentationDefinition
}
return json
}
}
Expand All @@ -133,6 +163,43 @@ class VerificationService : VerificationServiceInterface {
}
}

private suspend fun getPresentationDefinitionFromDefinitionUri(presentationDefinitionUri:String?):PresentationDefinition?{
if (presentationDefinitionUri.isNullOrBlank())
return null

val response =
ApiManager.api.getService()
?.getPresentationDefinitionFromPresentationDefinitionUri(presentationDefinitionUri ?: "")
if (response?.isSuccessful == true) {
val contentType = response.headers()["Content-Type"]
val responseString = response.body()?.string()
val gson = Gson()
if (contentType?.contains("application/json") == true) {
val json = gson.fromJson(
responseString,
PresentationDefinition::class.java
)
return json
}else{
if (isValidJWT(responseString?:"")) {
val json = gson.fromJson(
parseJWTForPayload(responseString?:"{}"),
PresentationDefinition::class.java
)
return json
}else{
val json = gson.fromJson(
responseString?:"{}",
PresentationDefinition::class.java
)
return json
}
}
} else {
return null
}
}

private fun isValidJWT(token: String): Boolean {
try {
// Parse the JWT token
Expand Down

0 comments on commit c5150e0

Please sign in to comment.