diff --git a/src/CasLogic.js b/src/CasLogic.js
new file mode 100644
index 0000000000000000000000000000000000000000..cd25f8e067b5c43cd0a1246b1bd90a04ea40b12d
--- /dev/null
+++ b/src/CasLogic.js
@@ -0,0 +1,28 @@
+/*
+Copyright 2015 OpenMarket Ltd
+
+Licensed 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.
+*/
+
+'use strict';
+
+var Url = require ('url');
+
+function getServiceUrl() {
+    var parsedUrl = Url.parse(window.location.href);
+    return parsedUrl.protocol + "//" + parsedUrl.host + parsedUrl.pathname;
+}
+
+module.exports = {
+    getServiceUrl: getServiceUrl
+};
diff --git a/src/controllers/organisms/CasLogin.js b/src/controllers/organisms/CasLogin.js
new file mode 100644
index 0000000000000000000000000000000000000000..9c58d32c06fca87894db815b6df852f32414572f
--- /dev/null
+++ b/src/controllers/organisms/CasLogin.js
@@ -0,0 +1,37 @@
+/*
+Copyright 2015 OpenMarket Ltd
+
+Licensed 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.
+*/
+
+'use strict';
+
+var React = require("react");
+var MatrixClientPeg = require("../../MatrixClientPeg");
+var Cas = require("../../CasLogic");
+
+module.exports = {
+
+    onCasClicked: function(ev) {
+        var serviceRedirectUrl = Cas.getServiceUrl() + "#/login/cas";
+        var self = this;
+        MatrixClientPeg.get().getCasServer().done(function(data) {
+            var serverUrl = data.serverUrl + "/login?service=" + encodeURIComponent(serviceRedirectUrl);
+            window.location.href = serverUrl;
+        }, function(error) {
+            self.setStep("stage_m.login.cas");
+            self.setState({errorText: 'Login failed.'});
+        });
+    },
+
+};
diff --git a/src/controllers/pages/MatrixChat.js b/src/controllers/pages/MatrixChat.js
index f92fe51ce2b37c5095d997199304d679ca0de923..2e93ed184f5ee6a8b026eb7a6067fa1d4021e3e8 100644
--- a/src/controllers/pages/MatrixChat.js
+++ b/src/controllers/pages/MatrixChat.js
@@ -23,6 +23,8 @@ var q = require("q");
 var sdk = require('../../index');
 var MatrixTools = require('../../MatrixTools');
 
+var Cas = require("../../CasLogic");
+
 module.exports = {
     PageTypes: {
         RoomView: "room_view",
@@ -132,12 +134,11 @@ module.exports = {
 
                 var self = this;
                 var client = MatrixClientPeg.get();
-                var splitLocation = window.location.href.split('/');
-                var serviceUrl = splitLocation[0] + "//" + splitLocation[2];
+                var serviceUrl = Cas.getServiceUrl();
 
                 client.loginWithCas(payload.params.ticket, serviceUrl).done(function(data) {
                     MatrixClientPeg.replaceUsingAccessToken(
-                        client.getHsUrl(), client.getIsUrl(),
+                        client.getHomeserverUrl(), client.getIdentityServerUrl(),
                         data.user_id, data.access_token
                     );
                     self.setState({