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

hyperledger / identus-cloud-agent / 8938354333

03 May 2024 11:35AM UTC coverage: 49.343% (+0.06%) from 49.284%
8938354333

push

web-flow
docs: improve OAS docs for Event and IAM section (#1007)

Signed-off-by: Pat Losoponkul <pat.losoponkul@iohk.io>
Co-authored-by: Yurii Shynbuiev - IOHK <yurii.shynbuiev@iohk.io>

0 of 24 new or added lines in 3 files covered. (0.0%)

141 existing lines in 46 files now uncovered.

7328 of 14851 relevant lines covered (49.34%)

0.49 hits per line

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

76.39
/pollux/core/src/main/scala/org/hyperledger/identus/pollux/core/model/schema/CredentialSchema.scala
1
package org.hyperledger.identus.pollux.core.model.schema
2

3
import org.hyperledger.identus.pollux.core.model.error.CredentialSchemaError
4
import org.hyperledger.identus.pollux.core.model.error.CredentialSchemaError.*
5
import org.hyperledger.identus.pollux.core.model.schema.`type`.anoncred.AnoncredSchemaSerDesV1
6
import org.hyperledger.identus.pollux.core.model.schema.`type`.{
7
  AnoncredSchemaType,
8
  CredentialJsonSchemaType,
9
  CredentialSchemaType
10
}
11
import org.hyperledger.identus.pollux.core.model.schema.validator.{JsonSchemaValidator, JsonSchemaValidatorImpl}
12
import org.hyperledger.identus.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
) {
57
  def longId = CredentialSchema.makeLongId(author, id, version)
1✔
58
}
59

60
object CredentialSchema {
61

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

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

68
  def make(in: Input): ZIO[Any, Nothing, CredentialSchema] = {
1✔
69
    for {
1✔
70
      id <- zio.Random.nextUUID
1✔
71
      cs <- make(id, in)
1✔
72
    } yield cs
73
  }
74
  def make(id: UUID, in: Input): ZIO[Any, Nothing, CredentialSchema] = {
1✔
75
    for {
1✔
76
      ts <- zio.Clock.currentDateTime.map(
1✔
77
        _.atZoneSameInstant(ZoneOffset.UTC).toOffsetDateTime
1✔
78
      )
79
      guid = makeGUID(in.author, id, in.version)
1✔
80
    } yield CredentialSchema(
1✔
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,
×
99
      description: String,
100
      authored: Option[OffsetDateTime],
101
      tags: Seq[String],
UNCOV
102
      author: String = defaultAgentDid,
×
103
      `type`: String,
104
      schema: Schema
105
  )
106

107
  case class Filter(
108
      author: Option[String] = None,
1✔
109
      name: Option[String] = None,
1✔
110
      version: Option[String] = None,
1✔
111
      tags: Option[String] = None
1✔
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

119
  def validSchemaValidator(
1✔
120
      schemaId: String,
121
      uriDereferencer: URIDereferencer
122
  ): IO[CredentialSchemaError, JsonSchemaValidator] = {
123
    for {
1✔
124
      uri <- ZIO.attempt(new URI(schemaId)).mapError(t => URISyntaxError(t.getMessage))
1✔
125
      content <- uriDereferencer.dereference(uri).mapError(err => UnexpectedError(err.toString))
1✔
126
      json <- ZIO
1✔
127
        .fromEither(content.fromJson[Json])
1✔
128
        .mapError(error =>
129
          CredentialSchemaError.CredentialSchemaParsingError(s"Failed to parse resolved schema content as Json: $error")
×
130
        )
131
      schemaValidator <- JsonSchemaValidatorImpl
1✔
132
        .from(json)
1✔
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

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

153
  def validateAnonCredsClaims(
1✔
154
      schemaId: String,
155
      claims: String,
156
      uriDereferencer: URIDereferencer
157
  ): IO[CredentialSchemaError, Unit] = {
158
    for {
1✔
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 <-
1✔
162
        AnoncredSchemaSerDesV1.schemaSerDes
1✔
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
1✔
168
        case Json.Obj(fields) =>
1✔
169
          ZIO.foreach(fields) {
1✔
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)
1✔
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 ()
1✔
177
  }
178

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

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

190
  def validateCredentialSchema(vcSchema: CredentialSchema): IO[CredentialSchemaError, Unit] = {
1✔
191
    for {
1✔
192
      resolvedSchemaType <- resolveCredentialSchemaType(vcSchema.`type`)
1✔
193
      _ <- resolvedSchemaType.validate(vcSchema.schema).mapError(SchemaError.apply)
1✔
194
    } yield ()
1✔
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