Skip to content

Commit

Permalink
Fixed Connect-SFApp command to log in users with LOGIN_NAME different…
Browse files Browse the repository at this point in the history
… from USERNAME property
  • Loading branch information
sfc-gh-dodievich committed Oct 7, 2021
1 parent 0eb635c commit aad07da
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
42 changes: 39 additions & 3 deletions Commands/Authentication/ConnectAppCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected override void ProcessRecord()
throw new ArgumentException(String.Format("Unknown parameter set {0}", this.ParameterSetName));
}
appUserContext.UserName = this.UserName;
logger.Info("Username={0}", appUserContext.UserName);
logger.Info("LoginName={0}", appUserContext.UserName);

#endregion

Expand Down Expand Up @@ -529,14 +529,50 @@ protected override void ProcessRecord()
loggerConsole.Info("Converting redirect token to authentication token for user {0} in account {1}", appUserContext.UserName, appUserContext.AccountName);

// Authenticate Step 3, Converting OAuth redirect into authentication cookie
appUserContext.AuthTokenSnowsight = SnowflakeDriver.GetAuthenticationTokenFromOAuthRedirectToken(appUserContext.AppServerUrl, appUserContext.AccountUrl, oAuthRedirectCode);
if (appUserContext.AuthTokenSnowsight.Length == 0)
string authenticationTokenWebPageResult = SnowflakeDriver.GetAuthenticationTokenFromOAuthRedirectToken(appUserContext.AppServerUrl, appUserContext.AccountUrl, oAuthRedirectCode);
if (authenticationTokenWebPageResult.Length == 0)
{
throw new InvalidCredentialException(String.Format("Invalid response from completing redirect OAuth Token for user {0}@{1}", appUserContext.UserName, appUserContext.AccountName));
}

JObject authenticationTokenWebPageObject = JObject.Parse(authenticationTokenWebPageResult);
if (authenticationTokenWebPageObject == null)
{
throw new InvalidCredentialException(String.Format("Invalid result from completing redirect OAuth Token for user {0}@{1}", appUserContext.UserName, appUserContext.AccountName));
}
appUserContext.AuthTokenSnowsight = JSONHelper.getStringValueFromJToken(authenticationTokenWebPageObject, "authenticationCookie");

logger.Info("SnowsightAuthToken={0}", appUserContext.AuthTokenSnowsight);

// For users where their USERNAME is not the same as LOGIN_NAME, the future authentication needs USERNAME property. It is specified in var params = {...} block in HTML returned from the call
// <html>
// <head>
// <script>
// var params = {"account":"sfpscogs_dodievich_sso","appServerUrl":"https://apps-api.c1.westus2.azure.app.snowflake.com",.... "username":"DODIEVICH_ALT"}}
// if (window.opener && params.isPopupAuth) {
string paramsCarryingPageResult = HttpUtility.HtmlDecode(JSONHelper.getStringValueFromJToken(authenticationTokenWebPageObject, "resultPage"));
Regex regexParameters = new Regex(@"(?i)var params = ({.*})", RegexOptions.IgnoreCase);
match = regexParameters.Match(paramsCarryingPageResult);
if (match != null)
{
if (match.Groups.Count > 1)
{
string userAccountParams = match.Groups[1].Value;

JObject userAccountParamsObject = JObject.Parse(userAccountParams);
if (userAccountParamsObject != null)
{
string userName = JSONHelper.getStringValueFromJToken(userAccountParamsObject["user"], "username");
logger.Info("UserName={0}", userName);
// Only set the Username if it is indeed different from LoginName
if (String.Compare(userName, appUserContext.UserName, true) != 0)
{
appUserContext.UserName = userName;
}
}
}
}

#endregion

#region Organization and User ID context
Expand Down
22 changes: 11 additions & 11 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ The output should look like this:
```
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Connect-SFApp 2021.10.6.0 SnowflakePS
Cmdlet Get-SFDashboards 2021.10.6.0 SnowflakePS
Cmdlet Get-SFFolders 2021.10.6.0 SnowflakePS
Cmdlet Get-SFQueryProfile 2021.10.6.0 SnowflakePS
Cmdlet Get-SFWorksheets 2021.10.6.0 SnowflakePS
Cmdlet Invoke-SFDashboard 2021.10.6.0 SnowflakePS
Cmdlet Invoke-SFWorksheet 2021.10.6.0 SnowflakePS
Cmdlet New-SFDashboard 2021.10.6.0 SnowflakePS
Cmdlet New-SFWorksheet 2021.10.6.0 SnowflakePS
Cmdlet Remove-SFDashboard 2021.10.6.0 SnowflakePS
Cmdlet Remove-SFWorksheet 2021.10.6.0 SnowflakePS
Cmdlet Connect-SFApp 2021.10.7.0 SnowflakePS
Cmdlet Get-SFDashboards 2021.10.7.0 SnowflakePS
Cmdlet Get-SFFolders 2021.10.7.0 SnowflakePS
Cmdlet Get-SFQueryProfile 2021.10.7.0 SnowflakePS
Cmdlet Get-SFWorksheets 2021.10.7.0 SnowflakePS
Cmdlet Invoke-SFDashboard 2021.10.7.0 SnowflakePS
Cmdlet Invoke-SFWorksheet 2021.10.7.0 SnowflakePS
Cmdlet New-SFDashboard 2021.10.7.0 SnowflakePS
Cmdlet New-SFWorksheet 2021.10.7.0 SnowflakePS
Cmdlet Remove-SFDashboard 2021.10.7.0 SnowflakePS
Cmdlet Remove-SFWorksheet 2021.10.7.0 SnowflakePS
```

## Connect-SFApp
Expand Down
6 changes: 3 additions & 3 deletions SnowflakePS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<AssemblyName>SnowflakePS</AssemblyName>
<RootNamespace>Snowflake.Powershell</RootNamespace>

<Version>2021.10.6.0</Version>
<FileVersion>2021.10.6.0</FileVersion>
<AssemblyVersion>2021.10.6.0</AssemblyVersion>
<Version>2021.10.7.0</Version>
<FileVersion>2021.10.7.0</FileVersion>
<AssemblyVersion>2021.10.7.0</AssemblyVersion>
<Authors>Daniel Odievich (daniel.odievich@snowflake.com)</Authors>
<Company>Snowflake Computing</Company>
<Product>Snowflake Snowsight Extensions</Product>
Expand Down
2 changes: 1 addition & 1 deletion SnowflakePS.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
RootModule = 'SnowflakePS.dll'

# Version number of this module.
ModuleVersion = '2021.10.6.0'
ModuleVersion = '2021.10.7.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
2 changes: 1 addition & 1 deletion UtilityDrivers/SnowflakeDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ private static string apiGET(string baseUrl, string restAPIUrl, string acceptHea
{
if (cookie.StartsWith("user-") == true)
{
resultString = cookie;
resultString = String.Format("{{\"authenticationCookie\": \"{0}\", \"resultPage\": \"{1}\"}}", cookie, HttpUtility.HtmlEncode(resultString));
}
}
}
Expand Down

0 comments on commit aad07da

Please sign in to comment.