Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Anoncreds schemas and validation #3397

Merged
merged 8 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
19 changes: 13 additions & 6 deletions acapy_agent/messaging/valid.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,16 +363,20 @@ def __init__(self):


class AnoncredsDID(Regexp):
"""Validate value against indy DID."""
"""Validate value against anoncreds DID."""

METHOD = r"([a-zA-Z0-9_]+)"
NETWORK = r"(:[a-zA-Z0-9_.%-]+)?" # Optional network
METHOD_ID = r"([a-zA-Z0-9_.%-]+(:[a-zA-Z0-9_.%-]+)*)"

EXAMPLE = "did:(method):WgWxqztrNooG92RXvxSTWv"
PATTERN = re.compile("^(did:[a-z]:.+$)?$")
PATTERN = re.compile(rf"^did:{METHOD}{NETWORK}:{METHOD_ID}")

def __init__(self):
"""Initialize the instance."""

super().__init__(
IndyDID.PATTERN,
DIDValidation.PATTERN,
error="Value {input} is not an decentralized identifier (DID)",
)

Expand All @@ -381,14 +385,17 @@ class DIDValidation(Regexp):
"""Validate value against any valid DID spec."""

METHOD = r"([a-zA-Z0-9_]+)"
NETWORK = r"(:[a-zA-Z0-9_.%-]+)?" # Optional network
METHOD_ID = r"([a-zA-Z0-9_.%-]+(:[a-zA-Z0-9_.%-]+)*)"
PARAMS = r"((;[a-zA-Z0-9_.:%-]+=[a-zA-Z0-9_.:%-]*)*)"
PATH = r"(\/[^#?]*)?"
QUERY = r"([?][^#]*)?"
FRAGMENT = r"(\#.*)?$"

EXAMPLE = "did:peer:WgWxqztrNooG92RXvxSTWv"
PATTERN = re.compile(rf"^did:{METHOD}:{METHOD_ID}{PARAMS}{PATH}{QUERY}{FRAGMENT}$")
PATTERN = re.compile(
rf"^did:{METHOD}{NETWORK}:{METHOD_ID}{PARAMS}{PATH}{QUERY}{FRAGMENT}$"
)

def __init__(self):
"""Initialize the instance."""
Expand Down Expand Up @@ -485,7 +492,7 @@ def __init__(self):
"""Initialize the instance."""

super().__init__(
IndyCredDefId.PATTERN,
AnoncredsCredDefId.PATTERN,
error="Value {input} is not an anoncreds credential definition identifier",
)

Expand Down Expand Up @@ -530,7 +537,7 @@ def __init__(self):
"""Initialize the instance."""

super().__init__(
IndySchemaId.PATTERN,
AnoncredsSchemaId.PATTERN,
error="Value {input} is not an anoncreds schema identifier",
)

