Beispiel #1
0
    def environment_id(self) -> str:
        if self.organization_name and not self.environment_name:
            raise ValueError(
                "Must specify --environment when you specify --organization")
        if self.environment_name:
            env = get_environment_by_name(self.organization_id,
                                          self.environment_name)
            return env["uid"]
        if self.cfg.environment_id and not self.ignore_cfg_environment:
            return self.cfg.environment_id

        environments = list(paginated_environments(self.organization_id))
        envs_by_name = {env["name"]: env for env in environments}

        if len(environments) == 1:
            uid = environments[0]["uid"]
            update_devkit_config(environment_id=uid)
            return uid

        env_name = prompt_choices(
            "Available environments",
            "Select an environment",
            choices=envs_by_name.keys(),
        )
        return envs_by_name[env_name]["uid"]
Beispiel #2
0
def set_tmp_dir(tmp_dir: Path, create_devkit_config: bool = True) -> Path:
    cfg_pth = Path(tmp_dir) / ".test-config.json"
    os.environ[DEVKIT_CONFIG_ENV_VAR] = str(cfg_pth)
    if create_devkit_config:
        update_devkit_config(
            token="test-token",
            organization_id="test-org-uid",
            environment_id="test-env-uid",
        )
    return cfg_pth
Beispiel #3
0
 def organization_id(self) -> str:
     if self.organization_name:
         return get_organization_by_name(self.organization_name)["uid"]
     if self.cfg.organization_id:
         return self.cfg.organization_id
     organizations = list(paginated_organizations())
     orgs_by_name = {org["name"]: org for org in organizations}
     if len(organizations) == 1:
         org = organizations[0]
         update_devkit_config(organization_id=org["uid"])
     else:
         org_name = prompt_choices(
             "Available organizations",
             "Select an organization",
             choices=orgs_by_name.keys(),
         )
         org = orgs_by_name[org_name]
     return org["uid"]
Beispiel #4
0
def login():
    """Log in to your Patterns account"""

    with abort_on_error("Login failed"):
        login_service.login()

    ids = IdLookup(ignore_local_cfg=True)
    with abort_on_error("Fetching account failed"):
        update_devkit_config(organization_id=ids.organization_id,
                             environment_id=ids.environment_id)

    with abort_on_error("Fetching user profile failed"):
        profile = me()

    sprint(
        f"\n[success]Logged in to Patterns organization [b]{ids.organization_name}[/b] "
        f"as [b]{profile['username']}[/b] ([b]{profile['email']}[/b])")
    sprint(f"\n[info]Your login information is stored at "
           f"{get_devkit_config_path().as_posix()}")
    sprint(f"\n[info]If you want to create a new graph, run "
           f"[code]patterns create graph[/code] get started")
Beispiel #5
0
 def handle_callback(self, parsed_url: ParseResult):
     update_devkit_config(refresh=None, token=None, auth_server=None)
     self.finish_with_success("Successfully logged out",
                              "You have successfully logged out")
Beispiel #6
0
            )

        # Exchange code for access & refresh tokens
        response = requests.post(
            f"https://{login_config.auth_server.domain}/oauth/token",
            json={
                "client_id": login_config.auth_server.devkit_client_id,
                "code_verifier": login_config.code_verifier,
                "code": code,
                "grant_type": "authorization_code",
                "redirect_uri": login_config.redirect_url,
            },
        )
        response.raise_for_status()

        json = response.json()
        if "refresh_token" not in json or "access_token" not in json:
            return self.finish_with_error(
                401, f"We did not receive a valid authorization result: {json}"
            )

        update_devkit_config(
            auth_server=login_config.auth_server,
            refresh=json["refresh_token"],
            token=json["access_token"],
        )

        return self.finish_with_success(
            "Login successful", "Successfully logged in!  You may close this window."
        )