diff --git a/platformio/commands/account/client.py b/platformio/commands/account/client.py index 5acef85e..5381dab9 100644 --- a/platformio/commands/account/client.py +++ b/platformio/commands/account/client.py @@ -61,6 +61,24 @@ class AccountClient(object): app.set_state_item("account", result) return result + def login_with_code(self, client_id, code, redirect_uri): + try: + self.fetch_authentication_token() + except: # pylint:disable=bare-except + pass + else: + raise exception.AccountAlreadyAuthenticated( + app.get_state_item("account", {}).get("email", "") + ) + + response = self._session.post( + self.api_base_url + "/v1/login/code", + data={"client_id": client_id, "code": code, "redirect_uri": redirect_uri}, + ) + result = self.raise_error_from_response(response) + app.set_state_item("account", result) + return result + def logout(self): try: refresh_token = self.get_refresh_token() diff --git a/platformio/commands/home/command.py b/platformio/commands/home/command.py index 229c875e..1eaad046 100644 --- a/platformio/commands/home/command.py +++ b/platformio/commands/home/command.py @@ -71,6 +71,7 @@ def cli(port, host, no_open, shutdown_timeout): from platformio.commands.home.rpc.handlers.os import OSRPC from platformio.commands.home.rpc.handlers.piocore import PIOCoreRPC from platformio.commands.home.rpc.handlers.project import ProjectRPC + from platformio.commands.home.rpc.handlers.account import AccountRPC from platformio.commands.home.rpc.server import JSONRPCServerFactory from platformio.commands.home.web import WebRoot @@ -81,6 +82,7 @@ def cli(port, host, no_open, shutdown_timeout): factory.addHandler(OSRPC(), namespace="os") factory.addHandler(PIOCoreRPC(), namespace="core") factory.addHandler(ProjectRPC(), namespace="project") + factory.addHandler(AccountRPC(), namespace="account") contrib_dir = get_core_package_dir("contrib-piohome") if not isdir(contrib_dir): diff --git a/platformio/commands/home/rpc/handlers/account.py b/platformio/commands/home/rpc/handlers/account.py new file mode 100644 index 00000000..911006bc --- /dev/null +++ b/platformio/commands/home/rpc/handlers/account.py @@ -0,0 +1,29 @@ +# Copyright (c) 2014-present PlatformIO +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import jsonrpc # pylint: disable=import-error + +from platformio.commands.account.client import AccountClient + + +class AccountRPC(object): + @staticmethod + def call_client(method, *args, **kwargs): + try: + client = AccountClient() + return getattr(client, method)(*args, **kwargs) + except Exception as e: # pylint: disable=bare-except + raise jsonrpc.exceptions.JSONRPCDispatchException( + code=4003, message="PIO Account Call Error", data=str(e) + )