• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

input-output-hk / atala-prism-building-blocks / 8795121910

23 Apr 2024 04:45AM UTC coverage: 31.908% (+0.4%) from 31.528%
8795121910

Pull #975

CryptoKnightIOG
feat: VC Verification test coverage (#972)

Signed-off-by: Bassam Riman <bassam.riman@iohk.io>
Pull Request #975: feat: Vc Verification Api

104 of 281 new or added lines in 15 files covered. (37.01%)

379 existing lines in 106 files now uncovered.

4619 of 14476 relevant lines covered (31.91%)

0.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

42.65
/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/CredentialSchema.scala
1
package io.iohk.atala.pollux.core.model.schema
2

3
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError
4
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError.*
5
import io.iohk.atala.pollux.core.model.schema.`type`.anoncred.AnoncredSchemaSerDesV1
6
import io.iohk.atala.pollux.core.model.schema.`type`.{
7
  AnoncredSchemaType,
8
  CredentialJsonSchemaType,
9
  CredentialSchemaType
10
}
11
import io.iohk.atala.pollux.core.model.schema.validator.{JsonSchemaValidator, JsonSchemaValidatorImpl}
12
import io.iohk.atala.pollux.core.service.URIDereferencer
13
import zio.*
14
import zio.json.*
15
import zio.json.ast.Json
16

17
import java.net.URI
18
import java.time.{OffsetDateTime, ZoneOffset}
19
import java.util.UUID
20

21
type Schema = zio.json.ast.Json
22

23
/** @param guid
24
  *   Globally unique identifier of the CredentialSchema object It's calculated as a UUID from string that contains the
25
  *   following fields: author, id and version
26
  * @param id
27
  *   Locally unique identifier of the CredentialSchema. It is UUID When the version of the credential schema changes
28
  *   this `id` keeps the same value
29
  * @param name
30
  *   Human readable name of the CredentialSchema
31
  * @param version
32
  *   Version of the CredentialSchema
33
  * @param author
34
  *   DID of the CredentialSchema's author
35
  * @param authored
36
  *   Datetime stamp of the schema creation
37
  * @param tags
38
  *   Tags of the CredentialSchema used for convenient lookup
39
  * @param description
40
  *   Human readable description of the schema
41
  * @param schema
42
  *   Internal schema object that depends on concrete implementation For W3C JsonSchema it is a JsonSchema object For
43
  *   AnonCreds schema is a AnonCreds schema
44
  */
45
case class CredentialSchema(
46
    guid: UUID,
47
    id: UUID,
48
    name: String,
49
    version: String,
50
    author: String,
51
    authored: OffsetDateTime,
52
    tags: Seq[String],
53
    description: String,
54
    `type`: String,
55
    schema: Schema
56
) {
1✔
57
  def longId = CredentialSchema.makeLongId(author, id, version)
58
}
59

60
object CredentialSchema {
61

1✔
62
  def makeLongId(author: String, id: UUID, version: String) =
1✔
63
    s"$author/${id.toString}?version=${version}"
64

1✔
65
  def makeGUID(author: String, id: UUID, version: String) =
1✔
66
    UUID.nameUUIDFromBytes(makeLongId(author, id, version).getBytes)
67

1✔
68
  def make(in: Input): ZIO[Any, Nothing, CredentialSchema] = {
1✔
69
    for {
×
70
      id <- zio.Random.nextUUID
1✔
71
      cs <- make(id, in)
72
    } yield cs
73
  }
1✔
74
  def make(id: UUID, in: Input): ZIO[Any, Nothing, CredentialSchema] = {
1✔
75
    for {
×
76
      ts <- zio.Clock.currentDateTime.map(
×
77
        _.atZoneSameInstant(ZoneOffset.UTC).toOffsetDateTime
78
      )
×
79
      guid = makeGUID(in.author, id, in.version)
80
    } yield CredentialSchema(
81
      guid = guid,
82
      id = id,
83
      name = in.name,
84
      version = in.version,
85
      author = in.author,
86
      authored = ts,
87
      tags = in.tags,
88
      description = in.description,
89
      `type` = in.`type`,
90
      schema = in.schema
91
    )
92
  }
93

94
  val defaultAgentDid = "did:prism:agent"
95

96
  case class Input(
97
      name: String,
98
      version: String,
UNCOV
99
      description: String,
×
100
      authored: Option[OffsetDateTime],
UNCOV
101
      tags: Seq[String],
×
102
      author: String = defaultAgentDid,
103
      `type`: String,
104
      schema: Schema
105
  )
106

107
  case class Filter(
1✔
108
      author: Option[String] = None,
1✔
109
      name: Option[String] = None,
1✔
110
      version: Option[String] = None,
1✔
111
      tags: Option[String] = None
112
  )
113

114
  case class FilteredEntries(entries: Seq[CredentialSchema], count: Long, totalCount: Long)
115

×
116
  given JsonEncoder[CredentialSchema] = DeriveJsonEncoder.gen[CredentialSchema]
×
117
  given JsonDecoder[CredentialSchema] = DeriveJsonDecoder.gen[CredentialSchema]
118

1✔
119
  def validSchemaValidator(
120
      schemaId: String,
121
      uriDereferencer: URIDereferencer
122
  ): IO[CredentialSchemaError, JsonSchemaValidator] = {
1✔
123
    for {
×
UNCOV
124
      uri <- ZIO.attempt(new URI(schemaId)).mapError(t => URISyntaxError(t.getMessage))
×
125
      content <- uriDereferencer.dereference(uri).mapError(err => UnexpectedError(err.toString))
1✔
126
      json <- ZIO
×
127
        .fromEither(content.fromJson[Json])
128
        .mapError(error =>
×
129
          CredentialSchemaError.CredentialSchemaParsingError(s"Failed to parse resolved schema content as Json: $error")
130
        )
1✔
131
      schemaValidator <- JsonSchemaValidatorImpl
×
132
        .from(json)
133
        .orElse(
×
134
          ZIO
×
135
            .fromEither(json.as[CredentialSchema])
×
136
            .mapError(error => CredentialSchemaParsingError(s"Failed to parse schema content as Json or OEA: $error"))
×
137
            .flatMap(cs => JsonSchemaValidatorImpl.from(cs.schema).mapError(SchemaError.apply))
138
        )
139
    } yield schemaValidator
140
  }
141

1✔
142
  def validateJWTCredentialSubject(
NEW
143
      schemaId: String,
×
NEW
144
      credentialSubject: String,
×
NEW
145
      uriDereferencer: URIDereferencer
×
146
  ): IO[CredentialSchemaError, Unit] = {
1✔
NEW
147
    for {
×
NEW
148
      schemaValidator <- validSchemaValidator(schemaId, uriDereferencer)
×
149
      _ <- schemaValidator.validate(credentialSubject).mapError(SchemaError.apply)
150
    } yield ()
151
  }
152

1✔
153
  def validateAnonCredsClaims(
154
      schemaId: String,
155
      claims: String,
156
      uriDereferencer: URIDereferencer
157
  ): IO[CredentialSchemaError, Unit] = {
1✔
158
    for {
×
159
      uri <- ZIO.attempt(new URI(schemaId)).mapError(t => URISyntaxError(t.getMessage))
1✔
160
      content <- uriDereferencer.dereference(uri).mapError(err => UnexpectedError(err.toString))
1✔
161
      validAttrNames <-
×
162
        AnoncredSchemaSerDesV1.schemaSerDes
163
          .deserialize(content)
×
164
          .mapError(error => CredentialSchemaParsingError(s"AnonCreds Schema parsing error: $error"))
165
          .map(_.attrNames)
×
166
      jsonClaims <- ZIO.fromEither(claims.fromJson[Json]).mapError(err => UnexpectedError(err))
1✔
167
      _ <- jsonClaims match
×
168
        case Json.Obj(fields) =>
×
169
          ZIO.foreach(fields) {
×
170
            case (k, _) if !validAttrNames.contains(k) =>
×
171
              ZIO.fail(UnexpectedError(s"Claim name undefined in schema: $k"))
×
172
            case (k, Json.Str(v)) => ZIO.succeed(k -> v)
×
173
            case (k, _)           => ZIO.fail(UnexpectedError(s"Claim value should be a string: $k"))
174
          }
×
175
        case _ => ZIO.fail(UnexpectedError("Provided 'claims' is not a JSON object"))
176
    } yield ()
177
  }
178

179
  private val supportedCredentialSchemaTypes: Map[String, CredentialSchemaType] =
×
180
    IndexedSeq(CredentialJsonSchemaType, AnoncredSchemaType)
×
181
      .map(credentialSchemaType => (credentialSchemaType.`type`, credentialSchemaType))
182
      .toMap
183

1✔
UNCOV
184
  def resolveCredentialSchemaType(`type`: String): IO[CredentialSchemaError, CredentialSchemaType] = {
×
185
    ZIO
×
186
      .fromOption(supportedCredentialSchemaTypes.get(`type`))
1✔
187
      .mapError(_ => UnsupportedCredentialSchemaType(s"Unsupported VC Schema type ${`type`}"))
188
  }
189

1✔
190
  def validateCredentialSchema(vcSchema: CredentialSchema): IO[CredentialSchemaError, Unit] = {
1✔
191
    for {
×
UNCOV
192
      resolvedSchemaType <- resolveCredentialSchemaType(vcSchema.`type`)
×
193
      _ <- resolvedSchemaType.validate(vcSchema.schema).mapError(SchemaError.apply)
194
    } yield ()
195
  }
196

197
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc