Skip to content

Commit

Permalink
Use dummy sample showcase user for no-signin access. (imodeljs#11)
Browse files Browse the repository at this point in the history
* Use dummy sample showcase user for no-signin access.

* Add copyright notice for NoSignInIAuthClien.ts.

* Pull from imjs_sample_showcase_user if exists, default to dev services API.

* Set homepage to './' for relative sources. Buttons needs to also point to relative directory.

* Add .nuspec for deployment.
  • Loading branch information
BaoTon committed Jun 10, 2020
1 parent 0d7177f commit 833c051
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 7 deletions.
18 changes: 18 additions & 0 deletions NugetPackage.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<package >
<metadata>
<id>iModeljsSampleShowcase</id>
<version>$version$</version>
<title>iModeljs Samples Showcase</title>
<authors>IMSS Dev</authors>
<owners>IMSS Dev</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>iModeljs Samples Showcase</description>
<releaseNotes></releaseNotes>
<copyright>Copyright 2020</copyright>
<tags></tags>
</metadata>
<files>
<file src="build\**" target="content\sample-showcase" />
</files>
</package>
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"lint": "tslint -p . 1>&2",
"clean": "rimraf build"
},
"homepage": "./",
"eslintConfig": {
"extends": "react-app"
},
Expand Down
10 changes: 5 additions & 5 deletions src/Components/Viewport/ViewportAndNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export class ViewportAndNavigation extends React.PureComponent<ViewportAndNaviga
const toolbar = () => {
return (
<div className="toolbar">
<a href="/#" title={SelectionTool.flyover} onClick={select}><span className="icon icon-cursor"></span></a>
<a href="/#" title={FitViewTool.flyover} onClick={fitView}><span className="icon icon-fit-to-view"></span></a>
<a href="/#" title={RotateViewTool.flyover} onClick={rotate}><span className="icon icon-gyroscope"></span></a>
<a href="/#" title={PanViewTool.flyover} onClick={pan}><span className="icon icon-hand-2"></span></a>
<a href="/#" title={ZoomViewTool.flyover} onClick={zoom}><span className="icon icon-zoom"></span></a>
<a href="./#" title={SelectionTool.flyover} onClick={select}><span className="icon icon-cursor"></span></a>
<a href="./#" title={FitViewTool.flyover} onClick={fitView}><span className="icon icon-fit-to-view"></span></a>
<a href="./#" title={RotateViewTool.flyover} onClick={rotate}><span className="icon icon-gyroscope"></span></a>
<a href="./#" title={PanViewTool.flyover} onClick={pan}><span className="icon icon-hand-2"></span></a>
<a href="./#" title={ZoomViewTool.flyover} onClick={zoom}><span className="icon icon-zoom"></span></a>
</div>
);
};
Expand Down
61 changes: 61 additions & 0 deletions src/NoSignInIAuthClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { AuthStatus, BeEvent, BentleyError, ClientRequestContext } from "@bentley/bentleyjs-core";
import { AccessToken } from "@bentley/itwin-client";
import { FrontendAuthorizationClient } from "@bentley/frontend-authorization-client";

export class NoSignInIAuthClient implements FrontendAuthorizationClient {
public readonly onUserStateChanged: BeEvent<(token: AccessToken | undefined) => void>;
protected _accessToken?: AccessToken;

constructor() {
this.onUserStateChanged = new BeEvent();
}

public async signIn(requestContext?: ClientRequestContext): Promise<void> {
if (requestContext) {
requestContext.enter();
}
await this.getAccessToken();
}
public async signOut(requestContext?: ClientRequestContext): Promise<void> {
if (requestContext) {
requestContext.enter();
}
this._accessToken = undefined;
}

public get isAuthorized(): boolean {
return this.hasSignedIn;
}

public get hasExpired(): boolean {
return !this._accessToken;
}

public get hasSignedIn(): boolean {
return !!this._accessToken;
}

public async generateTokenString(userURL: string, requestContext?: ClientRequestContext) {
if (requestContext) {
requestContext.enter();
}

const response = await fetch(userURL);
const tokenJson = {
...await response.json(),
_userInfo: { id: "MockId" },
};
this._accessToken = AccessToken.fromJson(tokenJson);
}

public async getAccessToken(): Promise<AccessToken> {
if (!this._accessToken)
throw new BentleyError(AuthStatus.Error, "Cannot get access token");

return this._accessToken;
}
}
13 changes: 11 additions & 2 deletions src/SampleBaseApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { ClientRequestContext, Config, Id64String } from "@bentley/bentleyjs-core";
import { BrowserAuthorizationCallbackHandler, BrowserAuthorizationClient, BrowserAuthorizationClientConfiguration } from "@bentley/frontend-authorization-client";
import { /* BrowserAuthorizationCallbackHandler */ BrowserAuthorizationClient /* BrowserAuthorizationClientConfiguration */ } from "@bentley/frontend-authorization-client";
import { UrlDiscoveryClient } from "@bentley/itwin-client";
import { FrontendRequestContext, IModelApp, IModelAppOptions, IModelConnection, TileAdmin } from "@bentley/imodeljs-frontend";
import { BentleyCloudRpcManager, BentleyCloudRpcParams, IModelReadRpcInterface, IModelTileRpcInterface } from "@bentley/imodeljs-common";
Expand All @@ -12,6 +12,7 @@ import { Presentation } from "@bentley/presentation-frontend";
import { UiComponents } from "@bentley/ui-components";
import { ShowcaseToolAdmin } from "./api/showcasetooladmin";
import { ShowcaseNotificationManager } from "./api/Notifications/NotificationManager";
import { NoSignInIAuthClient } from "./NoSignInIAuthClient";

// Boiler plate code
export interface SampleContext {
Expand Down Expand Up @@ -73,6 +74,7 @@ export class SampleBaseApp {

private static async initializeOidc() {
// Gather configuration out of the environment
/* Uncomment this block to enable signin.
const clientId = Config.App.get("imjs_frontend_sample_client_id", "imodeljs-spa-samples-2686");
const redirectUri = Config.App.get("imjs_frontend_sample_redirect_uri", "http://localhost:3000/signin-callback.html");
const scope = Config.App.get("imjs_frontend_sample_scope", "openid email profile organization imodelhub context-registry-service:read-only product-settings-service general-purpose-imodeljs-backend imodeljs-router");
Expand All @@ -82,7 +84,14 @@ export class SampleBaseApp {
await BrowserAuthorizationCallbackHandler.handleSigninCallback(oidcConfig.redirectUri);
// Setup the IModelApp authorization client
IModelApp.authorizationClient = new BrowserAuthorizationClient(oidcConfig);
IModelApp.authorizationClient = new BrowserAuthorizationClient(oidcConfig); ..
*/

// Comment this block to disable no-signin.
const authClient = new NoSignInIAuthClient();
const userURL = Config.App.get("imjs_sample_showcase_user", "https://prod-imodeldeveloperservices-eus.azurewebsites.net/api/v0/sampleShowcaseUser");
await authClient.generateTokenString(userURL, new ClientRequestContext());
IModelApp.authorizationClient = authClient;

try {
await SampleBaseApp.oidcClient.signInSilent(new ClientRequestContext());
Expand Down

0 comments on commit 833c051

Please sign in to comment.