|
| 1 | +/* |
| 2 | +Copyright Avast Software. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +// Package bitstringstatus handles client-side validation and parsing for |
| 8 | +// Credential Status fields of type BitstringStatusList, as per spec: https://www.w3.org/TR/vc-bitstring-status-list/ |
| 9 | +package bitstringstatus |
| 10 | + |
| 11 | +import ( |
| 12 | + "errors" |
| 13 | + "fmt" |
| 14 | + "strconv" |
| 15 | + |
| 16 | + "github.com/trustbloc/vc-go/verifiable" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + // BitstringStatusListType represents the implementation of Bitstring Status List. |
| 21 | + // VC.Status.Type |
| 22 | + // Doc: https://www.w3.org/TR/vc-bitstring-status-list/#bitstringstatuslistentry |
| 23 | + BitstringStatusListType = "BitstringStatusListEntry" |
| 24 | + |
| 25 | + // StatusListCredential stores the link to the status list VC. |
| 26 | + // VC.Status.CustomFields key. |
| 27 | + StatusListCredential = "statusListCredential" |
| 28 | + |
| 29 | + // StatusListIndex identifies the bit position of the status value of the VC. |
| 30 | + // VC.Status.CustomFields key. |
| 31 | + StatusListIndex = "statusListIndex" |
| 32 | + |
| 33 | + // StatusPurpose for BitstringStatusList. |
| 34 | + // VC.Status.CustomFields key. Only "revocation" value is supported. |
| 35 | + // TODO: check if it's really only 'revocation'. Spec allows: refresh, revocation, suspension, message. |
| 36 | + StatusPurpose = "statusPurpose" |
| 37 | + // StatusSize indicates the size of the status entry in bits. |
| 38 | + StatusSize = "statusSize" |
| 39 | + // StatusMessage represents custom descriptive messages about the status of the verifiable credential. |
| 40 | + StatusMessage = "statusMessage" |
| 41 | +) |
| 42 | + |
| 43 | +// Validator validates a Verifiable Credential's Status field against the BitstringStatusList specification, and |
| 44 | +// returns fields for status verification. |
| 45 | +// |
| 46 | +// Implements spec: https://www.w3.org/TR/vc-bitstring-status-list/#bitstringstatuslistentry |
| 47 | +type Validator struct{} |
| 48 | + |
| 49 | +// ValidateStatus validates that a Verifiable Credential's Status field matches the BitstringStatusList specification. |
| 50 | +func (v *Validator) ValidateStatus(vcStatus *verifiable.TypedID) error { |
| 51 | + if vcStatus == nil { |
| 52 | + return errors.New("vc status does not exist") |
| 53 | + } |
| 54 | + |
| 55 | + if vcStatus.Type != BitstringStatusListType { |
| 56 | + return fmt.Errorf("vc status %s not supported", vcStatus.Type) |
| 57 | + } |
| 58 | + |
| 59 | + for _, field := range []string{StatusListCredential, StatusListIndex, StatusPurpose} { |
| 60 | + if err := isMissingField(vcStatus, field); err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + err := checkStatusSize(vcStatus) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func isMissingField(vcStatus *verifiable.TypedID, field string) error { |
| 74 | + if vcStatus.CustomFields[field] == nil { |
| 75 | + return fmt.Errorf("%s field does not exist in vc status", field) |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +func checkStatusSize(vcStatus *verifiable.TypedID) error { |
| 82 | + statusSizeRaw := vcStatus.CustomFields[StatusSize] |
| 83 | + if statusSizeRaw == nil { |
| 84 | + return nil |
| 85 | + } |
| 86 | + |
| 87 | + statusSizeF, ok := statusSizeRaw.(float64) |
| 88 | + if !ok { |
| 89 | + return errors.New("statusSize must be an integer") |
| 90 | + } |
| 91 | + |
| 92 | + statusSize := int(statusSizeF) |
| 93 | + |
| 94 | + if statusSize <= 0 { |
| 95 | + return fmt.Errorf("statusSize must be greater than 0, but got %d", statusSize) |
| 96 | + } |
| 97 | + |
| 98 | + if statusSize == 1 { |
| 99 | + return nil |
| 100 | + } |
| 101 | + |
| 102 | + possibleStatusSizes := 1<<statusSize - 1 |
| 103 | + |
| 104 | + statusMessages, ok := vcStatus.CustomFields[StatusMessage].([]any) |
| 105 | + if !ok { |
| 106 | + return fmt.Errorf("%s must be an array", StatusMessage) |
| 107 | + } |
| 108 | + |
| 109 | + if len(statusMessages) != possibleStatusSizes { |
| 110 | + return fmt.Errorf("the length of %s must be equal to %d", StatusMessage, possibleStatusSizes) |
| 111 | + } |
| 112 | + |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +// GetStatusVCURI returns the ID (URL) of status VC. |
| 117 | +func (v *Validator) GetStatusVCURI(vcStatus *verifiable.TypedID) (string, error) { |
| 118 | + statusListVC, ok := vcStatus.CustomFields[StatusListCredential].(string) |
| 119 | + if !ok { |
| 120 | + return "", errors.New("failed to cast URI of statusListCredential") |
| 121 | + } |
| 122 | + |
| 123 | + return statusListVC, nil |
| 124 | +} |
| 125 | + |
| 126 | +// GetStatusListIndex returns the bit position of the status value of the VC. |
| 127 | +func (v *Validator) GetStatusListIndex(vcStatus *verifiable.TypedID) (int, error) { |
| 128 | + statusListIndex, ok := vcStatus.CustomFields[StatusListIndex].(string) |
| 129 | + if !ok { |
| 130 | + return -1, fmt.Errorf("%s must be a string", StatusListIndex) |
| 131 | + } |
| 132 | + |
| 133 | + idx, err := strconv.Atoi(statusListIndex) |
| 134 | + if err != nil { |
| 135 | + return -1, fmt.Errorf("unable to get statusListIndex: %w", err) |
| 136 | + } |
| 137 | + |
| 138 | + return idx, nil |
| 139 | +} |
| 140 | + |
| 141 | +// GetStatusPurpose returns the purpose of the status list. For example, "revocation", "suspension". |
| 142 | +func (v *Validator) GetStatusPurpose(vcStatus *verifiable.TypedID) (string, error) { |
| 143 | + statusPurpose, ok := vcStatus.CustomFields[StatusPurpose].(string) |
| 144 | + if !ok { |
| 145 | + return "", fmt.Errorf("%s must be a string", StatusPurpose) |
| 146 | + } |
| 147 | + |
| 148 | + return statusPurpose, nil |
| 149 | +} |
| 150 | + |
| 151 | +// MultiBaseEncoding indicates that status uses MultiBase encoding. |
| 152 | +// See https://www.w3.org/TR/cid-1.0/#multibase-0 for more details. |
| 153 | +func (v *Validator) MultiBaseEncoding() bool { |
| 154 | + return true |
| 155 | +} |
0 commit comments