From 5365ab66c0a23dfdb16ff44bb05a81aaff680bb6 Mon Sep 17 00:00:00 2001 From: "jay.chang" Date: Mon, 8 Apr 2024 10:16:56 +0800 Subject: [PATCH] =?UTF-8?q?[Backend]=E4=BF=AE=E6=94=B9forge=E7=9B=B8?= =?UTF-8?q?=E9=97=9C=E5=8F=83=E6=95=B8=E7=9A=84=E4=BD=8D=E7=BD=AE=E5=8F=8A?= =?UTF-8?q?=E5=B0=8D=E6=87=89=E7=9A=84=E6=92=88=E5=8F=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ModelDerivativeController.cs | 6 +++++- Backend/Controllers/OAuthController.cs | 20 +++++++++++-------- Backend/Controllers/OSSController.cs | 14 ++++++++----- Backend/appsettings.Development.json | 4 ++++ Backend/appsettings.json | 4 ++++ Backend/wwwroot/forgeDemo.html | 4 ++-- 6 files changed, 36 insertions(+), 16 deletions(-) diff --git a/Backend/Controllers/ModelDerivativeController.cs b/Backend/Controllers/ModelDerivativeController.cs index 951cae1..db62d9d 100644 --- a/Backend/Controllers/ModelDerivativeController.cs +++ b/Backend/Controllers/ModelDerivativeController.cs @@ -1,6 +1,7 @@ using Autodesk.Forge; using Autodesk.Forge.Model; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; using System.Collections.Generic; using System.Threading.Tasks; @@ -9,6 +10,9 @@ namespace forgeSample.Controllers [ApiController] public class ModelDerivativeController : ControllerBase { + private static IConfiguration _configuration; + + public ModelDerivativeController(IConfiguration configuration) { _configuration = configuration; } /// /// Start the translation job for a give bucketKey/objectName /// @@ -18,7 +22,7 @@ namespace forgeSample.Controllers [Route("api/forge/modelderivative/jobs")] public async Task TranslateObject([FromBody] TranslateObjectModel objModel) { - dynamic oauth = await OAuthController.GetInternalAsync(); + dynamic oauth = await OAuthController.GetInternalAsync(_configuration); // prepare the payload List outputs = new List() diff --git a/Backend/Controllers/OAuthController.cs b/Backend/Controllers/OAuthController.cs index ddf54b5..0ef6173 100644 --- a/Backend/Controllers/OAuthController.cs +++ b/Backend/Controllers/OAuthController.cs @@ -1,5 +1,6 @@ using Autodesk.Forge; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; using System; using System.Threading.Tasks; @@ -14,6 +15,9 @@ namespace forgeSample.Controllers // them after the expires_in time (in seconds) private static dynamic InternalToken { get; set; } private static dynamic PublicToken { get; set; } + private static IConfiguration _configuration; + + public OAuthController(IConfiguration configuration) { _configuration = configuration; } /// /// Get access token with public (viewables:read) scope @@ -24,7 +28,7 @@ namespace forgeSample.Controllers { if (PublicToken == null || PublicToken.ExpiresAt < DateTime.UtcNow) { - PublicToken = await Get2LeggedTokenAsync(new Scope[] { Scope.ViewablesRead }); + PublicToken = await Get2LeggedTokenAsync(_configuration, new Scope[] { Scope.ViewablesRead }); PublicToken.ExpiresAt = DateTime.UtcNow.AddSeconds(PublicToken.expires_in); } return PublicToken; @@ -33,11 +37,11 @@ namespace forgeSample.Controllers /// /// Get access token with internal (write) scope /// - public static async Task GetInternalAsync() + public static async Task GetInternalAsync(IConfiguration Configuration) { if (InternalToken == null || InternalToken.ExpiresAt < DateTime.UtcNow) { - InternalToken = await Get2LeggedTokenAsync(new Scope[] { Scope.BucketCreate, Scope.BucketRead, Scope.BucketDelete, Scope.DataRead, Scope.DataWrite, Scope.DataCreate, Scope.CodeAll }); + InternalToken = await Get2LeggedTokenAsync(Configuration, new Scope[] { Scope.BucketCreate, Scope.BucketRead, Scope.BucketDelete, Scope.DataRead, Scope.DataWrite, Scope.DataCreate, Scope.CodeAll }); InternalToken.ExpiresAt = DateTime.UtcNow.AddSeconds(InternalToken.expires_in); } @@ -47,13 +51,13 @@ namespace forgeSample.Controllers /// /// Get the access token from Autodesk /// - private static async Task Get2LeggedTokenAsync(Scope[] scopes) + private static async Task Get2LeggedTokenAsync(IConfiguration Configuration, Scope[] scopes) { TwoLeggedApi oauth = new TwoLeggedApi(); string grantType = "client_credentials"; dynamic bearer = await oauth.AuthenticateAsync( - GetAppSetting("FORGE_CLIENT_ID"), - GetAppSetting("FORGE_CLIENT_SECRET"), + GetAppSetting(Configuration, "Forge:CLIENT_ID"), + GetAppSetting(Configuration, "Forge:CLIENT_SECRET"), grantType, scopes); return bearer; @@ -62,9 +66,9 @@ namespace forgeSample.Controllers /// /// Reads appsettings from web.config /// - public static string GetAppSetting(string settingKey) + public static string GetAppSetting(IConfiguration Configuration, string settingKey) { - return Environment.GetEnvironmentVariable(settingKey).Trim(); + return Configuration.GetValue(settingKey).Trim(); } } } \ No newline at end of file diff --git a/Backend/Controllers/OSSController.cs b/Backend/Controllers/OSSController.cs index 6a5cb26..f2c1e6f 100644 --- a/Backend/Controllers/OSSController.cs +++ b/Backend/Controllers/OSSController.cs @@ -3,6 +3,7 @@ using Autodesk.Forge.Model; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; @@ -13,9 +14,12 @@ namespace forgeSample.Controllers public class OSSController : ControllerBase { private IWebHostEnvironment _env; - public OSSController(IWebHostEnvironment env) { _env = env; } - public string ClientId { get { return OAuthController.GetAppSetting("FORGE_CLIENT_ID").ToLower(); } } + private static IConfiguration _configuration; + public OSSController(IWebHostEnvironment env, IConfiguration configuration) { _env = env; _configuration = configuration; } + public string ClientId { get { return _configuration.GetValue("Forge:CLIENT_ID").ToLower(); } } + + /// /// Return list of buckets (id=#) or list of objects (id=bucketKey) /// @@ -24,7 +28,7 @@ namespace forgeSample.Controllers public async Task> GetOSSAsync(string id) { IList nodes = new List(); - dynamic oauth = await OAuthController.GetInternalAsync(); + dynamic oauth = await OAuthController.GetInternalAsync(_configuration); if (id == "#") // root { @@ -81,7 +85,7 @@ namespace forgeSample.Controllers public async Task CreateBucket([FromBody] CreateBucketModel bucket) { BucketsApi buckets = new BucketsApi(); - dynamic token = await OAuthController.GetInternalAsync(); + dynamic token = await OAuthController.GetInternalAsync(_configuration); buckets.Configuration.AccessToken = token.access_token; PostBucketsPayload bucketPayload = new PostBucketsPayload(string.Format("{0}-{1}", ClientId, bucket.bucketKey.ToLower()), null, PostBucketsPayload.PolicyKeyEnum.Transient); @@ -111,7 +115,7 @@ namespace forgeSample.Controllers // get the bucket... - dynamic oauth = await OAuthController.GetInternalAsync(); + dynamic oauth = await OAuthController.GetInternalAsync(_configuration); ObjectsApi objects = new ObjectsApi(); objects.Configuration.AccessToken = oauth.access_token; diff --git a/Backend/appsettings.Development.json b/Backend/appsettings.Development.json index f99be74..1a61e16 100644 --- a/Backend/appsettings.Development.json +++ b/Backend/appsettings.Development.json @@ -82,5 +82,9 @@ "Account": "FMiVG4sIeDFmAUc/8Hn/kw==", //巨蛋 "Password": "4+ussQ8rHohjPWpNvoujJQ==" //巨蛋 } + }, + "Forge": { + "CLIENT_ID": "kDOSvxcoLPCFKTncWV9GxgFcGFMkUz9W", + "CLIENT_SECRET": "peaXsHcAorGNdZYd" } } diff --git a/Backend/appsettings.json b/Backend/appsettings.json index d276a82..512cbeb 100644 --- a/Backend/appsettings.json +++ b/Backend/appsettings.json @@ -64,5 +64,9 @@ "Account": "FMiVG4sIeDFmAUc/8Hn/kw==", //巨蛋 "Password": "4+ussQ8rHohjPWpNvoujJQ==" //巨蛋 } + }, + "Forge": { + "CLIENT_ID": "kDOSvxcoLPCFKTncWV9GxgFcGFMkUz9W", + "CLIENT_SECRET": "peaXsHcAorGNdZYd" } } diff --git a/Backend/wwwroot/forgeDemo.html b/Backend/wwwroot/forgeDemo.html index 2a681f4..8ac57ed 100644 --- a/Backend/wwwroot/forgeDemo.html +++ b/Backend/wwwroot/forgeDemo.html @@ -287,8 +287,8 @@ //2023-05 三菱 dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6em80emd1eWFsaGpybDVvODF2YTM5cGtkZ3I4MndhbXEtbWl6dWJpc2hpLyVFMyU4MCU5MCVFNSU4Ri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2Zta3V6OXctZGVtby8lRTMlODAlOTAlRTklQkIlOTElRTQlQjglQjglRTMlODAlOTElRTQlQkElOEMlRTUlQkIlQTArJUU0JUI4JTg5JUU1JUJCJUEwKCVFNiVCMCVCNCVFNyVBRSVBMSkwMzE2Lm53ZA + launchViewer("dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6a2Rvc3Z4Y29scGNma3RuY3d2OWd4Z2ZjZ2Zta3V6OXctZGVtby8lRTMlODAlOTAlRTklQkIlOTElRTQlQjglQjglRTMlODAlOTElRTQlQkElOEMlRTUlQkIlQTArJUU0JUI4JTg5JUU1JUJCJUEwKCVFNiVCMCVCNCVFNyVBRSVBMSkwMzE2Lm53ZA"); }); function move1Floor() { setElevatorSpeed(0.2);