Expand Down
46 changes: 23 additions & 23 deletions acapy_agent/revocation/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
from ..messaging.models.openapi import OpenAPISchema
from ..messaging.responder import BaseResponder
from ..messaging.valid import (
INDY_CRED_DEF_ID_EXAMPLE,
INDY_CRED_DEF_ID_VALIDATE,
ANONCREDS_CRED_DEF_ID_EXAMPLE,
ANONCREDS_CRED_DEF_ID_VALIDATE,
INDY_CRED_REV_ID_EXAMPLE,
INDY_CRED_REV_ID_VALIDATE,
INDY_REV_REG_ID_EXAMPLE,
INDY_REV_REG_ID_VALIDATE,
ANONCREDS_REV_REG_ID_EXAMPLE,
ANONCREDS_REV_REG_ID_VALIDATE,
INDY_REV_REG_SIZE_EXAMPLE,
INDY_REV_REG_SIZE_VALIDATE,
UUID4_EXAMPLE,
Expand Down Expand Up @@ -86,10 +86,10 @@ class RevRegCreateRequestSchema(OpenAPISchema):
"""Request schema for revocation registry creation request."""

credential_definition_id = fields.Str(
validate=INDY_CRED_DEF_ID_VALIDATE,
validate=ANONCREDS_CRED_DEF_ID_VALIDATE,
metadata={
"description": "Credential definition identifier",
"example": INDY_CRED_DEF_ID_EXAMPLE,
"example": ANONCREDS_CRED_DEF_ID_EXAMPLE,
},
)
max_cred_num = fields.Int(
Expand Down Expand Up @@ -143,10 +143,10 @@ def validate_fields(self, data, **kwargs):

rev_reg_id = fields.Str(
required=False,
validate=INDY_REV_REG_ID_VALIDATE,
validate=ANONCREDS_REV_REG_ID_VALIDATE,
metadata={
"description": "Revocation registry identifier",
"example": INDY_REV_REG_ID_EXAMPLE,
"example": ANONCREDS_REV_REG_ID_EXAMPLE,
},
)
cred_rev_id = fields.Str(
Expand Down Expand Up @@ -182,18 +182,18 @@ def validate_fields(self, data, **kwargs):

rev_reg_id = fields.Str(
required=False,
validate=INDY_REV_REG_ID_VALIDATE,
validate=ANONCREDS_REV_REG_ID_VALIDATE,
metadata={
"description": "Revocation registry identifier",
"example": INDY_REV_REG_ID_EXAMPLE,
"example": ANONCREDS_REV_REG_ID_EXAMPLE,
},
)
cred_def_id = fields.Str(
required=False,
validate=INDY_CRED_DEF_ID_VALIDATE,
validate=ANONCREDS_CRED_DEF_ID_VALIDATE,
metadata={
"description": "Credential definition identifier",
"example": INDY_CRED_DEF_ID_EXAMPLE,
"example": ANONCREDS_CRED_DEF_ID_EXAMPLE,
},
)

Expand Down Expand Up @@ -270,7 +270,7 @@ class PublishRevocationsSchema(OpenAPISchema):

rrid2crid = fields.Dict(
required=False,
keys=fields.Str(metadata={"example": INDY_REV_REG_ID_EXAMPLE}),
keys=fields.Str(metadata={"example": ANONCREDS_REV_REG_ID_EXAMPLE}),
values=fields.List(
fields.Str(
validate=INDY_CRED_REV_ID_VALIDATE,
Expand Down Expand Up @@ -303,7 +303,7 @@ class ClearPendingRevocationsRequestSchema(OpenAPISchema):

purge = fields.Dict(
required=False,
keys=fields.Str(metadata={"example": INDY_REV_REG_ID_EXAMPLE}),
keys=fields.Str(metadata={"example": ANONCREDS_REV_REG_ID_EXAMPLE}),
values=fields.List(
fields.Str(
validate=INDY_CRED_REV_ID_VALIDATE,
Expand Down Expand Up @@ -383,10 +383,10 @@ class RevRegsCreatedSchema(OpenAPISchema):

rev_reg_ids = fields.List(
fields.Str(
validate=INDY_REV_REG_ID_VALIDATE,
validate=ANONCREDS_REV_REG_ID_VALIDATE,
metadata={
"description": "Revocation registry identifiers",
"example": INDY_REV_REG_ID_EXAMPLE,
"example": ANONCREDS_REV_REG_ID_EXAMPLE,
},
)
)
Expand All @@ -401,7 +401,7 @@ class RevRegUpdateTailsFileUriSchema(OpenAPISchema):
"description": "Public URI to the tails file",
"example": (
"http://192.168.56.133:6543/revocation/registry/"
f"{INDY_REV_REG_ID_EXAMPLE}/tails-file"
f"{ANONCREDS_REV_REG_ID_EXAMPLE}/tails-file"
),
},
)
Expand All @@ -412,10 +412,10 @@ class RevRegsCreatedQueryStringSchema(OpenAPISchema):

cred_def_id = fields.Str(
required=False,
validate=INDY_CRED_DEF_ID_VALIDATE,
validate=ANONCREDS_CRED_DEF_ID_VALIDATE,
metadata={
"description": "Credential definition identifier",
"example": INDY_CRED_DEF_ID_EXAMPLE,
"example": ANONCREDS_CRED_DEF_ID_EXAMPLE,
},
)
state = fields.Str(
Expand Down Expand Up @@ -452,10 +452,10 @@ class RevRegIdMatchInfoSchema(OpenAPISchema):

rev_reg_id = fields.Str(
required=True,
validate=INDY_REV_REG_ID_VALIDATE,
validate=ANONCREDS_REV_REG_ID_VALIDATE,
metadata={
"description": "Revocation Registry identifier",
"example": INDY_REV_REG_ID_EXAMPLE,
"example": ANONCREDS_REV_REG_ID_EXAMPLE,
},
)

Expand All @@ -465,10 +465,10 @@ class RevocationCredDefIdMatchInfoSchema(OpenAPISchema):

cred_def_id = fields.Str(
required=True,
validate=INDY_CRED_DEF_ID_VALIDATE,
validate=ANONCREDS_CRED_DEF_ID_VALIDATE,
metadata={
"description": "Credential definition identifier",
"example": INDY_CRED_DEF_ID_EXAMPLE,
"example": ANONCREDS_CRED_DEF_ID_EXAMPLE,
},
)

Expand Down
10 changes: 6 additions & 4 deletions acapy_agent/revocation/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,23 @@ async def test_validate_cred_rev_rec_qs_and_revoke_req(self):
):
req.validate_fields(
{
"rev_reg_id": test_module.INDY_REV_REG_ID_EXAMPLE,
"rev_reg_id": test_module.ANONCREDS_REV_REG_ID_EXAMPLE,
"cred_rev_id": test_module.INDY_CRED_REV_ID_EXAMPLE,
}
)
req.validate_fields({"cred_ex_id": test_module.UUID4_EXAMPLE})
with self.assertRaises(test_module.ValidationError):
req.validate_fields({})
with self.assertRaises(test_module.ValidationError):
req.validate_fields({"rev_reg_id": test_module.INDY_REV_REG_ID_EXAMPLE})
req.validate_fields(
{"rev_reg_id": test_module.ANONCREDS_REV_REG_ID_EXAMPLE}
)
with self.assertRaises(test_module.ValidationError):
req.validate_fields({"cred_rev_id": test_module.INDY_CRED_REV_ID_EXAMPLE})
with self.assertRaises(test_module.ValidationError):
req.validate_fields(
{
"rev_reg_id": test_module.INDY_REV_REG_ID_EXAMPLE,
"rev_reg_id": test_module.ANONCREDS_REV_REG_ID_EXAMPLE,
"cred_ex_id": test_module.UUID4_EXAMPLE,
}
)
Expand All @@ -102,7 +104,7 @@ async def test_validate_cred_rev_rec_qs_and_revoke_req(self):
with self.assertRaises(test_module.ValidationError):
req.validate_fields(
{
"rev_reg_id": test_module.INDY_REV_REG_ID_EXAMPLE,
"rev_reg_id": test_module.ANONCREDS_REV_REG_ID_EXAMPLE,
"cred_rev_id": test_module.INDY_CRED_REV_ID_EXAMPLE,
"cred_ex_id": test_module.UUID4_EXAMPLE,
}
Expand Down
42 changes: 21 additions & 21 deletions acapy_agent/revocation_anoncreds/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
from ..ledger.multiple_ledger.base_manager import BaseMultipleLedgerManager
from ..messaging.models.openapi import OpenAPISchema
from ..messaging.valid import (
INDY_CRED_DEF_ID_EXAMPLE,
INDY_CRED_DEF_ID_VALIDATE,
ANONCREDS_CRED_DEF_ID_EXAMPLE,
ANONCREDS_CRED_DEF_ID_VALIDATE,
INDY_CRED_REV_ID_EXAMPLE,
INDY_CRED_REV_ID_VALIDATE,
INDY_REV_REG_ID_EXAMPLE,
INDY_REV_REG_ID_VALIDATE,
ANONCREDS_REV_REG_ID_EXAMPLE,
ANONCREDS_REV_REG_ID_VALIDATE,
UUID4_EXAMPLE,
UUID4_VALIDATE,
WHOLE_NUM_EXAMPLE,
Expand Down Expand Up @@ -99,10 +99,10 @@ def validate_fields(self, data, **kwargs):

rev_reg_id = fields.Str(
required=False,
validate=INDY_REV_REG_ID_VALIDATE,
validate=ANONCREDS_REV_REG_ID_VALIDATE,
metadata={
"description": "Revocation registry identifier",
"example": INDY_REV_REG_ID_EXAMPLE,
"example": ANONCREDS_REV_REG_ID_EXAMPLE,
},
)
cred_rev_id = fields.Str(
Expand Down Expand Up @@ -138,18 +138,18 @@ def validate_fields(self, data, **kwargs):

rev_reg_id = fields.Str(
required=False,
validate=INDY_REV_REG_ID_VALIDATE,
validate=ANONCREDS_REV_REG_ID_VALIDATE,
metadata={
"description": "Revocation registry identifier",
"example": INDY_REV_REG_ID_EXAMPLE,
"example": ANONCREDS_REV_REG_ID_EXAMPLE,
},
)
cred_def_id = fields.Str(
required=False,
validate=INDY_CRED_DEF_ID_VALIDATE,
validate=ANONCREDS_CRED_DEF_ID_VALIDATE,
metadata={
"description": "Credential definition identifier",
"example": INDY_CRED_DEF_ID_EXAMPLE,
"example": ANONCREDS_CRED_DEF_ID_EXAMPLE,
},
)

Expand Down Expand Up @@ -215,10 +215,10 @@ class RevRegsCreatedSchemaAnoncreds(OpenAPISchema):

rev_reg_ids = fields.List(
fields.Str(
validate=INDY_REV_REG_ID_VALIDATE,
validate=ANONCREDS_REV_REG_ID_VALIDATE,
metadata={
"description": "Revocation registry identifiers",
"example": INDY_REV_REG_ID_EXAMPLE,
"example": ANONCREDS_REV_REG_ID_EXAMPLE,
},
)
)
Expand All @@ -233,7 +233,7 @@ class RevRegUpdateTailsFileUriSchema(OpenAPISchema):
"description": "Public URI to the tails file",
"example": (
"http://192.168.56.133:6543/revocation/registry/"
f"{INDY_REV_REG_ID_EXAMPLE}/tails-file"
f"{ANONCREDS_REV_REG_ID_EXAMPLE}/tails-file"
),
},
)
Expand All @@ -244,10 +244,10 @@ class RevRegsCreatedQueryStringSchema(OpenAPISchema):

cred_def_id = fields.Str(
required=False,
validate=INDY_CRED_DEF_ID_VALIDATE,
validate=ANONCREDS_CRED_DEF_ID_VALIDATE,
metadata={
"description": "Credential definition identifier",
"example": INDY_CRED_DEF_ID_EXAMPLE,
"example": ANONCREDS_CRED_DEF_ID_EXAMPLE,
},
)
state = fields.Str(
Expand Down Expand Up @@ -284,10 +284,10 @@ class RevRegIdMatchInfoSchema(OpenAPISchema):

rev_reg_id = fields.Str(
required=True,
validate=INDY_REV_REG_ID_VALIDATE,
validate=ANONCREDS_REV_REG_ID_VALIDATE,
metadata={
"description": "Revocation Registry identifier",
"example": INDY_REV_REG_ID_EXAMPLE,
"example": ANONCREDS_REV_REG_ID_EXAMPLE,
},
)

Expand All @@ -297,10 +297,10 @@ class RevocationCredDefIdMatchInfoSchema(OpenAPISchema):

cred_def_id = fields.Str(
required=True,
validate=INDY_CRED_DEF_ID_VALIDATE,
validate=ANONCREDS_CRED_DEF_ID_VALIDATE,
metadata={
"description": "Credential definition identifier",
"example": INDY_CRED_DEF_ID_EXAMPLE,
"example": ANONCREDS_CRED_DEF_ID_EXAMPLE,
},
)

Expand Down Expand Up @@ -348,7 +348,7 @@ class PublishRevocationsSchemaAnoncreds(OpenAPISchema):

rrid2crid = fields.Dict(
required=False,
keys=fields.Str(metadata={"example": INDY_REV_REG_ID_EXAMPLE}),
keys=fields.Str(metadata={"example": ANONCREDS_REV_REG_ID_EXAMPLE}),
values=fields.List(
fields.Str(
validate=INDY_CRED_REV_ID_VALIDATE,
Expand All @@ -368,7 +368,7 @@ class PublishRevocationsResultSchemaAnoncreds(OpenAPISchema):

rrid2crid = fields.Dict(
required=False,
keys=fields.Str(metadata={"example": INDY_REV_REG_ID_EXAMPLE}),
keys=fields.Str(metadata={"example": ANONCREDS_REV_REG_ID_EXAMPLE}),
values=fields.List(
fields.Str(
validate=INDY_CRED_REV_ID_VALIDATE,
Expand Down
Loading
Loading