Skip to content
This repository has been archived by the owner on Feb 1, 2021. It is now read-only.

Support new implementation for requesting claims in selective disclosure #99

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package me.uport.sdk.credentials

/**
* This is a helper class for building verifiable claim requests
* [specs](https://github.com/uport-project/specs/blob/develop/messages/sharereq.md#claims-spec)
* It supports adding multiple verifiable and user-info parameters and returns a map which can be
* added to the selective disclosure request
*
*/
class ClaimsRequestParams {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this requires a call to build() to be used in a request, it makes sense to call it ClaimsRequestBuilder or something along those lines.
This class does not encapsulate the params themselves.


private val verifiableMap: MutableMap<String, Any?> = mutableMapOf()
private val userInfoMap: MutableMap<String, Any?> = mutableMapOf()

/**
* This adds a unique record in the verifiable object
* The [type] param for this method is used as the key for the verifiable
* Values will be overwritten if this method is called more than once with the same type
*
*/
fun addVerifiable(type: String, params: VerifiableParams?): ClaimsRequestParams {
verifiableMap.put(type, params?.getMap())
return this
}

/**
* This adds a unique record in the user_info object
* The [type] param for this method is used as the key for the user_info
* Values will be overwritten if this method is called more than once with the same type
*
*/
fun addUserInfo(type: String, params: UserInfoParams?): ClaimsRequestParams {
userInfoMap.put(type, params?.getMap())
return this
}

/**
* Returns the final map which can be used in a selective disclosure request
*
*/
fun build(): Map<String, Any?> {
val payload: MutableMap<String, Any?> = mutableMapOf()
payload["verifiable"] = verifiableMap
payload["user_info"] = userInfoMap
return payload
}
}

/**
* This is a helper class for adding properties and values to a verifiable
*
*/
data class VerifiableParams(

/**
* [**optional**]
* Short string explaining why you need this
*
*/
private val reason: String? = null,

/**
* [**optional**]
* Indicate if this claim is essential
*
*/
private val essential: Boolean = false
) {

private val params: MutableMap<String, Any?> = mutableMapOf()
private val issuers: MutableList<Issuer?> = mutableListOf()

/**
* This adds records to the array of issuers
*
*/
fun addIssuer(did: String, url: String? = null): VerifiableParams {
issuers.add(Issuer(did, url))
return this
}

/**
* Returns a Mutable map of the a verifiable's params
*
*/
fun getMap(): MutableMap<String, Any?> {
params["iss"] = issuers
params["reason"] = reason
params["essential"] = essential
return params
}
}

/**
* This is a helper class for adding properties and values to user_info
*
*/
data class UserInfoParams(

/**
* [**optional**]
* Short string explaining why you need this
*
*/
private val reason: String? = null,

/**
* [**optional**]
* Indicate if this claim is essential
*
*/
private val essential: Boolean = false

) {

private val params: MutableMap<String, Any?> = mutableMapOf()

/**
* Returns a Mutable map of the user_info params
*
*/
fun getMap(): MutableMap<String, Any?> {
params["reason"] = reason
params["essential"] = essential
return params
}
}


