-
Notifications
You must be signed in to change notification settings - Fork 199
/
SandboxAuthParticipantFixture.scala
85 lines (76 loc) · 2.84 KB
/
SandboxAuthParticipantFixture.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package com.daml.lf.engine.script.test
import java.io.File
import com.daml.bazeltools.BazelRunfiles._
import com.daml.jwt.domain.DecodedJwt
import com.daml.jwt.{HMAC256Verifier, JwtSigner}
import com.daml.ledger.api.auth.{AuthServiceJWT, AuthServiceJWTCodec, AuthServiceJWTPayload}
import com.daml.ledger.api.refinements.ApiTypes.ApplicationId
import com.daml.ledger.api.testing.utils.AkkaBeforeAndAfterAll
import com.daml.ledger.api.tls.TlsConfiguration
import com.daml.lf.engine.script._
import com.daml.lf.engine.script.ledgerinteraction.ScriptTimeMode
import com.daml.platform.sandboxnext.SandboxNextFixture
import com.daml.platform.services.time.TimeProviderType
import org.scalatest.Suite
import scalaz.syntax.tag._
import scalaz.{-\/, \/-}
import scala.concurrent.ExecutionContext
trait SandboxAuthParticipantFixture
extends AbstractScriptTest
with SandboxNextFixture
with AkkaBeforeAndAfterAll {
self: Suite =>
private implicit val ec: ExecutionContext = system.dispatcher
def participantClients(parties: List[String], admin: Boolean) =
Runner.connect(
Participants(
default_participant = Some(
ApiParameters(
host = "localhost",
port = serverPort.value,
access_token = Some(getToken(parties, admin)),
application_id = Some(appId),
)
),
party_participants = Map.empty,
participants = Map.empty,
),
tlsConfig = TlsConfiguration(false, None, None, None),
maxInboundMessageSize = RunnerConfig.DefaultMaxInboundMessageSize,
)
private val secret = "secret"
override def config = super.config.copy(
timeProviderType = Some(TimeProviderType.WallClock),
authService = Some(
AuthServiceJWT(
HMAC256Verifier(secret).valueOr(err =>
sys.error(s"Failed to create HMAC256 verifierd $err")
)
)
),
)
override def timeMode = ScriptTimeMode.WallClock
private val appId = ApplicationId("daml-script-test")
def getToken(parties: List[String], admin: Boolean): String = {
val payload = AuthServiceJWTPayload(
ledgerId = None,
participantId = None,
exp = None,
// Set the application id to make sure it is set correctly.
applicationId = Some(appId.unwrap),
actAs = parties,
admin = admin,
readAs = List(),
)
val header = """{"alg": "HS256", "typ": "JWT"}"""
val jwt = DecodedJwt[String](header, AuthServiceJWTCodec.writeToString(payload))
JwtSigner.HMAC256.sign(jwt, secret) match {
case -\/(e) => throw new IllegalStateException(e.toString)
case \/-(a) => a.value
}
}
override def packageFiles: List[File] =
List(new File(rlocation("daml-script/test/script-test.dar")))
}