Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions src/cli/artemis.toit
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import .pod-specification

import .utils

import .artemis-servers.artemis-server
import .auth-providers.auth-provider
import .brokers.broker
import .sdk
import .organization
Expand All @@ -31,7 +31,7 @@ import .server-config
Manages devices that have an Artemis service running on them.
*/
class Artemis:
artemis-server_/ArtemisServerCli? := null
auth-provider_/AuthProvider? := null
network_/net.Interface? := null

cli_/Cli
Expand All @@ -47,9 +47,9 @@ class Artemis:
If the manager opened any connections, closes them as well.
*/
close:
if artemis-server_: artemis-server_.close
if auth-provider_: auth-provider_.close
if network_: network_.close
artemis-server_ = null
auth-provider_ = null
network_ = null

/** Opens the network. */
Expand All @@ -58,31 +58,27 @@ class Artemis:
network_ = net.open

/**
Returns a connected artemis-server, using the $server-config to connect.
Returns a connected auth provider, using the $server-config to connect.

If $authenticated is true (the default), calls $ArtemisServerCli.ensure-authenticated.
If $authenticated is true (the default), calls $AuthProvider.ensure-authenticated.
*/
connected-artemis-server_ --authenticated/bool=true -> ArtemisServerCli:
if not artemis-server_:
connected-auth-provider_ --authenticated/bool=true -> AuthProvider:
if not auth-provider_:
connect-network_
artemis-server_ = ArtemisServerCli network_ server-config --cli=cli_
auth-provider_ = AuthProvider network_ server-config --cli=cli_
if authenticated:
artemis-server_.ensure-authenticated: | error-message |
auth-provider_.ensure-authenticated: | error-message |
cli_.ui.abort "$error-message (artemis)."
return artemis-server_
return auth-provider_

/**
Ensures that the user is authenticated with the Artemis server.
*/
ensure-authenticated -> none:
connected-artemis-server_

notify-created --hardware-id/Uuid:
server := connected-artemis-server_
server.notify-created --hardware-id=hardware-id
connected-auth-provider_

create-device --device-id/Uuid? --organization-id/Uuid -> Device:
return connected-artemis-server_.create-device-in-organization
return connected-auth-provider_.create-device-in-organization
--device-id=device-id
--organization-id=organization-id

Expand All @@ -92,7 +88,7 @@ class Artemis:
Returns null if the organization doesn't exist.
*/
get-organization --id/Uuid -> OrganizationDetailed?:
return connected-artemis-server_.get-organization id
return connected-auth-provider_.get-organization id

service-path-in-repository root/string --chip-family/string -> string:
return "$root/src/service/run/$(chip-family).toit"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import log
import net
import uuid show Uuid

import .supabase show ArtemisServerCliSupabase
import .http.base show ArtemisServerCliHttpToit
import .supabase show AuthProviderSupabase
import .http.base show AuthProviderHttpToit
import ...shared.server-config
import ..auth
import ..config
Expand All @@ -16,12 +16,12 @@ import ..organization
/**
An abstraction for the Artemis server.
*/
interface ArtemisServerCli implements Authenticatable:
interface AuthProvider implements Authenticatable:
constructor network/net.Interface server-config/ServerConfig --cli/Cli:
if server-config is ServerConfigSupabase:
return ArtemisServerCliSupabase network (server-config as ServerConfigSupabase) --cli=cli
return AuthProviderSupabase network (server-config as ServerConfigSupabase) --cli=cli
if server-config is ServerConfigHttp:
return ArtemisServerCliHttpToit network (server-config as ServerConfigHttp) --cli=cli
return AuthProviderHttpToit network (server-config as ServerConfigHttp) --cli=cli
throw "UNSUPPORTED ARTEMIS SERVER CONFIG"

is-closed -> bool
Expand Down Expand Up @@ -68,14 +68,6 @@ interface ArtemisServerCli implements Authenticatable:
*/
create-device-in-organization --organization-id/Uuid --device-id/Uuid? -> Device

/**
Notifies the server that the device with the given $hardware-id was created.

This operation is mostly for debugging purposes, as the $create-device-in-organization
already has a similar effect.
*/
notify-created --hardware-id/Uuid

/** Returns the used-id of the authenticated user. */
get-current-user-id -> string

Expand Down Expand Up @@ -140,11 +132,11 @@ interface ArtemisServerCli implements Authenticatable:
// TODO(florian): add support for changing the email.
update-profile --name/string

with-server server-config/ServerConfig --cli/Cli [block]:
with-auth-provider server-config/ServerConfig --cli/Cli [block]:
network := net.open
server/ArtemisServerCli? := null
server/AuthProvider? := null
try:
server = ArtemisServerCli network server-config --cli=cli
server = AuthProvider network server-config --cli=cli
block.call server
finally:
if server: server.close
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import encoding.json
import encoding.base64
import uuid show Uuid

