Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add authentication feature to Dynamo Sandbox #13445

Merged
merged 7 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/DynamoApplications/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/DynamoApplications/Properties/Resources.en-US.resx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Expand Down Expand Up @@ -130,6 +130,6 @@
<value>Error Loading A Dependency</value>
</data>
<data name="SplashScreenPreLoadingAsm" xml:space="preserve">
<value>Preloading ASM...</value>
<value>Preloading Geometry library</value>
</data>
</root>
</root>
2 changes: 1 addition & 1 deletion src/DynamoApplications/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,6 @@
<value>Error Loading A Dependency</value>
</data>
<data name="SplashScreenPreLoadingAsm" xml:space="preserve">
<value>Preloading ASM...</value>
<value>Preloading Geometry library</value>
</data>
</root>
2 changes: 1 addition & 1 deletion src/DynamoApplications/StartupUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ private static bool PreloadASM(string asmPath, out string geometryFactoryPath, o
HostAnalyticsInfo = info,
CLIMode = CLImode
};

config.AuthProvider = CLImode ? null : new Core.IDSDKManager();
config.UpdateManager = CLImode ? null : InitializeUpdateManager();
config.StartInTestMode = CLImode;
config.PathResolver = CLImode ? new CLIPathResolver(preloaderLocation) as IPathResolver : new SandboxPathResolver(preloaderLocation) as IPathResolver;
Expand Down
2 changes: 2 additions & 0 deletions src/DynamoCore/App.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="IDSDK_CLIENT_ID" value="IAtF1TBSlCeGqWAXKsBkcZBBwomALZsq"/>
<add key="IDSDK_ENVIRONMENT" value=""/>
QilongTang marked this conversation as resolved.
Show resolved Hide resolved
</appSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/></startup></configuration>
7 changes: 6 additions & 1 deletion src/DynamoCore/Core/AuthenticationManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

using Greg;
using Greg.AuthProviders;
Expand Down Expand Up @@ -34,6 +34,11 @@ internal LoginState LoginState
get { return HasAuthProvider ? authProvider.LoginState : LoginState.LoggedOut; }
}

internal bool IsLoggedIn()
{
return HasAuthProvider && authProvider.LoginState == LoginState.LoggedIn ? true : false;
}

/// <summary>
/// The username of the current user, if logged in. Otherwise null
/// </summary>
Expand Down
241 changes: 241 additions & 0 deletions src/DynamoCore/Core/IDSDKManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
using System;
using Greg;
using Greg.AuthProviders;
using RestSharp;

