Skip to content

Commit

Permalink
Fix the Cloudian Integration SSO Redirect link (#9656)
Browse files Browse the repository at this point in the history
  • Loading branch information
tpodowd committed Sep 10, 2024
1 parent 6ec3c48 commit 638c152
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,8 @@ public static String generateSSOUrl(final String cmcUrlPath, final String user,
return null;
}

stringBuilder.append("&redirect=");
if (group.equals("0")) {
stringBuilder.append("admin.htm");
} else {
stringBuilder.append("explorer.htm");
}
// Redirects to dashboard for admin users or the bucket browser for regular users
stringBuilder.append("&redirect=/");

return cmcUrlPath + "ssosecurelogin.htm?" + stringBuilder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.cloudstack.cloudian;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.HashMap;
import org.apache.cloudstack.cloudian.client.CloudianUtils;
import org.junit.Assert;
import org.junit.Test;

public class CloudianUtilsTest {

@Test
public void testGenerateSSOUrl() {
final String cmcUrlPath = "https://cmc.cloudian.com:8443/Cloudian/";
final String user = "abc-def-ghi";
final String group = "uvw-xyz";
final String ssoKey = "randomkey";

// test expectations
final String expPath = "/Cloudian/ssosecurelogin.htm";
HashMap<String, String> expected = new HashMap();
expected.put("user", user);
expected.put("group", group);
expected.put("timestamp", null); // null value will not be checked by this test
expected.put("signature", null); // null value will not be checked by this test
expected.put("redirect", "/");

// Generated URL will be something like this
// https://cmc.cloudian.com:8443/Cloudian/ssosecurelogin.htm?user=abc-def-ghi&group=uvw-xyz&timestamp=1725937474949&signature=Wu1hjafeyE82mGwd1MIwrp5hPt4%3D&redirect=/
String output = CloudianUtils.generateSSOUrl(cmcUrlPath, user, group, ssoKey);
Assert.assertNotNull(output);

// Check main parts of the output URL
URL url = null;
try {
url = new URL(output);
} catch (MalformedURLException e) {
Assert.fail("failed to parse URL: " + output);
}
String path = url.getPath();
Assert.assertEquals(expPath, path);

// No easy way to check Query parameters in Java still?
// Just do a rudementary check as we are in charge of the URL
String query = url.getQuery();
String[] nameValues = query.split("&");
int matchedCount = 0;
for(String nameValue : nameValues) {
String[] nameValuePair = nameValue.split("=", 2);
Assert.assertEquals(nameValue, 2, nameValuePair.length);
String name = null;
String value = null;
try {
name = URLDecoder.decode(nameValuePair[0], "UTF-8");
value = URLDecoder.decode(nameValuePair[1], "UTF-8");
} catch (UnsupportedEncodingException e) {
Assert.fail("not expecting UTF-8 to fail");
}
Assert.assertTrue(expected.containsKey(name));
matchedCount++;
String expValue = expected.get(name);
if (expValue != null) {
Assert.assertEquals("Parameter " + name, expValue, value);
}
}
Assert.assertEquals("Should be 5 query parameters", 5, matchedCount);
}
}

0 comments on commit 638c152

Please sign in to comment.