import ..artemis-server
import ..auth-provider
import ...config
import ...device
import ...organization
Expand All @@ -19,7 +19,7 @@ import ....shared.server-config
import ....shared.utils as utils
import ....shared.constants show *

class ArtemisServerCliHttpToit implements ArtemisServerCli:
class AuthProviderHttpToit implements AuthProvider:
client_/http.Client? := ?
server-config_/ServerConfigHttp
current-user-id_/Uuid? := null
Expand Down Expand Up @@ -87,12 +87,6 @@ class ArtemisServerCliHttpToit implements ArtemisServerCli:
--id=Uuid.parse device-info["alias"]
--organization-id=Uuid.parse device-info["organization_id"]

notify-created --hardware-id/Uuid -> none:
send-request_ COMMAND-NOTIFY-ARTEMIS-CREATED_ {
"hardware_id": "$hardware-id",
"data": { "type": "created" },
}

get-current-user-id -> Uuid:
return current-user-id_

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import supabase
import supabase.filter show equals is-null orr
import uuid show Uuid

import ..artemis-server
import ..auth-provider
import ...config
import ...device
import ...organization
Expand All @@ -19,7 +19,7 @@ import ....shared.server-config

TOIT_IO_AUTH_REDIRECT_URL ::= "https://toit.io/auth"

class ArtemisServerCliSupabase implements ArtemisServerCli:
class AuthProviderSupabase implements AuthProvider:
client_/supabase.Client? := ?
server-config_/ServerConfigSupabase

Expand Down Expand Up @@ -73,12 +73,6 @@ class ArtemisServerCliSupabase implements ArtemisServerCli:
--id=Uuid.parse inserted["alias"]
--organization-id=Uuid.parse inserted["organization_id"]

notify-created --hardware-id/Uuid -> none:
client_.rest.insert "events" --no-return-inserted {
"device_id": "$hardware-id",
"data": { "type": "created" }
}

get-current-user-id -> Uuid:
return Uuid.parse client_.auth.get-current-user["id"]

Expand Down
4 changes: 2 additions & 2 deletions src/cli/cmds/auth.toit
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ..cache
import ..config
import ..auth show Authenticatable
import ..server-config
import ..artemis-servers.artemis-server show with-server ArtemisServerCli
import ..auth-providers.auth-provider show with-auth-provider AuthProvider
import ..brokers.broker show with-broker BrokerCli