namespace Dynamo.Core
{
/// <summary>
/// The class to provide auth APIs for IDSDK related methods.
/// </summary>
public class IDSDKManager : IOAuth2AuthProvider
{
/// <summary>
/// Used by the auth provider to request authentication.
/// </summary>
public event Func<object, bool> RequestLogin;
/// <summary>
/// Tracks any change in the login status.
/// </summary>
public event Action<LoginState> LoginStateChanged;

/// <summary>
/// Returns the login status of the current session.
/// </summary>
/// <returns>LoginState Enum value</returns>
public LoginState LoginState
{
get
{
var result = IDSDK_IsLoggedIn();
return result ? LoginState.LoggedIn : LoginState.LoggedOut;
}
}

/// <summary>
/// Returns the login status of the current session.
/// </summary>
/// <returns>Boolean Status Value</returns>
public bool IsLoggedIn()
{
return LoginState == LoginState.LoggedIn;
}

/// <summary>
/// Triggers login using Auth API, if the user is not already logged in.
/// </summary>
/// <returns>True, if login was successfull, else False</returns>
public bool Login()
{
OnLoginStateChanged(LoginState.LoggingIn);
var result = IDSDK_Login();
OnLoginStateChanged(result ? LoginState.LoggedIn : LoginState.LoggedOut);
return result;
}

/// <summary>
/// Logs out the user from the current session.
/// </summary>
public void Logout()
{
var res = IDSDK_Logout();
OnLoginStateChanged(LoginState);
}

/// <summary>
/// Gets the username of the logged in user.
/// </summary>
public string Username
{
get
{
var result = IDSDK_GetUserInfo();
return result != null ? result.UserName : String.Empty;
}
}

/// <summary>
/// Used by the auth provider to sign request with the authorized token.
/// </summary>
public void SignRequest(ref RestRequest m, RestClient client)
{
if (LoginState == LoginState.LoggedOut && !Login())
{
throw new Exception("You must be logged in, to use the Package Manager.");
}
m.AddHeader("Authorization", $"Bearer {IDSDK_GetToken()}");
}

private void OnLoginStateChanged(LoginState state)
{
if (LoginStateChanged != null)
{
LoginStateChanged(state);
}
}

#region IDSDK methods
private class IDSDK_User_Info {
internal string UserId { get; set; }
internal string UserFirstName { get; set; }
internal string UserLastName { get; set; }
internal string UserEmail { get; set; }
internal string AnalyticsId { get; set; }
internal string UserName { get; set; }
internal IDSDK_User_Info(string userId, string userFirstName, string userLastName, string userEmail, string analyticsId, string userName) {
UserId = userId;
UserFirstName = userFirstName;
UserLastName = userLastName;
UserEmail = userEmail;
AnalyticsId = analyticsId;
UserName = userName;
}
}
private bool IDSDK_Login()
{
if (IDSDK_IsLoggedIn())
{
return true;
}
else
{
if (Initialize())
{
//idsdk_status_code statusCode = Client.Login();
//if (Client.IsSuccess(statusCode))
//{
// return true;
//}
}
return false;
}
}
private bool IDSDK_IsLoggedIn()
{
if (Initialize())
{
//bool ret = Client.IsLoggedIn();
//return ret;
}
return false;
}
private bool IDSDK_Logout()
{
if (IDSDK_IsLoggedIn())
{
//idsdk_status_code statusCode = Client.Logout(idsdk_logout_flags.IDSDK_LOGOUT_MODE_SILENT);
//if (Client.IsSuccess(statusCode))
//{
// Deinitialize();
// return true;
//}
}
return false;
}
private IDSDK_User_Info IDSDK_GetUserInfo()
{
//if (Client.IsInitialized() && Client.IsLoggedIn())
//{
// idsdk_status_code statusCode = Client.GetUserInfo(out bool loginState, out string userId, out string userFirstName, out string userLastName,
// out string userEmail, out string analyticsId, out string userName, out uint loginExpireDay);

// IDSDK_User_Info strUserInfo = null;
// if (Client.IsSuccess(statusCode))
// {
// strUserInfo= new IDSDK_User_Info(userId, userFirstName, userLastName, userEmail, analyticsId, userName);
// }
// return strUserInfo;
//}
return null;
}
#endregion

#region IDSDK Utilities
//private bool SetProductConfigs(string productLineCode, idsdk_server server, string oauthKey)
//{
// idsdk_status_code bRet = Client.SetProductConfig(oauthKey, "", productLineCode, DateTime.Now.Year.ToString(), "1.2.3.4", server);
// return Client.IsSuccess(bRet);
//}
/// <summary>
/// Returns the OAuth2 token for the current session, or an empty string if token is not available.
/// </summary>
private string IDSDK_GetToken()
{
//idsdk_status_code ret = Client.GetToken(out string strToken);
//if (Client.IsSuccess(ret))
//{
// return strToken;
//}
return String.Empty;
}
private bool Initialize()
{
//idsdk_status_code bRet = Client.Init();

//if (Client.IsSuccess(bRet))
//{
// if (Client.IsInitialized())
// {
// bool ret = GetClientIDAndServer(out idsdk_server server, out string client_id);
// if (ret)
// {
// ret = SetProductConfigs("Dynamo", server, client_id);
// return ret;
// }
// }
//}
return false;
}
private bool Deinitialize()
{
//idsdk_status_code bRet = Client.DeInit();

//if (Client.IsSuccess(bRet))
//{
// return true;
//}
return false;
}
//private bool GetClientIDAndServer(out idsdk_server server, out string client_id)
//{
// server = idsdk_server.IDSDK_PRODUCTION_SERVER;

// client_id = DynamoUtilities.PathHelper.getServiceConfigValues(this, "IDSDK_CLIENT_ID");

// string env = DynamoUtilities.PathHelper.getServiceConfigValues(this, "IDSDK_ENVIRONMENT");
// if (!string.IsNullOrEmpty(env))
// {
// if (env.Trim().ToLower() == "stg")
// {
// server = idsdk_server.IDSDK_STAGING_SERVER;
// }
// else if (env.Trim().ToLower() == "dev")
// {
// server = idsdk_server.IDSDK_DEVELOPMENT_SERVER;
// }
// }
// return !string.IsNullOrEmpty(client_id);
//}
#endregion
}
}
Loading