-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathExternalLoginBox.js
More file actions
215 lines (192 loc) · 7.74 KB
/
ExternalLoginBox.js
File metadata and controls
215 lines (192 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { FormattedMessage, injectIntl } from 'react-intl';
import Button from '../../components/widgets/TheButton';
import Box from '../../components/widgets/Box';
import Callout from '../../components/widgets/Callout';
import Explanation from '../../components/widgets/Explanation';
import { LinkIcon, LoadingIcon, SuccessIcon } from '../../components/icons';
import NiceCheckbox from '../../components/forms/NiceCheckbox';
import { externalLogin, externalLoginFailed, statusTypes } from '../../redux/modules/auth.js';
import { statusSelector, loginErrorSelector } from '../../redux/selectors/auth.js';
import { hasErrorMessage, getErrorMessage } from '../../locales/apiErrorMessages.js';
export const openPopupWindow = url =>
typeof window !== 'undefined'
? window.open(url, 'ExternalLogin', 'modal=true,width=1024,height=850,centerscreen=yes')
: null;
class ExternalLoginBox extends Component {
state = { pending: false, lastError: null, short: false };
constructor(props) {
super(props);
this.popupWindow = null;
this.pollPopupClosed = null;
}
setShort = () => {
this.setState({ short: !this.state.short });
};
// Handle the messages from our popup window...
messageHandler = e => {
const { shortSessionConfig, login } = this.props;
const token = e.data; // the message should be the external JWT token
if (token !== null && e.source === this.popupWindow && this.popupWindow !== null) {
// cancel the window and the interval
this.popupWindow.postMessage('received', e.origin);
login(token, this.state.short && shortSessionConfig ? shortSessionConfig * 60 : null, this.popupWindow, error => {
if (hasErrorMessage(error)) {
this.setState({ lastError: error });
}
});
this.dispose(); // delayed window close (1s)
}
};
onClick = () => {
if (this.popupWindow === null || this.popupWindow.closed) {
const { url, fail } = this.props;
this.popupWindow = openPopupWindow(url);
if (!this.popupWindow) {
fail(); // not in browser or for some reason the window could not have been opened
} else {
// the window is open, now periodically check if the user has already logged in
window.addEventListener('message', this.messageHandler);
this.pollPopupClosed = window.setInterval(this.pollPopupClosedHandler, 100);
}
} else {
this.popupWindow.focus(); // no need to create the window again
}
this.setState({ pending: true, lastError: null });
};
pollPopupClosedHandler = () => {
// Check, whether the popup has been closed ...
if (!this.popupWindow || this.popupWindow.closed === true) {
this.dispose();
}
};
/**
* Clean up all the mess (the window, the interval)
*/
dispose = () => {
window.removeEventListener('message', this.messageHandler);
if (this.pollPopupClosed) {
clearInterval(this.pollPopupClosed);
this.pollPopupClosed = null;
}
if (this.popupWindow) {
this.popupWindow = null;
}
this.setState({ pending: false });
};
/**
* Avoid memory leaks if the user leaves the page before the popup window is closed.
*/
componentWillUnmount = this.dispose;
render() {
const {
name,
helpUrl,
shortSessionConfig = null,
loginStatus,
loginError,
intl: { formatMessage },
} = this.props;
const pending = this.state.pending || loginStatus === statusTypes.LOGGING_IN;
const loggedIn = loginStatus === statusTypes.LOGGED_IN;
return (
<Box
title={<FormattedMessage id="app.externalLogin.title" defaultMessage="Sign-in by External Authenticator" />}
footer={
<div className="text-center">
<Button variant="success" onClick={this.onClick} disabled={pending || loggedIn}>
{pending ? (
<LoadingIcon gapRight={2} />
) : loggedIn ? (
<SuccessIcon gapRight={2} />
) : (
<LinkIcon gapRight={2} />
)}
{pending ? (
<FormattedMessage id="app.externalLogin.button.authenticating" defaultMessage="Authenticating..." />
) : loggedIn ? (
<FormattedMessage id="app.externalLogin.button.authenticated" defaultMessage="Authenticated" />
) : (
<FormattedMessage id="app.externalLogin.button.authenticate" defaultMessage="Authenticate" />
)}
</Button>
</div>
}>
<>
<p>
<FormattedMessage
id="app.externalLogin.description"
defaultMessage='Sign-in into ReCodEx using external authentication service "{name}". If you do not have an account in ReCodEx, it will attempt to create one. If you do have a local account, it will be associated with your external identity if both have the same e-mail address.'
values={{ name }}
/>
</p>
{helpUrl && (
<p>
<FormattedMessage
id="app.externalLogin.help"
defaultMessage="In case of any problems, <a>contact the support</a>."
values={{
a: contents => (
<a href={helpUrl}>
{Array.isArray(contents)
? contents.map((c, i) => <React.Fragment key={i}>{c}</React.Fragment>)
: contents}
</a>
),
}}
/>
</p>
)}
{shortSessionConfig && shortSessionConfig > 0 && (
<NiceCheckbox name="external.short" checked={this.state.short} onChange={this.setShort}>
<FormattedMessage id="app.loginForm.short" defaultMessage="Short session" /> ({shortSessionConfig} min)
<Explanation id="loginForm.shortSession">
<FormattedMessage
id="app.loginForm.short.explanation"
defaultMessage="Use short session on public computers to reduce the risk of unauthorized access to your account (if you forget to log out)."
/>
</Explanation>
</NiceCheckbox>
)}
{!pending && (loginStatus === statusTypes.LOGIN_FAILED || this.state.lastError) && (
<Callout variant="danger" className="mt-3">
{loginError || this.state.lastError ? (
getErrorMessage(formatMessage)(loginError || this.state.lastError)
) : (
<FormattedMessage id="app.externalLogin.failed" defaultMessage="External authentication failed." />
)}
</Callout>
)}
</>
</Box>
);
}
}
ExternalLoginBox.propTypes = {
name: PropTypes.string.isRequired,
url: PropTypes.string.isRequired,
service: PropTypes.string.isRequired,
helpUrl: PropTypes.string,
shortSessionConfig: PropTypes.number,
loginStatus: PropTypes.string,
loginError: PropTypes.object,
login: PropTypes.func.isRequired,
fail: PropTypes.func.isRequired,
afterLogin: PropTypes.func.isRequired,
intl: PropTypes.object,
};
export default connect(
(state, { service }) => ({
loginStatus: statusSelector(service)(state),
loginError: loginErrorSelector(state, service),
}),
(dispatch, { service, afterLogin = null }) => ({
login: (token, expiration, popupWindow, errorHandler = null) => {
const promise = dispatch(externalLogin(service, token, expiration, popupWindow));
return (afterLogin ? promise.then(afterLogin) : promise).catch(e => errorHandler && errorHandler(e));
},
fail: () => dispatch(externalLoginFailed(service)),
})
)(injectIntl(ExternalLoginBox));