SIGNIN-OPTIONS ::= [
Expand Down Expand Up @@ -181,7 +181,7 @@ with-authenticatable invocation/Invocation [block]:
block.call server-config.name broker
else:
server-config = get-server-from-config --cli=cli --key=CONFIG-ARTEMIS-DEFAULT-KEY
with-server server-config --cli=cli: | server/ArtemisServerCli |
with-auth-provider server-config --cli=cli: | server/AuthProvider |
block.call server-config.name server

sign-in invocation/Invocation:
Expand Down
26 changes: 13 additions & 13 deletions src/cli/cmds/org.toit
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ..config
import ..cache
import ..server-config
import ..organization
import ..artemis-servers.artemis-server show with-server ArtemisServerCli
import ..auth-providers.auth-provider show with-auth-provider AuthProvider
import ..utils

create-org-commands -> List:
Expand Down Expand Up @@ -226,7 +226,7 @@ with-org-server invocation/Invocation [block]:
server-config/ServerConfig := ?
server-config = get-server-from-config --key=CONFIG-ARTEMIS-DEFAULT-KEY --cli=cli

with-server server-config --cli=cli: | server/ArtemisServerCli |
with-auth-provider server-config --cli=cli: | server/AuthProvider |
server.ensure-authenticated: | error-message |
ui.abort "$error-message (artemis)."
block.call server
Expand All @@ -246,7 +246,7 @@ with-org-server-id invocation/Invocation [block]:
block.call server org-id

list-orgs invocation/Invocation -> none:
with-org-server invocation: | server/ArtemisServerCli |
with-org-server invocation: | server/AuthProvider |
orgs := server.get-organizations
invocation.cli.ui.emit-table --result
--header={"id": "ID", "name": "Name"}
Expand All @@ -257,16 +257,16 @@ list-orgs invocation/Invocation -> none:

add-org invocation/Invocation -> none:
should-make-default := invocation["default"]
with-org-server invocation: | server/ArtemisServerCli |
with-org-server invocation: | server/AuthProvider |
org := server.create-organization invocation["name"]
invocation.cli.ui.emit --info "Added organization $org.id - $org.name."
if should-make-default: make-default_ org --cli=invocation.cli

show-org invocation/Invocation -> none:
with-org-server-id invocation: | server/ArtemisServerCli org-id/Uuid |
with-org-server-id invocation: | server/AuthProvider org-id/Uuid |
print-org org-id server --cli=invocation.cli

print-org org-id/Uuid server/ArtemisServerCli --cli/Cli -> none:
print-org org-id/Uuid server/AuthProvider --cli/Cli -> none:
ui := cli.ui
org := server.get-organization org-id
if not org:
Expand Down Expand Up @@ -309,12 +309,12 @@ default-org invocation/Invocation -> none:
ui.emit --result "$org-id"
return

with-org-server invocation: | server/ArtemisServerCli |
with-org-server invocation: | server/AuthProvider |
print-org org-id server --cli=cli

return

with-org-server-id invocation: | server/ArtemisServerCli org-id/Uuid |
with-org-server-id invocation: | server/AuthProvider org-id/Uuid |
org/OrganizationDetailed? := null
exception := catch: org = server.get-organization org-id
if exception or not org:
Expand All @@ -337,14 +337,14 @@ update-org invocation/Invocation -> none:
if not name: ui.abort "No name provided."
if name == "": ui.abort "Name cannot be empty."

with-org-server-id invocation: | server/ArtemisServerCli org-id/Uuid |
with-org-server-id invocation: | server/AuthProvider org-id/Uuid |
server.update-organization org-id --name=name
ui.emit --info "Updated organization $org-id."

member-list invocation/Invocation -> none:
ui := invocation.cli.ui

with-org-server-id invocation: | server/ArtemisServerCli org-id/Uuid |
with-org-server-id invocation: | server/AuthProvider org-id/Uuid |
members := server.get-organization-members org-id
if invocation["id-only"]:
member-ids := members.map: "$it["id"]"
Expand Down Expand Up @@ -375,7 +375,7 @@ member-add invocation/Invocation -> none:
user-id := invocation["user-id"]
role := invocation["role"]

with-org-server-id invocation: | server/ArtemisServerCli org-id/Uuid|
with-org-server-id invocation: | server/AuthProvider org-id/Uuid|
existing-members := server.get-organization-members org-id
if (existing-members.any: it["id"] == user-id):
ui.abort "User $user-id is already a member of organization $org-id."
Expand All @@ -391,7 +391,7 @@ member-remove invocation/Invocation -> none:
user-id := invocation["user-id"]
force := invocation["force"]

with-org-server-id invocation: | server/ArtemisServerCli org-id/Uuid |
with-org-server-id invocation: | server/AuthProvider org-id/Uuid |
if not force:
current-user-id := server.get-current-user-id
if user-id == current-user-id:
Expand All @@ -405,7 +405,7 @@ member-set-role invocation/Invocation -> none:
user-id := invocation["user-id"]
role := invocation["role"]

with-org-server-id invocation: | server/ArtemisServerCli org-id/Uuid|
with-org-server-id invocation: | server/AuthProvider org-id/Uuid|
server.organization-member-set-role
--organization-id=org-id
--user-id=user-id
Expand Down
8 changes: 4 additions & 4 deletions src/cli/cmds/profile.toit
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import net
import ..config
import ..cache
import ..server-config
import ..artemis-servers.artemis-server show with-server ArtemisServerCli
import ..auth-providers.auth-provider show with-auth-provider AuthProvider

create-profile-commands -> List:
profile-cmd := Command "profile"
Expand Down Expand Up @@ -40,14 +40,14 @@ with-profile-server invocation/Invocation [block]:

server-config := get-server-from-config --key=CONFIG-ARTEMIS-DEFAULT-KEY --cli=cli

with-server server-config --cli=cli: | server/ArtemisServerCli |
with-auth-provider server-config --cli=cli: | server/AuthProvider |
server.ensure-authenticated: | error-message |
cli.ui.abort "$error-message (artemis)."
block.call server

show-profile invocation/Invocation:
ui := invocation.cli.ui
with-profile-server invocation: | server/ArtemisServerCli |
with-profile-server invocation: | server/AuthProvider |
profile := server.get-profile
if ui.wants-structured --kind=Ui.RESULT:
// We recreate the map, so we don't show unnecessary entries.
Expand All @@ -73,6 +73,6 @@ update-profile invocation/Invocation:
if not name:
ui.abort "No name specified."

with-profile-server invocation: | server/ArtemisServerCli |
with-profile-server invocation: | server/AuthProvider |
server.update-profile --name=name
ui.emit --info "Profile updated."
2 changes: 1 addition & 1 deletion src/cli/cmds/sdk.toit
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ..config
import ..cache
import ..fleet
import ..server-config
import ..artemis-servers.artemis-server show with-server ArtemisServerCli
import ..auth-providers.auth-provider show with-auth-provider AuthProvider
import .utils_

create-sdk-commands -> List:
Expand Down
3 changes: 0 additions & 3 deletions src/cli/fleet.toit
Original file line number Diff line number Diff line change
Expand Up @@ -1041,10 +1041,7 @@ class FleetWithDevices extends Fleet:
--device-id=device-id
--organization-id=organization-id
assert: device.id == device-id
hardware-id := device.hardware-id

// Insert an initial event mostly for testing purposes.
artemis.notify-created --hardware-id=hardware-id
broker.notify-created device

write-identity-file device --out-path=out-path
Expand Down
Loading
Loading