2023-11-01 03:32:09 +00:00
|
|
|
# Nimbus
|
2024-01-13 01:41:57 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-11-01 03:32:09 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
2022-07-18 04:34:42 +00:00
|
|
|
import
|
|
|
|
std/[base64, times, strutils],
|
|
|
|
test_env,
|
|
|
|
chronicles,
|
2023-07-24 14:50:39 +00:00
|
|
|
nimcrypto/[hmac],
|
2023-08-27 01:23:45 +00:00
|
|
|
web3/engine_api_types,
|
2024-01-13 01:41:57 +00:00
|
|
|
web3/conversions,
|
2022-07-18 04:34:42 +00:00
|
|
|
json_rpc/[rpcclient],
|
|
|
|
./types
|
|
|
|
|
|
|
|
# JWT Authentication Related
|
|
|
|
const
|
|
|
|
defaultJwtTokenSecretBytes = "secretsecretsecretsecretsecretse"
|
2023-08-07 11:10:55 +00:00
|
|
|
maxTimeDriftSeconds = 60'i64
|
2022-07-18 04:34:42 +00:00
|
|
|
defaultProtectedHeader = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9"
|
|
|
|
|
2024-01-13 01:41:57 +00:00
|
|
|
createRpcSigsFromNim(RpcClient):
|
|
|
|
proc engine_exchangeTransitionConfigurationV1(transitionConfiguration: TransitionConfigurationV1): TransitionConfigurationV1
|
|
|
|
|
2022-07-18 04:34:42 +00:00
|
|
|
proc base64urlEncode(x: auto): string =
|
|
|
|
base64.encode(x, safe = true).replace("=", "")
|
|
|
|
|
|
|
|
proc prepareAuthCallToken(secret: string, time: int64): string =
|
|
|
|
let key = cast[seq[byte]](secret)
|
|
|
|
let payload = """{"iat": $1}""" % [$time]
|
|
|
|
let token = defaultProtectedHeader & "." & payload.base64urlEncode
|
|
|
|
let sig = base64urlEncode(sha256.hmac(key, token).data)
|
|
|
|
token & "." & sig
|
|
|
|
|
2023-09-06 09:18:26 +00:00
|
|
|
proc getClient(env: TestEnv, token: string): RpcHttpClient =
|
2022-07-18 04:34:42 +00:00
|
|
|
proc authHeaders(): seq[(string, string)] =
|
|
|
|
@[("Authorization", "Bearer " & token)]
|
|
|
|
|
|
|
|
let client = newRpcHttpClient(getHeaders = authHeaders)
|
2024-01-29 13:20:04 +00:00
|
|
|
waitFor client.connect("127.0.0.1", env.engine.httpPort, false)
|
2022-07-18 04:34:42 +00:00
|
|
|
return client
|
|
|
|
|
|
|
|
template genAuthTest(procName: untyped, timeDriftSeconds: int64, customAuthSecretBytes: string, authOK: bool) =
|
2023-09-06 09:18:26 +00:00
|
|
|
proc procName(env: TestEnv): bool =
|
2022-07-18 04:34:42 +00:00
|
|
|
# Default values
|
|
|
|
var
|
|
|
|
# All test cases send a simple TransitionConfigurationV1 to check the Authentication mechanism (JWT)
|
|
|
|
tConf = TransitionConfigurationV1(
|
2023-09-06 09:18:26 +00:00
|
|
|
terminalTotalDifficulty: env.engine.ttd
|
2022-07-18 04:34:42 +00:00
|
|
|
)
|
|
|
|
testSecret = customAuthSecretBytes
|
|
|
|
testTime = getTime().toUnix
|
|
|
|
|
|
|
|
if testSecret.len == 0:
|
|
|
|
testSecret = defaultJwtTokenSecretBytes
|
|
|
|
|
|
|
|
if timeDriftSeconds != 0:
|
|
|
|
testTime = testTime + timeDriftSeconds
|
|
|
|
|
|
|
|
let token = prepareAuthCallToken(testSecret, testTime)
|
2023-09-06 09:18:26 +00:00
|
|
|
let client = getClient(env, token)
|
2022-07-18 04:34:42 +00:00
|
|
|
|
|
|
|
try:
|
2024-01-13 01:41:57 +00:00
|
|
|
discard waitFor client.engine_exchangeTransitionConfigurationV1(tConf)
|
2022-07-18 04:34:42 +00:00
|
|
|
testCond authOk:
|
|
|
|
error "Authentication was supposed to fail authentication but passed"
|
2023-09-06 09:18:26 +00:00
|
|
|
except CatchableError:
|
2022-07-18 04:34:42 +00:00
|
|
|
testCond not authOk:
|
|
|
|
error "Authentication was supposed to pass authentication but failed"
|
2023-08-21 02:08:54 +00:00
|
|
|
return true
|
2022-07-18 04:34:42 +00:00
|
|
|
|
|
|
|
genAuthTest(authTest1, 0'i64, "", true)
|
|
|
|
genAuthTest(authTest2, 0'i64, "secretsecretsecretsecretsecrets", false)
|
|
|
|
genAuthTest(authTest3, 0'i64, "\0secretsecretsecretsecretsecretse", false)
|
|
|
|
genAuthTest(authTest4, -1 - maxTimeDriftSeconds, "", false)
|
|
|
|
genAuthTest(authTest5, 1 - maxTimeDriftSeconds, "", true)
|
|
|
|
genAuthTest(authTest6, maxTimeDriftSeconds + 1, "", false)
|
|
|
|
genAuthTest(authTest7, maxTimeDriftSeconds - 1, "", true)
|
|
|
|
|
2023-08-21 02:08:54 +00:00
|
|
|
type
|
|
|
|
AuthSpec* = ref object of BaseSpec
|
2023-09-06 09:18:26 +00:00
|
|
|
exec*: proc(env: TestEnv): bool
|
2023-08-21 02:08:54 +00:00
|
|
|
|
|
|
|
proc specExecute(ws: BaseSpec): bool =
|
|
|
|
let
|
|
|
|
ws = AuthSpec(ws)
|
2023-09-06 09:18:26 +00:00
|
|
|
env = TestEnv.new("", true)
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2023-10-31 03:18:37 +00:00
|
|
|
env.engine.setRealTTD()
|
2023-08-21 02:08:54 +00:00
|
|
|
result = ws.exec(env)
|
2023-09-06 09:18:26 +00:00
|
|
|
env.close()
|
2023-08-21 02:08:54 +00:00
|
|
|
|
2022-07-18 04:34:42 +00:00
|
|
|
# JWT Authentication Tests
|
2023-08-21 02:08:54 +00:00
|
|
|
let authTestList* = [
|
|
|
|
TestDesc(
|
2022-07-18 04:34:42 +00:00
|
|
|
name: "JWT Authentication: No time drift, correct secret",
|
2023-08-21 02:08:54 +00:00
|
|
|
run: specExecute,
|
|
|
|
spec: AuthSpec(
|
|
|
|
exec: authTest1,
|
|
|
|
)
|
2022-07-18 04:34:42 +00:00
|
|
|
),
|
2023-08-21 02:08:54 +00:00
|
|
|
TestDesc(
|
2022-07-18 04:34:42 +00:00
|
|
|
name: "JWT Authentication: No time drift, incorrect secret (shorter)",
|
2023-08-21 02:08:54 +00:00
|
|
|
run: specExecute,
|
|
|
|
spec: AuthSpec(
|
|
|
|
exec: authTest2,
|
|
|
|
)
|
2022-07-18 04:34:42 +00:00
|
|
|
),
|
2023-08-21 02:08:54 +00:00
|
|
|
TestDesc(
|
2022-07-18 04:34:42 +00:00
|
|
|
name: "JWT Authentication: No time drift, incorrect secret (longer)",
|
2023-08-21 02:08:54 +00:00
|
|
|
run: specExecute,
|
|
|
|
spec: AuthSpec(
|
|
|
|
exec: authTest3,
|
|
|
|
)
|
2022-07-18 04:34:42 +00:00
|
|
|
),
|
2023-08-21 02:08:54 +00:00
|
|
|
TestDesc(
|
2022-07-18 04:34:42 +00:00
|
|
|
name: "JWT Authentication: Negative time drift, exceeding limit, correct secret",
|
2023-08-21 02:08:54 +00:00
|
|
|
run: specExecute,
|
|
|
|
spec: AuthSpec(
|
|
|
|
exec: authTest4,
|
|
|
|
)
|
2022-07-18 04:34:42 +00:00
|
|
|
),
|
2023-08-21 02:08:54 +00:00
|
|
|
TestDesc(
|
2022-07-18 04:34:42 +00:00
|
|
|
name: "JWT Authentication: Negative time drift, within limit, correct secret",
|
2023-08-21 02:08:54 +00:00
|
|
|
run: specExecute,
|
|
|
|
spec: AuthSpec(
|
|
|
|
exec: authTest5,
|
|
|
|
)
|
2022-07-18 04:34:42 +00:00
|
|
|
),
|
2023-08-21 02:08:54 +00:00
|
|
|
TestDesc(
|
2022-07-18 04:34:42 +00:00
|
|
|
name: "JWT Authentication: Positive time drift, exceeding limit, correct secret",
|
2023-08-21 02:08:54 +00:00
|
|
|
run: specExecute,
|
|
|
|
spec: AuthSpec(
|
|
|
|
exec: authTest6,
|
|
|
|
)
|
2022-07-18 04:34:42 +00:00
|
|
|
),
|
2023-08-21 02:08:54 +00:00
|
|
|
TestDesc(
|
2022-07-18 04:34:42 +00:00
|
|
|
name: "JWT Authentication: Positive time drift, within limit, correct secret",
|
2023-08-21 02:08:54 +00:00
|
|
|
run: specExecute,
|
|
|
|
spec: AuthSpec(
|
|
|
|
exec: authTest7,
|
|
|
|
)
|
2022-07-18 04:34:42 +00:00
|
|
|
)
|
|
|
|
]
|