Skip to content

Commit da42d2c

Browse files
committed
refactor(fdo): re-enable credential reuse logic
1 parent 2c49d2a commit da42d2c

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

libs/astarte_fdo/lib/owner_onboarding.ex

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,16 @@ defmodule Astarte.FDO.OwnerOnboarding do
140140
with {:ok, ownership_voucher} <- OwnershipVoucher.fetch(realm_name, guid),
141141
{:ok, owner_key} <- Secrets.get_key_for_guid(realm_name, guid),
142142
{:ok, owner_public_key} <- OwnershipVoucher.owner_public_key(ownership_voucher) do
143-
rendezvous_info = ownership_voucher.header.rendezvous_info
143+
next_guid = session.replacement_guid || guid
144+
next_rv_info = session.replacement_rv_info || ownership_voucher.header.rendezvous_info
145+
next_owner_pub_key = session.replacement_pub_key || owner_public_key
144146

145147
# {:ok, private_key} = COSE.Keys.from_pem(private_key)
146148

147149
connection_credentials = %{
148-
guid: guid,
149-
rendezvous_info: rendezvous_info,
150-
owner_pub_key: owner_public_key,
150+
guid: next_guid,
151+
rendezvous_info: next_rv_info,
152+
owner_pub_key: next_owner_pub_key,
151153
owner_private_key: owner_key,
152154
device_info: "owned by astarte - realm #{realm_name}.#{Config.base_url_domain!()}"
153155
}

libs/astarte_fdo/lib/ownership_voucher/ownership_voucher.ex

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,10 @@ defmodule Astarte.FDO.OwnershipVoucher do
7575
{:ok, new_voucher}
7676
end
7777

78-
def credential_reuse?(_session) do
79-
# Credential reuse requires also Owner2Key and/or rv info
80-
# to be changed for credential reuse; so far, there is no API to do so,
81-
# so it is limited to the guid
82-
true
78+
def credential_reuse?(session) do
79+
is_nil(session.replacement_pub_key) and
80+
is_nil(session.replacement_rv_info) and
81+
session.replacement_guid == session.guid
8382
end
8483

8584
@doc """

libs/astarte_fdo/test/astarte_fdo/onboarding/done_test.exs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ defmodule Astarte.FDO.Onboarding.DoneTest do
3131
alias Astarte.FDO.Core.OwnerOnboarding.Session
3232
alias Astarte.FDO.OwnerOnboarding
3333
alias Astarte.FDO.OwnershipVoucher
34+
alias Astarte.FDO.Core.OwnershipVoucher, as: OVCore
35+
36+
alias Astarte.DataAccess.FDO.OwnershipVoucher, as: DataAccessOwnershipVoucher
3437

3538
@wrong_prove_dv_nonce :crypto.strong_rand_bytes(16)
3639

@@ -131,8 +134,6 @@ defmodule Astarte.FDO.Onboarding.DoneTest do
131134
assert voucher.hmac == old_voucher.hmac
132135
end
133136

134-
@tag :skip
135-
# TODO: re-enable this test when credential reuse logic is implemented.
136137
test "ensure old voucher is keep when ProveDv nonces match and TO2.done ends successfully without credential reuse ",
137138
%{
138139
realm: realm_name,
@@ -155,11 +156,18 @@ defmodule Astarte.FDO.Onboarding.DoneTest do
155156
{:ok, old_voucher} = OwnershipVoucher.fetch(realm_name, session.guid)
156157
{:ok, _} = OwnerOnboarding.done(realm_name, session, done_msg)
157158

158-
{:ok, voucher} = OwnershipVoucher.fetch(realm_name, session.guid)
159+
keyspace = Realm.keyspace_name(realm_name)
160+
161+
ov_record =
162+
Repo.get!(DataAccessOwnershipVoucher, session.guid, prefix: keyspace)
163+
164+
assert ov_record.output_voucher != nil
159165

160-
assert voucher.entries == []
166+
{:ok, output_voucher} =
167+
OVCore.decode_cbor(ov_record.output_voucher)
161168

162-
assert voucher.hmac != old_voucher.hmac
169+
assert output_voucher.entries == []
170+
assert output_voucher.hmac != old_voucher.hmac
163171
end
164172

165173
test "returns {:error, :invalid_message} when the ProveDv nonces don't match", %{

libs/astarte_fdo/test/astarte_fdo/owner_onboarding/owner_onboarding_test.exs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,6 @@ defmodule Astarte.FDO.OwnerOnboarding.OwnerOnboardingTest do
199199
end
200200

201201
describe "build_owner_service_info_ready/3" do
202-
@tag :skip
203-
# TODO: re-enable this test when credential reuse logic is implemented.
204202
test "successfully processes DeviceServiceInfoReady, creates new voucher, and returns OwnerServiceInfoReady",
205203
%{
206204
realm: realm_name,

0 commit comments

Comments
 (0)