Skip to content

Commit 1172867

Browse files
committed
CLOUDSTACK-8195: Don't break IdP, return metadata XML
Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent 5159cbe commit 1172867

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;
@@ -170,6 +171,7 @@ public String authenticate(String command, Map<String, Object[]> params, HttpSes
170171
spSSODescriptor.addSupportedProtocol(SAMLConstants.SAML20P_NS);
171172
spEntityDescriptor.getRoleDescriptors().add(spSSODescriptor);
172173

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

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

194203
@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
@@ -272,7 +272,7 @@ public String authenticate(final String command, final Map<String, Object[]> par
272272
UserAccount userAccount = _userAccountDao.getUserAccount(username, domainId);
273273
if (userAccount == null && uniqueUserId != null && username != null) {
274274
CallContext.current().setEventDetails("SAML Account/User with UserName: " + username + ", FirstName :" + password + ", LastName: " + lastName);
275-
_accountService.createUserAccount(username, password, firstName, lastName, email, timeZone,
275+
userAccount = _accountService.createUserAccount(username, password, firstName, lastName, email, timeZone,
276276
username, (short) accountType, domainId, null, null, UUID.randomUUID().toString(), uniqueUserId);
277277
}
278278

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)