2022-11-14 09:41:12 +08:00
|
|
|
|
using Autodesk.Forge;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace FrontendWebApi.ApiControllers
|
|
|
|
|
{
|
|
|
|
|
public class OAuthController : ControllerBase //MyBaseApiController<OAuthController>//Controller
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static dynamic InternalToken { get; set; }
|
|
|
|
|
private static dynamic PublicToken { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get access token with public (viewables:read) scope
|
|
|
|
|
/// </summary>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("api/forge/oauth/token")]
|
|
|
|
|
public async Task<dynamic> GetPublicAsync()
|
|
|
|
|
{
|
|
|
|
|
if (PublicToken == null || PublicToken.ExpiresAt < DateTime.UtcNow)
|
|
|
|
|
{
|
|
|
|
|
PublicToken = await Get2LeggedTokenAsync(new Scope[] { Scope.ViewablesRead });
|
|
|
|
|
PublicToken.ExpiresAt = DateTime.UtcNow.AddSeconds(PublicToken.expires_in);
|
|
|
|
|
}
|
|
|
|
|
return PublicToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get access token with internal (write) scope
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static async Task<dynamic> GetInternalAsync()
|
|
|
|
|
{
|
|
|
|
|
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.ExpiresAt = DateTime.UtcNow.AddSeconds(InternalToken.expires_in);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return InternalToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get the access token from Autodesk
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static async Task<dynamic> Get2LeggedTokenAsync(Scope[] scopes)
|
|
|
|
|
{
|
|
|
|
|
TwoLeggedApi oauth = new TwoLeggedApi();
|
|
|
|
|
string grantType = "client_credentials";
|
|
|
|
|
dynamic bearer = await oauth.AuthenticateAsync(
|
2023-05-15 16:26:36 +08:00
|
|
|
|
"zO4ZGuyALhjrL5o81vA39Pkdgr82WAmq",
|
|
|
|
|
"Bz5KyXqP6UqgGhxm",
|
2022-11-14 09:41:12 +08:00
|
|
|
|
grantType,
|
|
|
|
|
scopes);
|
|
|
|
|
return bearer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads appsettings from web.config
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static string GetAppSetting(string settingKey)
|
|
|
|
|
{
|
|
|
|
|
return Environment.GetEnvironmentVariable(settingKey).Trim();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|