/**
* This is a helper class for adding properties and values to issuer
*
*/
data class Issuer(

/**
* [**required**]
* The DID of allowed issuer of claims
*
*/
private val did: String,

/**
* [**optional**]
* The URL for obtaining the claim
*
*/
private val url: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

package me.uport.sdk.credentials

import me.uport.sdk.credentials.RequestAccountType.devicekey
import me.uport.sdk.credentials.RequestAccountType.general
import me.uport.sdk.credentials.RequestAccountType.keypair
import me.uport.sdk.credentials.RequestAccountType.none
import me.uport.sdk.credentials.RequestAccountType.segregated
import me.uport.sdk.credentials.RequestAccountType.*

/**
* A class that encapsulates the supported parameter types for creating a SelectiveDisclosureRequest.
Expand All @@ -19,78 +15,89 @@ import me.uport.sdk.credentials.RequestAccountType.segregated
* and ease of use of frequently used params.
*/
class SelectiveDisclosureRequestParams(
/**
* [**required**]
* a simple_list of attributes for which you are requesting credentials.
* Ex. [ 'name', 'country' ]
*/
val requested: List<String>,

/**
* [**required**]
* the url that can receive the response to this request.
* TODO: detail how that URL should be handled by the APP implementing this SDK
*
* This gets encoded as `callback` in the JWT payload
*/
val callbackUrl: String,

/**
* [**optional**]
* A simple_list of signed claims being requested.
* This is semantically similar to the [requested] field
* but the response should contain signatures as well.
*/
val verified: List<String>? = null,

/**
* [**optional**]
* The Ethereum network ID if it is relevant for this request.
*
* This gets encoded as `net` in the JWT payload
*
* Examples: `"0x4"`, [Networks.mainnet.networkId]
*/
val networkId: String? = null,


/**
* [**optional**]
* If this request implies a particular kind of account.
* This defaults to [RequestAccountType.general] (user choice)
*
* This gets encoded as `act` in the JWT payload
*
* @see [RequestAccountType]
*/
val accountType: RequestAccountType? = general,

/**
* [**optional**]
* A list of signed claims about the issuer, usually signed by 3rd parties.
*/
val vc: List<String>? = null,

/**
* [**optional**] defaults to [DEFAULT_SHARE_REQ_VALIDITY_SECONDS]
* The validity interval of this request, measured in seconds since the moment it is issued.
*/
val expiresInSeconds: Long? = DEFAULT_SHARE_REQ_VALIDITY_SECONDS,


//omitting the "notifications" permission because it has no relevance on android.
// It may be worth adding for direct interop with iOS but that is unclear now

/**
* [**optional**]
* This can hold extra fields for the JWT payload representing the request.
* Use this to provide any of the extra fields described in the
* [specs](https://github.com/uport-project/specs/blob/develop/messages/sharereq.md)
*
* The fields contained in [extras] will get overwritten by the named parameters
* in this class in case of a name collision.
*/
val extras: Map<String, Any>? = null
/**
* [**required**]
* a simple_list of attributes for which you are requesting credentials.
* Ex. [ 'name', 'country' ]
*/
val requested: List<String>,

/**
* [**required**]
* the url that can receive the response to this request.
* TODO: detail how that URL should be handled by the APP implementing this SDK
*
* This gets encoded as `callback` in the JWT payload
*/
val callbackUrl: String,

/**
* [**optional**]
* A simple_list of signed claims being requested.
* This is semantically similar to the [requested] field
* but the response should contain signatures as well.
*/
val verified: List<String>? = null,

/**
* [**optional**]
* This allows you to request claims with very specific properties.
* This replaces the [requested] and [verified] parameters of this request.
* You may still include requested and verified to provide support for older clients.
* But they will be ignored if by newer clients if the claims field is present
* [specs](https://github.com/uport-project/specs/blob/develop/messages/sharereq.md#claims-spec)
*
*/
val claims: Map<String, Any?>? = null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The claims param makes the other ones obsolete.
Should we print out a warning when claims param is used along with requested and verified ?


/**
* [**optional**]
* The Ethereum network ID if it is relevant for this request.
*
* This gets encoded as `net` in the JWT payload
*
* Examples: `"0x4"`, [Networks.mainnet.networkId]
*/
val networkId: String? = null,


/**
* [**optional**]
* If this request implies a particular kind of account.
* This defaults to [RequestAccountType.general] (user choice)
*
* This gets encoded as `act` in the JWT payload
*
* @see [RequestAccountType]
*/
val accountType: RequestAccountType? = general,

/**
* [**optional**]
* A list of signed claims about the issuer, usually signed by 3rd parties.
*/
val vc: List<String>? = null,

/**
* [**optional**] defaults to [DEFAULT_SHARE_REQ_VALIDITY_SECONDS]
* The validity interval of this request, measured in seconds since the moment it is issued.
*/
val expiresInSeconds: Long? = DEFAULT_SHARE_REQ_VALIDITY_SECONDS,


//omitting the "notifications" permission because it has no relevance on android.
// It may be worth adding for direct interop with iOS but that is unclear now

/**
* [**optional**]
* This can hold extra fields for the JWT payload representing the request.
* Use this to provide any of the extra fields described in the
* [specs](https://github.com/uport-project/specs/blob/develop/messages/sharereq.md)
*
* The fields contained in [extras] will get overwritten by the named parameters
* in this class in case of a name collision.
*/
val extras: Map<String, Any>? = null
)

/**
Expand Down Expand Up @@ -121,6 +128,7 @@ internal fun buildPayloadForShareReq(params: SelectiveDisclosureRequestParams):
val payload = params.extras.orEmpty().toMutableMap()

payload["callback"] = params.callbackUrl
payload["claims"] = params.claims.orEmpty().toMutableMap()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the claims field does not get modified later on, there is probably no need to make it mutable here.

Suggested change
payload["claims"] = params.claims.orEmpty().toMutableMap()
payload["claims"] = params.claims.orEmpty()

payload["requested"] = params.requested
params.verified?.let { payload["verified"] = it }
params.vc?.let { payload["vc"] = it }
Expand Down
Loading