Skip to content

Commit 008911d

Browse files
committed
CLOUDSTACK-8195: Don't break IdP, return metadata XML
Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com> (cherry picked from commit 1172867) Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent 552f2ae commit 008911d

5 files changed

Lines changed: 35 additions & 7 deletions

File tree

plugins/user-authenticators/saml2/src/org/apache/cloudstack/api/command/GetServiceProviderMetaDataCmd.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.cloud.api.response.ApiResponseSerializer;
2121
import com.cloud.user.Account;
22+
import com.cloud.utils.HttpUtils;
2223
import org.apache.cloudstack.api.APICommand;
2324
import org.apache.cloudstack.api.ApiErrorCode;
2425
import org.apache.cloudstack.api.ApiServerService;
@@ -171,6 +172,7 @@ public String authenticate(String command, Map<String, Object[]> params, HttpSes
171172
spSSODescriptor.addSupportedProtocol(SAMLConstants.SAML20P_NS);
172173
spEntityDescriptor.getRoleDescriptors().add(spSSODescriptor);
173174

175+
StringWriter stringWriter = new StringWriter();
174176
try {
175177
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
176178
DocumentBuilder builder = factory.newDocumentBuilder();
@@ -179,17 +181,24 @@ public String authenticate(String command, Map<String, Object[]> params, HttpSes
179181
out.marshall(spEntityDescriptor, document);
180182

181183
Transformer transformer = TransformerFactory.newInstance().newTransformer();
182-
StringWriter stringWriter = new StringWriter();
183184
StreamResult streamResult = new StreamResult(stringWriter);
184185
DOMSource source = new DOMSource(document);
185186
transformer.transform(source, streamResult);
186187
stringWriter.close();
187188
response.setMetadata(stringWriter.toString());
188189
} catch (ParserConfigurationException | IOException | MarshallingException | TransformerException e) {
189-
response.setMetadata("Error creating Service Provider MetaData XML: " + e.getMessage());
190+
if (responseType.equals(HttpUtils.JSON_CONTENT_TYPE)) {
191+
response.setMetadata("Error creating Service Provider MetaData XML: " + e.getMessage());
192+
} else {
193+
return "Error creating Service Provider MetaData XML: " + e.getMessage();
194+
}
190195
}
191-
192-
return ApiResponseSerializer.toSerializedString(response, responseType);
196+
// For JSON type return serialized response object
197+
if (responseType.equals(HttpUtils.RESPONSE_TYPE_JSON)) {
198+
return ApiResponseSerializer.toSerializedString(response, responseType);
199+
}
200+
// For other response types return XML
201+
return stringWriter.toString();
193202
}
194203

195204
@Override

plugins/user-authenticators/saml2/src/org/apache/cloudstack/api/command/SAML2LoginAPIAuthenticatorCmd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public String authenticate(final String command, final Map<String, Object[]> par
273273
UserAccount userAccount = _userAccountDao.getUserAccount(username, domainId);
274274
if (userAccount == null && uniqueUserId != null && username != null) {
275275
CallContext.current().setEventDetails("SAML Account/User with UserName: " + username + ", FirstName :" + password + ", LastName: " + lastName);
276-
_accountService.createUserAccount(username, password, firstName, lastName, email, timeZone,
276+
userAccount = _accountService.createUserAccount(username, password, firstName, lastName, email, timeZone,
277277
username, (short) accountType, domainId, null, null, UUID.randomUUID().toString(), uniqueUserId);
278278
}
279279

ui/index.jsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
</div>
6868
<!-- Submit (login) -->
6969
<input type="submit" value="<fmt:message key="label.login"/>" />
70-
<input type="samlsubmit" value="<fmt:message key="label.saml.login"/>" />
70+
<div id="saml-login"><input type="samlsubmit" value="<fmt:message key="label.saml.login"/>"/></div>
7171
<!-- Select language -->
7272
<div class="select-language">
7373
<select name="language">

ui/scripts/ui-custom/login.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,25 @@
126126
});
127127
});
128128

129+
// Show SAML button if only SP is configured
130+
$login.find("#saml-login").hide();
131+
$.ajax({
132+
type: "GET",
133+
url: createURL("getSPMetadata"),
134+
dataType: "json",
135+
async: false,
136+
success: function(data, textStatus, xhr) {
137+
if (xhr.status === 200) {
138+
$login.find('#saml-login').show();
139+
} else {
140+
$login.find('#saml-login').hide();
141+
}
142+
},
143+
error: function(xhr) {
144+
$login.find('#saml-login').hide();
145+
},
146+
});
147+
129148
// Select language
130149
var $languageSelect = $login.find('select[name=language]');
131150
$languageSelect.change(function() {

utils/src/org/apache/cloudstack/utils/auth/SAMLUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public static PrivateKey loadPrivateKey(String privateKey) {
309309
public static KeyPair generateRandomKeyPair() throws NoSuchProviderException, NoSuchAlgorithmException {
310310
Security.addProvider(new BouncyCastleProvider());
311311
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
312-
keyPairGenerator.initialize(2048, new SecureRandom());
312+
keyPairGenerator.initialize(4096, new SecureRandom());
313313
return keyPairGenerator.generateKeyPair();
314314
}
315315

0 commit comments

Comments
 (0)