This repository was archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathAuthentication.react.js
253 lines (229 loc) · 7.46 KB
/
Authentication.react.js
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* global window:true, document:true */
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import queryString from 'query-string';
import {login} from './actions/api';
import {readConfig} from './actions/index';
import {contains, isEmpty, merge, type} from 'ramda';
import * as styles from './styles/styles.js';
import {REDIRECT_URI_PATHNAME, STATUS} from './constants/constants';
// http://stackoverflow.com/questions/4068373/center-a-popup-window-on-screen
const popupCenter = (url, title, w, h) => {
// Fixes dual-screen position
const screenLeft = window.screenLeft;
const screenTop = window.screenTop;
const width = window.innerWidth;
const height = window.innerHeight;
const left = width / 2 - w / 2 + screenLeft;
const top = height / 2 - h / 2 + screenTop;
const popupWindow = window.open(
url,
title,
'scrollbars=yes,width=' +
w +
', height=' +
h +
', top=' +
top +
', left=' +
left
);
return popupWindow;
};
/**
* Login displays an interface that guides the user through an oauth flow.
* - Clicking on a login button will launch a new window with the plot.ly
* oauth url
* - plot.ly will redirect that window to defined redirect URL when complete
* - The <OauthRedirect/> component will render the oauth redirect page
* - When the <OauthRedirect/> window is closed, <Login/> will call its
* `onClosed` prop
*/
class UnconnectedLogin extends Component {
constructor(props) {
super(props);
this.buildOauthUrl = this.buildOauthUrl.bind(this);
this.oauthPopUp = this.oauthPopUp.bind(this);
}
buildOauthUrl() {
const {oauth_client_id, plotly_domain} = this.props.config;
return (
`${plotly_domain}/o/authorize/?response_type=token&` +
`client_id=${oauth_client_id}&` +
`redirect_uri=${window.location.origin}${REDIRECT_URI_PATHNAME}`
);
}
oauthPopUp() {
const popupWindow = popupCenter(
this.buildOauthUrl(),
'Authorization',
'500',
'500'
);
if (window.focus) {
popupWindow.focus();
}
window.popupWindow = popupWindow;
const interval = setInterval(() => {
if (popupWindow.closed) {
this.props.onClosed();
clearInterval(interval);
}
}, 100);
}
render() {
const {plotly_domain} = this.props.config;
return (
<div style={merge(styles.base.html, styles.base.container)}>
<div style={styles.base.h2}>Dash</div>
<div style={styles.base.h4}>
{'Log in to Plotly to continue'}
</div>
<button style={styles.base.button} onClick={this.oauthPopUp}>
{'Log in'}
</button>
<div style={styles.base.caption}>
<span>
{`This dash app requires a plotly login to view.
Don't have an account yet?`}
</span>
<a
style={styles.base.a}
href={`${plotly_domain}/accounts/login/?action=signup`}
>
{' Create an account '}
</a>
<span>
{` (it's free)
and then request access from the owner of this app.`}
</span>
</div>
</div>
);
}
}
UnconnectedLogin.propTypes = {
onClosed: PropTypes.func,
config: PropTypes.object,
};
const Login = connect(state => ({config: state.config}))(UnconnectedLogin);
/**
* OAuth redirect component
* - Looks for an oauth token in the URL as provided by the plot.ly redirect
* - Make an API call to dash with that oauth token
* - In response, Dash will set the oauth token as a cookie
* if it is valid
* Parent is component is responsible for rendering
* this component in the appropriate context
* (the URL redirect)
*/
class UnconnectedOauthRedirect extends Component {
constructor(props) {
super(props);
}
componentDidMount() {
const params = queryString.parse(window.location.hash);
const {access_token} = params;
const {dispatch} = this.props;
dispatch(login(access_token));
}
render() {
const {loginRequest} = this.props;
let content;
if (isEmpty(loginRequest) || loginRequest.status === 'loading') {
content = <div className="_dash-loading">Loading...</div>;
} else if (loginRequest.status === STATUS.OK) {
window.close();
} else {
content = (
<div>
<h3>{'Yikes! An error occurred trying to log in.'}</h3>
{loginRequest.content ? (
<pre>{JSON.stringify(loginRequest.content)}</pre>
) : null}
</div>
);
}
return <div>{content}</div>;
}
}
UnconnectedOauthRedirect.propTypes = {
loginRequest: PropTypes.object,
login: PropTypes.func,
dispatch: PropTypes.func,
};
const OauthRedirect = connect(
state => ({loginRequest: state.loginRequest}),
dispatch => ({dispatch})
)(UnconnectedOauthRedirect);
/**
* Authentication component renders the children if the user is
* logged in or doesn't need to login.
* Otherwise, it renders an interface that allows a user to log in.
*
* Log in is checked through the presence of an oauth token as a cookie.
* Log in is only required for apps that have a `fid` in the `config`
* API response
*
* Note that a user that is logged in does not necessarily have have
* view access to the app.
*
* This component also renders the OAuth redirect URL
*/
class Authentication extends Component {
constructor(props) {
super(props);
this.state = {
oauth_flow_counter: 0,
};
}
componentDidMount() {
this.initialization(this.props);
}
componentWillReceiveProps(props) {
this.initialization(props);
}
initialization(props) {
const {config, dispatch} = props;
if (type(config) === 'Null') {
dispatch(readConfig());
}
}
render() {
const {children, config} = this.props;
// OAuth redirect
if (window.location.pathname === REDIRECT_URI_PATHNAME) {
return <OauthRedirect />;
}
if (type(config) === 'Null') {
return <div className="_dash-loading">Loading...</div>;
} else if (config.fid) {
if (contains('plotly_oauth_token=', document.cookie)) {
return children;
}
// Set oauth token cookie through an oauth flow
return (
<Login
onClosed={() =>
this.setState({
oauth_flow_counter:
this.state.oauth_flow_counter + 1,
})
}
/>
);
}
return children;
}
}
Authentication.propTypes = {
children: PropTypes.object,
config: PropTypes.object,
};
export default connect(
state => ({
config: state.config,
}),
dispatch => ({dispatch})
)(Authentication);