Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit 53a9fdb

Browse files
authored
v0.3.2
Adding BIM 360 helper page (/bim360) to allow users to access their BIM 360 models in the Reference App
2 parents 5ef789d + 42d5004 commit 53a9fdb

File tree

9 files changed

+585
-102
lines changed

9 files changed

+585
-102
lines changed

assets/images/autodesk-logo.svg

Lines changed: 11 additions & 79 deletions
Loading

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "forge-dataviz-iot-reference-app",
3-
"version": "0.3.1",
3+
"version": "0.3.2",
44
"description": "Autodesk Forge Viewer Data Visualization Sample Application",
55
"main": "server/localserver.js",
66
"scripts": {
@@ -59,7 +59,7 @@
5959
"socket.io": "^3.0.4",
6060
"socket.io-client": "^3.0.4",
6161
"uuid": "^8.3.2",
62-
"forge-dataviz-iot-data-modules": "0.1.10",
63-
"forge-dataviz-iot-react-components": "0.1.16"
62+
"forge-dataviz-iot-data-modules": "0.1.11",
63+
"forge-dataviz-iot-react-components": "0.1.17"
6464
}
65-
}
65+
}

server/ForgeThreeOAuth.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/////////////////////////////////////////////////////////////////////
2+
// Copyright (c) Autodesk, Inc. All rights reserved
3+
// Written by Forge Partner Development
4+
//
5+
// Permission to use, copy, modify, and distribute this software in
6+
// object code form for any purpose and without fee is hereby granted,
7+
// provided that the above copyright notice appears in all copies and
8+
// that both that copyright notice and the limited warranty and
9+
// restricted rights notice below appear in all supporting
10+
// documentation.
11+
//
12+
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
13+
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
14+
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
15+
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
16+
// UNINTERRUPTED OR ERROR FREE.
17+
/////////////////////////////////////////////////////////////////////
18+
19+
const { AuthClientThreeLegged } = require('forge-apis');
20+
21+
const config = {
22+
scopes: {
23+
// Required scopes for the server-side application
24+
internal: ['bucket:create', 'bucket:read', 'data:read', 'data:create', 'data:write'],
25+
// Required scope for the client-side viewer
26+
public: ['viewables:read']
27+
}
28+
};
29+
30+
class ThreeOAuth {
31+
constructor(session) {
32+
this._session = session;
33+
}
34+
35+
getClient(scopes = config.scopes.internal) {
36+
return new AuthClientThreeLegged(process.env.FORGE_CLIENT_ID, process.env.FORGE_CLIENT_SECRET, process.env.FORGE_CALLBACK_URL, scopes);
37+
}
38+
39+
isAuthorized() {
40+
return !!this._session.public_token;
41+
}
42+
43+
async getPublicToken() {
44+
if (this._isExpired()) {
45+
await this._refreshTokens();
46+
}
47+
48+
return {
49+
access_token: this._session.public_token,
50+
expires_in: this._expiresIn()
51+
};
52+
}
53+
54+
async getInternalToken() {
55+
if (this._isExpired()) {
56+
await this._refreshTokens();
57+
}
58+
59+
return {
60+
access_token: this._session.internal_token,
61+
expires_in: this._expiresIn()
62+
};
63+
}
64+
65+
// On callback, pass the CODE to this function, it will
66+
// get the internal and public tokens and store them
67+
// on the session
68+
async setCode(code) {
69+
const internalTokenClient = this.getClient(config.scopes.internal);
70+
const publicTokenClient = this.getClient(config.scopes.public);
71+
const internalCredentials = await internalTokenClient.getToken(code);
72+
const publicCredentials = await publicTokenClient.refreshToken(internalCredentials);
73+
74+
const now = new Date();
75+
this._session.internal_token = internalCredentials.access_token;
76+
this._session.public_token = publicCredentials.access_token;
77+
this._session.refresh_token = publicCredentials.refresh_token;
78+
this._session.expires_at = (now.setSeconds(now.getSeconds() + publicCredentials.expires_in));
79+
}
80+
81+
_expiresIn() {
82+
const now = new Date();
83+
const expiresAt = new Date(this._session.expires_at)
84+
return Math.round((expiresAt.getTime() - now.getTime()) / 1000);
85+
};
86+
87+
_isExpired() {
88+
return (new Date() > new Date(this._session.expires_at));
89+
}
90+
91+
async _refreshTokens() {
92+
let internalTokenClient = this.getClient(config.scopes.internal);
93+
let publicTokenClient = this.getClient(config.scopes.public);
94+
const internalCredentials = await internalTokenClient.refreshToken({ refresh_token: this._session.refresh_token });
95+
const publicCredentials = await publicTokenClient.refreshToken(internalCredentials);
96+
97+
const now = new Date();
98+
this._session.internal_token = internalCredentials.access_token;
99+
this._session.public_token = publicCredentials.access_token;
100+
this._session.refresh_token = publicCredentials.refresh_token;
101+
this._session.expires_at = (now.setSeconds(now.getSeconds() + publicCredentials.expires_in));
102+
}
103+
}
104+
105+
module.exports = ThreeOAuth;

server/app.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ var bodyParser = require("body-parser");
33

44
var app = express();
55
var router = require("express").Router();
6+
const cookieSession = require('cookie-session');
67

78
app.use(bodyParser.json());
9+
app.use(cookieSession({
10+
name: 'forge_session',
11+
keys: ['forge_secure_key'],
12+
maxAge: 14 * 24 * 60 * 60 * 1000 // 14 days, same as refresh token
13+
}));
814

915
var env = process.env.ENV || "local";
1016
var buildNumber = process.env.BUILD_NUMBER || "1";

server/env_template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ FORGE_CLIENT_ID=
22
FORGE_CLIENT_SECRET=
33
FORGE_ENV=AutodeskProduction
44
FORGE_API_URL=https://developer.api.autodesk.com
5+
FORGE_CALLBACK_URL=http://localhost:9000/oauth/callback
56

67
FORGE_BUCKET=
78
ENV=local

server/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"azure-iothub": "^1.13.0",
1919
"body-parser": "^1.19.0",
2020
"bufferutil": "^4.0.1",
21+
"cookie-session": "^1.4.0",
2122
"dotenv": "^8.2.0",
2223
"event-stream": "^4.0.1",
2324
"express": "4.16.2",
@@ -29,7 +30,7 @@
2930
"tween-functions": "^1.2.0",
3031
"utf-8-validate": "^5.0.2",
3132
"uuid": "^8.3.1",
32-
"forge-dataviz-iot-data-modules": "0.1.10"
33+
"forge-dataviz-iot-data-modules": "0.1.11"
3334
},
3435
"devDependencies": {}
35-
}
36+
}

0 commit comments

Comments
 (0)