Skip to content

Commit d296318

Browse files
committed
chore(backend): initalize graphql authorization
Adds an authorization layer, providing an actor to queries trough the token claims. Such actor is then used to authorize the query/mutation and allow subscriptions. The claims structures is unchanged, the actor only contains a map with the matched claims and is not persisted in the database, but rather passed down the context of triggered actions. Signed-off-by: Luca Zaninotto <luca.zaninotto@secomind.com>
1 parent eed0dcb commit d296318

File tree

15 files changed

+168
-10
lines changed

15 files changed

+168
-10
lines changed

backend/config/config.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ config :edgehog, EdgehogWeb.Endpoint,
9595

9696
config :edgehog, :ash_domains, [
9797
Edgehog.Astarte,
98+
Edgehog.Actors,
9899
Edgehog.BaseImages,
99100
Edgehog.Campaigns,
100101
Edgehog.Containers,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#
2+
# This file is part of Edgehog.
3+
#
4+
# Copyright 2026 SECO Mind Srl
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# SPDX-License-Identifier: Apache-2.0
19+
#
20+
21+
defmodule Edgehog.Actors.Actor do
22+
@moduledoc """
23+
Edgheog Actors.
24+
This module represents an actor performing a call trough the GraphQL APIs.
25+
"""
26+
27+
use Ash.Resource,
28+
domain: Edgehog.Actors
29+
30+
actions do
31+
defaults [:read]
32+
33+
create :from_claims do
34+
accept [:claims]
35+
end
36+
end
37+
38+
attributes do
39+
attribute :claims, :map, allow_nil?: false
40+
end
41+
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#
2+
# This file is part of Edgehog.
3+
#
4+
# Copyright 2026 SECO Mind Srl
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# SPDX-License-Identifier: Apache-2.0
19+
#
20+
21+
defmodule Edgehog.Actors do
22+
@moduledoc """
23+
The actors domain.
24+
"""
25+
26+
use Ash.Domain
27+
28+
alias Edgehog.Actors.Actor
29+
30+
resources do
31+
resource Actor
32+
end
33+
end

backend/lib/edgehog/base_images/base_images.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ defmodule Edgehog.BaseImages do
2525

2626
use Ash.Domain,
2727
extensions: [
28-
AshGraphql.Domain
28+
AshGraphql.Domain,
29+
Ash.Authorizer
2930
]
3031

3132
alias Edgehog.BaseImages.BaseImage
3233
alias Edgehog.BaseImages.BaseImageCollection
3334

35+
authorization do
36+
authorize :when_requested
37+
end
38+
3439
graphql do
3540
root_level_errors? true
3641

backend/lib/edgehog/campaigns/campaigns.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ defmodule Edgehog.Campaigns do
2525

2626
use Ash.Domain,
2727
extensions: [
28-
AshGraphql.Domain
28+
AshGraphql.Domain,
29+
Ash.Authorizer
2930
]
3031

3132
alias Edgehog.Campaigns.Campaign
3233
alias Edgehog.Campaigns.CampaignTarget
3334
alias Edgehog.Campaigns.Channel
3435

36+
authorization do
37+
authorize :when_requested
38+
end
39+
3540
graphql do
3641
root_level_errors? true
3742

backend/lib/edgehog/containers/containers.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
defmodule Edgehog.Containers do
2222
@moduledoc false
2323
use Ash.Domain,
24-
extensions: [AshGraphql.Domain]
24+
extensions: [
25+
AshGraphql.Domain,
26+
Ash.Authorizer
27+
]
2528

2629
alias Edgehog.Containers.Application
2730
alias Edgehog.Containers.Deployment
@@ -33,6 +36,10 @@ defmodule Edgehog.Containers do
3336
alias Edgehog.Containers.Release
3437
alias Edgehog.Containers.Volume
3538

39+
authorization do
40+
authorize :when_requested
41+
end
42+
3643
graphql do
3744
root_level_errors? true
3845

backend/lib/edgehog/devices/devices.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@ defmodule Edgehog.Devices do
2323

2424
use Ash.Domain,
2525
extensions: [
26-
AshGraphql.Domain
26+
AshGraphql.Domain,
27+
Ash.Authorizer
2728
]
2829

2930
alias Edgehog.Devices.Device
3031
alias Edgehog.Devices.HardwareType
3132
alias Edgehog.Devices.SystemModel
3233

34+
authorization do
35+
authorize :when_requested
36+
end
37+
3338
graphql do
3439
root_level_errors? true
3540

backend/lib/edgehog/files/files.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ defmodule Edgehog.Files do
2424
"""
2525

2626
use Ash.Domain,
27-
extensions: [AshGraphql.Domain]
27+
extensions: [AshGraphql.Domain, Ash.Authorizer]
28+
29+
authorization do
30+
authorize :when_requested
31+
end
2832

2933
alias Edgehog.Files.File
3034
alias Edgehog.Files.Repository

backend/lib/edgehog/forwarder/forwarder.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ defmodule Edgehog.Forwarder do
2222
@moduledoc false
2323
use Ash.Domain,
2424
extensions: [
25-
AshGraphql.Domain
25+
AshGraphql.Domain,
26+
Ash.Authorizer
2627
]
2728

2829
alias Edgehog.Forwarder.Config
2930
alias Edgehog.Forwarder.Session
3031

32+
authorization do
33+
authorize :when_requested
34+
end
35+
3136
graphql do
3237
root_level_errors? true
3338

backend/lib/edgehog/groups/groups.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ defmodule Edgehog.Groups do
2323
The Groups context.
2424
"""
2525

26-
use Ash.Domain, extensions: [AshGraphql.Domain]
26+
use Ash.Domain, extensions: [AshGraphql.Domain, Ash.Authorizer]
2727

2828
alias Edgehog.Groups.DeviceGroup
2929

30+
authorization do
31+
authorize :when_requested
32+
end
33+
3034
graphql do
3135
root_level_errors? true
3236

0 commit comments

Comments
 (0)