2022-11-28 23:18:13 +08:00
|
|
|
|
using Autodesk.Forge;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-04-08 10:16:56 +08:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2022-11-28 23:18:13 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace forgeSample.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class OAuthController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
// As both internal & public tokens are used for all visitors
|
|
|
|
|
// we don't need to request a new token on every request, so let's
|
|
|
|
|
// cache them using static variables. Note we still need to refresh
|
|
|
|
|
// them after the expires_in time (in seconds)
|
|
|
|
|
private static dynamic InternalToken { get; set; }
|
|
|
|
|
private static dynamic PublicToken { get; set; }
|
2024-04-08 10:16:56 +08:00
|
|
|
|
private static IConfiguration _configuration;
|
|
|
|
|
|
|
|
|
|
public OAuthController(IConfiguration configuration) { _configuration = configuration; }
|
2022-11-28 23:18:13 +08:00
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
2024-04-08 10:16:56 +08:00
|
|
|
|
PublicToken = await Get2LeggedTokenAsync(_configuration, new Scope[] { Scope.ViewablesRead });
|
2022-11-28 23:18:13 +08:00
|
|
|
|
PublicToken.ExpiresAt = DateTime.UtcNow.AddSeconds(PublicToken.expires_in);
|
|
|
|
|
}
|
|
|
|
|
return PublicToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get access token with internal (write) scope
|
|
|
|
|
/// </summary>
|
2024-04-08 10:16:56 +08:00
|
|
|
|
public static async Task<dynamic> GetInternalAsync(IConfiguration Configuration)
|
2022-11-28 23:18:13 +08:00
|
|
|
|
{
|
|
|
|
|
if (InternalToken == null || InternalToken.ExpiresAt < DateTime.UtcNow)
|
|
|
|
|
{
|
2024-04-08 10:16:56 +08:00
|
|
|
|
InternalToken = await Get2LeggedTokenAsync(Configuration, new Scope[] { Scope.BucketCreate, Scope.BucketRead, Scope.BucketDelete, Scope.DataRead, Scope.DataWrite, Scope.DataCreate, Scope.CodeAll });
|
2022-11-28 23:18:13 +08:00
|
|
|
|
InternalToken.ExpiresAt = DateTime.UtcNow.AddSeconds(InternalToken.expires_in);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return InternalToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get the access token from Autodesk
|
|
|
|
|
/// </summary>
|
2024-04-08 10:16:56 +08:00
|
|
|
|
private static async Task<dynamic> Get2LeggedTokenAsync(IConfiguration Configuration, Scope[] scopes)
|
2022-11-28 23:18:13 +08:00
|
|
|
|
{
|
|
|
|
|
TwoLeggedApi oauth = new TwoLeggedApi();
|
|
|
|
|
string grantType = "client_credentials";
|
|
|
|
|
dynamic bearer = await oauth.AuthenticateAsync(
|
2024-04-08 10:16:56 +08:00
|
|
|
|
GetAppSetting(Configuration, "Forge:CLIENT_ID"),
|
|
|
|
|
GetAppSetting(Configuration, "Forge:CLIENT_SECRET"),
|
2022-11-28 23:18:13 +08:00
|
|
|
|
grantType,
|
|
|
|
|
scopes);
|
|
|
|
|
return bearer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads appsettings from web.config
|
|
|
|
|
/// </summary>
|
2024-04-08 10:16:56 +08:00
|
|
|
|
public static string GetAppSetting(IConfiguration Configuration, string settingKey)
|
2022-11-28 23:18:13 +08:00
|
|
|
|
{
|
2024-04-08 10:16:56 +08:00
|
|
|
|
return Configuration.GetValue<string>(settingKey).Trim();
|
2022-11-28 23:18:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|