This repository has been archived by the owner on Feb 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Support new implementation for requesting claims in selective disclosure #99
Open
ugoamanoh
wants to merge
5
commits into
develop
Choose a base branch
from
feature/add-claims-support
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
89e1793
Added the claims parameter to the SelectiveDisclosureRequest
807e92b
Added a demonstration of the claims support in the demo app
3ec6074
Created helper classes for creating claims request
ccafcad
Added test for claims request helper class
8d817cb
Enabled optional fields as specified in the claims spec and added tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
credentials/src/main/java/me/uport/sdk/credentials/ClaimsRequestParams.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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. | ||||||
|
@@ -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, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||||||
|
||||||
/** | ||||||
* [**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 | ||||||
) | ||||||
|
||||||
/** | ||||||
|
@@ -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() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since the
Suggested change
|
||||||
payload["requested"] = params.requested | ||||||
params.verified?.let { payload["verified"] = it } | ||||||
params.vc?.let { payload["vc"] = it } | ||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 itClaimsRequestBuilder
or something along those lines.This class does not encapsulate the params themselves.