Skip to content

Commit 9e596eb

Browse files
committed
Merge pull request element-hq#19 from stevenhammerton/sh-cas-auth
Add support for CAS login
2 parents f7d3d4f + fc33306 commit 9e596eb

3 files changed

Lines changed: 96 additions & 0 deletions

File tree

‎src/CasLogic.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright 2015 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
var url = require ('url');
20+
21+
function getServiceUrl() {
22+
var parsedUrl = url.parse(window.location.href);
23+
return parsedUrl.protocol + "//" + parsedUrl.host + parsedUrl.pathname;
24+
}
25+
26+
module.exports = {
27+
getServiceUrl: getServiceUrl
28+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2015 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
var React = require("react");
20+
var MatrixClientPeg = require("../../MatrixClientPeg");
21+
var Cas = require("../../CasLogic");
22+
23+
module.exports = {
24+
25+
onCasClicked: function(ev) {
26+
var serviceRedirectUrl = Cas.getServiceUrl() + "#/login/cas";
27+
var self = this;
28+
MatrixClientPeg.get().getCasServer().done(function(data) {
29+
var serverUrl = data.serverUrl + "/login?service=" + encodeURIComponent(serviceRedirectUrl);
30+
window.location.href = serverUrl;
31+
}, function(error) {
32+
self.setStep("stage_m.login.cas");
33+
self.setState({errorText: 'Login failed.'});
34+
});
35+
},
36+
37+
};

‎src/controllers/pages/MatrixChat.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ var q = require("q");
2323
var sdk = require('../../index');
2424
var MatrixTools = require('../../MatrixTools');
2525

26+
var Cas = require("../../CasLogic");
27+
2628
module.exports = {
2729
PageTypes: {
2830
RoomView: "room_view",
@@ -128,6 +130,30 @@ module.exports = {
128130
screen: 'login'
129131
});
130132
this.notifyNewScreen('login');
133+
break;
134+
case 'cas_login':
135+
if (this.state.logged_in) return;
136+
137+
var self = this;
138+
var client = MatrixClientPeg.get();
139+
var serviceUrl = Cas.getServiceUrl();
140+
141+
client.loginWithCas(payload.params.ticket, serviceUrl).done(function(data) {
142+
MatrixClientPeg.replaceUsingAccessToken(
143+
client.getHomeserverUrl(), client.getIdentityServerUrl(),
144+
data.user_id, data.access_token
145+
);
146+
self.setState({
147+
screen: undefined,
148+
logged_in: true
149+
});
150+
self.startMatrixClient();
151+
self.notifyNewScreen('');
152+
}, function(error) {
153+
self.notifyNewScreen('login');
154+
self.setState({errorText: 'Login failed.'});
155+
});
156+
131157
break;
132158
case 'view_room':
133159
this.focusComposer = true;
@@ -336,6 +362,11 @@ module.exports = {
336362
action: 'start_login',
337363
params: params
338364
});
365+
} else if (screen == 'cas_login') {
366+
dis.dispatch({
367+
action: 'cas_login',
368+
params: params
369+
});
339370
} else if (screen.indexOf('room/') == 0) {
340371
var roomString = screen.split('/')[1];
341372
if (roomString[0] == '#') {

0 commit comments

Comments
 (0)