Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reroute cloud through IPC channel #10285

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions localtypings/pxtarget.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ declare namespace pxt {
timeMachineDiffInterval?: number; // An interval in milliseconds at which to take diffs to store in project history. Defaults to 5 minutes
timeMachineSnapshotInterval?: number; // An interval in milliseconds at which to take full project snapshots in project history. Defaults to 15 minutes
adjustBlockContrast?: boolean; // If set to true, all block colors will automatically be adjusted to have a contrast ratio of 4.5 with text
ipcIdentityProxy?: boolean; // for use with the in game minecraft experience only. If true, proxies all identity API requests through the ipc channel
}

interface DownloadDialogTheme {
Expand Down
84 changes: 84 additions & 0 deletions localtypings/pxteditor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ declare namespace pxt.editor {
| "serviceworkerregistered"
| "runeval"
| "precachetutorial"
| "cloudproxy"

// package extension messasges
| ExtInitializeType
Expand Down Expand Up @@ -1368,6 +1369,89 @@ declare namespace pxt.editor {
}

type AssetEditorEvent = AssetEditorRequestSaveEvent | AssetEditorReadyEvent;

type CloudProject = {
id: string;
shareId?: string;
header: string;
text: string;
version: string;
};

interface BaseCloudProxyRequest extends EditorMessageRequest {
action: "cloudproxy";
operation: string;
response: true;
}

interface CloudProxyUserRequest extends BaseCloudProxyRequest {
operation: "user";
}

interface CloudProxyListRequest extends BaseCloudProxyRequest {
operation: "list";
headerIds?: string[];
}

interface CloudProxyGetRequest extends BaseCloudProxyRequest {
operation: "get";
headerId: string;
}

interface CloudProxySetRequest extends BaseCloudProxyRequest {
operation: "set";
project: CloudProject;
}

interface CloudProxyDeleteRequest extends BaseCloudProxyRequest {
operation: "delete";
headerId: string;
}

type CloudProxyRequest =
| CloudProxyUserRequest
| CloudProxyListRequest
| CloudProxyGetRequest
| CloudProxySetRequest
| CloudProxyDeleteRequest;


interface BaseCloudProxyResponse extends EditorMessageResponse {
action: "cloudproxy";
operation: string;
resp: pxt.auth.ApiResult<any>;
}

interface CloudProxyUserResponse extends BaseCloudProxyResponse {
operation: "user";
}

interface CloudProxyListResponse extends BaseCloudProxyResponse {
operation: "list";
resp: pxt.auth.ApiResult<CloudProject[]>;
}

interface CloudProxyGetResponse extends BaseCloudProxyResponse {
operation: "get";
resp: pxt.auth.ApiResult<CloudProject>;
}

interface CloudProxySetResponse extends BaseCloudProxyResponse {
operation: "set";
resp: pxt.auth.ApiResult<string>;
}

interface CloudProxyDeleteResponse extends BaseCloudProxyResponse {
operation: "delete";
resp: pxt.auth.ApiResult<string>;
}

type CloudProxyResponse =
| CloudProxyUserResponse
| CloudProxyListResponse
| CloudProxyGetResponse
| CloudProxySetResponse
| CloudProxyDeleteResponse;
}

declare namespace pxt.workspace {
Expand Down
11 changes: 11 additions & 0 deletions pxtlib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ namespace pxt.auth {
return await setLocalStorageValueAsync(CSRF_TOKEN_KEY, token);
}
export async function hasAuthTokenAsync(): Promise<boolean> {
if (proxyIdentityThroughIPC()) {
cachedHasAuthToken = true;
return true;
}
return !!(await getAuthTokenAsync());
}
async function delAuthTokenAsync(): Promise<void> {
Expand Down Expand Up @@ -748,9 +752,16 @@ namespace pxt.auth {
}

export function hasIdentity(): boolean {
if (proxyIdentityThroughIPC()) {
return true;
}
return !authDisabled && !pxt.BrowserUtils.isPxtElectron() && identityProviders().length > 0;
}

export function proxyIdentityThroughIPC(): boolean {
return pxt.appTarget.appTheme.ipcIdentityProxy;
}

function idpEnabled(idp: pxt.IdentityProviderId): boolean {
return identityProviders().filter(prov => prov.id === idp).length > 0;
}
Expand Down
249 changes: 249 additions & 0 deletions webapp/public/cloudframe.html

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions webapp/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import { Tour } from "./components/onboarding/Tour";
import { parseTourStepsAsync } from "./onboarding";
import { initGitHubDb } from "./idbworkspace";
import { BlockDefinition, CategoryNameID } from "./toolbox";
import { MinecraftAuthClient } from "./minecraftAuthClient";

pxt.blocks.requirePxtBlockly = () => pxtblockly as any;
pxt.blocks.requireBlockly = () => Blockly;
Expand Down Expand Up @@ -5963,11 +5964,15 @@ document.addEventListener("DOMContentLoaded", async () => {
pxt.blocks.showBlockIdInTooltip = true;
}

initGitHubDb();

pxt.perf.measureStart("setAppTarget");
pkg.setupAppTarget((window as any).pxtTargetBundle);

initGitHubDb();

if (pxt.auth.proxyIdentityThroughIPC()) {
auth.overrideAuthClient(() => new MinecraftAuthClient());
}

// DO NOT put any async code before this line! The serviceworker must be initialized before
// the window load event fires
appcache.init(() => theEditor.reloadEditor());
Expand Down
10 changes: 8 additions & 2 deletions webapp/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as core from "./core";
import * as data from "./data";
import * as cloud from "./cloud";
import * as workspace from "./workspace";
import { MinecraftAuthClient } from "./minecraftAuthClient";

/**
* Virtual API keys
Expand Down Expand Up @@ -35,7 +36,7 @@ export class Component<TProps, TState> extends data.Component<TProps, TState> {
}
}

class AuthClient extends pxt.auth.AuthClient {
export class AuthClient extends pxt.auth.AuthClient {
protected async onSignedIn(): Promise<void> {
const state = await pxt.auth.getUserStateAsync();
core.infoNotification(lf("Signed in: {0}", pxt.auth.userName(state.profile)));
Expand Down Expand Up @@ -151,12 +152,13 @@ function initVirtualApi() {
}

let authClientPromise: Promise<AuthClient>;
let authClientFactory = () => new AuthClient();

async function clientAsync(): Promise<AuthClient | undefined> {
if (!pxt.auth.hasIdentity()) { return undefined; }
if (authClientPromise) return authClientPromise;
authClientPromise = new Promise<AuthClient>(async (resolve, reject) => {
const cli = new AuthClient();
const cli = authClientFactory();
await cli.initAsync();
await cli.authCheckAsync();
await cli.initialUserPreferencesAsync();
Expand All @@ -165,6 +167,10 @@ async function clientAsync(): Promise<AuthClient | undefined> {
return authClientPromise;
}

export function overrideAuthClient(factory: () => AuthClient) {
authClientFactory = factory;
}

export function hasIdentity(): boolean {
return pxt.auth.hasIdentity();
}
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/headerbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export class HeaderBar extends data.Component<ISettingsProps, {}> {
const { home, header, tutorialOptions } = this.props.parent.state;
const isController = pxt.shell.isControllerMode();
const isNativeHost = cmds.isNativeHost();
const hasIdentity = auth.hasIdentity();
const hasIdentity = auth.hasIdentity() && !pxt.auth.proxyIdentityThroughIPC();
const activeEditor = this.props.parent.isPythonActive() ? "Python"
: (this.props.parent.isJavaScriptActive() ? "JavaScript" : "Blocks");

Expand Down
Loading