Skip to content

Commit bff4308

Browse files
committed
refactor(fdo): re-enable credential reuse logic
Signed-off-by: Alioune Gaye <alioune.gaye@secomind.com>
1 parent 2c49d2a commit bff4308

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-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: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ defmodule Astarte.FDO.Onboarding.DoneTest do
2525
alias Astarte.Core.Device, as: CoreDevice
2626
alias Astarte.DataAccess.Device
2727
alias Astarte.DataAccess.Devices.Device, as: DeviceDB
28+
alias Astarte.DataAccess.FDO.OwnershipVoucher, as: DataAccessOwnershipVoucher
2829
alias Astarte.DataAccess.Realms.Realm
2930
alias Astarte.DataAccess.Repo
3031
alias Astarte.FDO.Core.Hash
3132
alias Astarte.FDO.Core.OwnerOnboarding.Session
33+
alias Astarte.FDO.Core.OwnershipVoucher, as: OVCore
3234
alias Astarte.FDO.OwnerOnboarding
3335
alias Astarte.FDO.OwnershipVoucher
3436

@@ -131,8 +133,6 @@ defmodule Astarte.FDO.Onboarding.DoneTest do
131133
assert voucher.hmac == old_voucher.hmac
132134
end
133135

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

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

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

162-
assert voucher.hmac != old_voucher.hmac
168+
assert output_voucher.entries == []
169+
assert output_voucher.hmac != old_voucher.hmac
163170
end
164171

165172
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)