| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1a8fe82 commit 0b94f25
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,7 +48,7 @@ public Pair<Boolean, ActionOnFailedAuthentication> authenticate(String username, | |||
| 48 | 48 | return new Pair<Boolean, ActionOnFailedAuthentication>(false, null); | |
| 49 | 49 | } else { | |
| 50 | 50 | User user = _userDao.getUser(userAccount.getId()); | |
| 51 | - if (user != null && SAMLUtils.checkSAMLUserId(user.getUuid()) && | ||
| 51 | + if (user != null && SAMLUtils.checkSAMLUser(user.getUuid(), username) && | ||
| 52 | 52 | requestParameters != null && requestParameters.containsKey(SAMLUtils.SAML_RESPONSE)) { | |
| 53 | 53 | return new Pair<Boolean, ActionOnFailedAuthentication>(true, null); | |
| 54 | 54 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -73,14 +73,28 @@ public void authenticate() throws NoSuchFieldException, SecurityException, Illeg | |||
| 73 | 73 | Mockito.when(userAccountDao.getUserAccount(Mockito.anyString(), Mockito.anyLong())).thenReturn(account); | |
| 74 | 74 | Mockito.when(userDao.getUser(Mockito.anyLong())).thenReturn(user); | |
| 75 | 75 | ||
| 76 | + Pair<Boolean, ActionOnFailedAuthentication> pair; | ||
| 77 | + Map<String, Object[]> params = new HashMap<String, Object[]>(); | ||
| 78 | + | ||
| 76 | 79 | // When there is no SAMLRequest in params | |
| 77 | - Pair<Boolean, ActionOnFailedAuthentication> pair1 = authenticator.authenticate(SAMLUtils.createSAMLId("user1234"), "random", 1l, null); | ||
| 78 | - Assert.assertFalse(pair1.first()); | ||
| 80 | + pair = authenticator.authenticate("someUID", "random", 1l, params); | ||
| 81 | + Assert.assertFalse(pair.first()); | ||
| 79 | 82 | ||
| 80 | - // When there is SAMLRequest in params | ||
| 81 | - Map<String, Object[]> params = new HashMap<String, Object[]>(); | ||
| 83 | + // When there is SAMLRequest in params and user is same as the mocked one | ||
| 82 | 84 | params.put(SAMLUtils.SAML_RESPONSE, new Object[]{}); | |
| 83 | - Pair<Boolean, ActionOnFailedAuthentication> pair2 = authenticator.authenticate(SAMLUtils.createSAMLId("user1234"), "random", 1l, params); | ||
| 84 | - Assert.assertTrue(pair2.first()); | ||
| 85 | + pair = authenticator.authenticate("someUID", "random", 1l, params); | ||
| 86 | + Assert.assertTrue(pair.first()); | ||
| 87 | + | ||
| 88 | + // When there is SAMLRequest in params but username is null | ||
| 89 | + pair = authenticator.authenticate(null, "random", 1l, params); | ||
| 90 | + Assert.assertFalse(pair.first()); | ||
| 91 | + | ||
| 92 | + // When there is SAMLRequest in params but username is empty | ||
| 93 | + pair = authenticator.authenticate("", "random", 1l, params); | ||
| 94 | + Assert.assertFalse(pair.first()); | ||
| 95 | + | ||
| 96 | + // When there is SAMLRequest in params but username is not valid | ||
| 97 | + pair = authenticator.authenticate("someOtherUID", "random", 1l, params); | ||
| 98 | + Assert.assertFalse(pair.first()); | ||
| 85 | 99 | } | |
| 86 | 100 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,6 +20,7 @@ | |||
| 20 | 20 | package org.apache.cloudstack.utils.auth; | |
| 21 | 21 | ||
| 22 | 22 | import com.cloud.utils.HttpUtils; | |
| 23 | + import org.apache.commons.codec.digest.DigestUtils; | ||
| 23 | 24 | import org.apache.log4j.Logger; | |
| 24 | 25 | import org.bouncycastle.jce.provider.BouncyCastleProvider; | |
| 25 | 26 | import org.bouncycastle.x509.X509V1CertificateGenerator; | |
@@ -96,18 +97,25 @@ public class SAMLUtils { | |||
| 96 | 97 | public static final Logger s_logger = Logger.getLogger(SAMLUtils.class); | |
| 97 | 98 | ||
| 98 | 99 | public static final String SAML_RESPONSE = "SAMLResponse"; | |
| 99 | - public static final String SAML_NS = "saml://"; | ||
| 100 | + public static final String SAML_NS = "SAML-"; | ||
| 100 | 101 | public static final String SAML_NAMEID = "SAML_NAMEID"; | |
| 101 | 102 | public static final String SAML_SESSION = "SAML_SESSION"; | |
| 102 | 103 | public static final String CERTIFICATE_NAME = "SAMLSP_CERTIFICATE"; | |
| 103 | 104 | ||
| 104 | 105 | public static String createSAMLId(String uid) { | |
| 105 | - String samlUuid = SAML_NS + uid; | ||
| 106 | - return samlUuid.length() > 40 ? samlUuid.substring(0, 40) : samlUuid; | ||
| 106 | + if (uid == null) { | ||
| 107 | + return null; | ||
| 108 | + } | ||
| 109 | + String hash = DigestUtils.sha256Hex(uid); | ||
| 110 | + String samlUuid = SAML_NS + hash; | ||
| 111 | + return samlUuid.substring(0, 40); | ||
| 107 | 112 | } | |
| 108 | 113 | ||
| 109 | - public static Boolean checkSAMLUserId(String uuid) { | ||
| 110 | - return uuid.startsWith(SAML_NS); | ||
| 114 | + public static boolean checkSAMLUser(String uuid, String username) { | ||
| 115 | + if (uuid == null || uuid.isEmpty() || username == null || username.isEmpty()) { | ||
| 116 | + return false; | ||
| 117 | + } | ||
| 118 | + return uuid.startsWith(SAML_NS) && createSAMLId(username).equals(uuid); | ||
| 111 | 119 | } | |
| 112 | 120 | ||
| 113 | 121 | public static String generateSecureRandomId() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -34,8 +34,14 @@ public class SAMLUtilsTest extends TestCase { | |||
| 34 | 34 | ||
| 35 | 35 | @Test | |
| 36 | 36 | public void testSAMLId() throws Exception { | |
| 37 | - assertTrue(SAMLUtils.checkSAMLUserId(SAMLUtils.createSAMLId("someUID"))); | ||
| 38 | - assertFalse(SAMLUtils.checkSAMLUserId("randomUID")); | ||
| 37 | + assertEquals(SAMLUtils.createSAMLId(null), null); | ||
| 38 | + assertEquals(SAMLUtils.createSAMLId("someUserName"), "SAML-305e19dd2581f33fd90b3949298ec8b17de"); | ||
| 39 | + | ||
| 40 | + assertTrue(SAMLUtils.checkSAMLUser(SAMLUtils.createSAMLId("someUserName"), "someUserName")); | ||
| 41 | + assertFalse(SAMLUtils.checkSAMLUser(SAMLUtils.createSAMLId("someUserName"), "someOtherUserName")); | ||
| 42 | + assertFalse(SAMLUtils.checkSAMLUser(SAMLUtils.createSAMLId(null), "someOtherUserName")); | ||
| 43 | + assertFalse(SAMLUtils.checkSAMLUser("randomUID", "randomUID")); | ||
| 44 | + assertFalse(SAMLUtils.checkSAMLUser(null, null)); | ||
| 39 | 45 | } | |
| 40 | 46 | ||
| 41 | 47 | @Test | |
| Back | FazBrowse Home | New Git URL |
0 commit comments