Skip to content

Commit 415563d

Browse files
committed
Updated (un)authentication handling
1 parent 2710416 commit 415563d

File tree

9 files changed

+139
-86
lines changed

9 files changed

+139
-86
lines changed

dashboard/assets.go

Lines changed: 46 additions & 46 deletions
Large diffs are not rendered by default.

dashboard/src/App.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import DeploymentOperator from './deployment/DeploymentOperator.js';
44
import StorageOperator from './storage/StorageOperator.js';
55
import NoOperator from './NoOperator.js';
66
import Loading from './util/Loading.js';
7-
import api from './api/api.js';
7+
import api, { IsUnauthorized } from './api/api.js';
88
import { Container, Segment, Message } from 'semantic-ui-react';
9+
import { withAuth } from './auth/Auth.js';
910

1011
const PodInfoView = ({pod, namespace}) => (
1112
<Segment basic>
@@ -46,25 +47,33 @@ class App extends Component {
4647
reloadOperators = async() => {
4748
try {
4849
const operators = await api.get('/api/operators');
49-
this.setState({operators, error: undefined});
50+
this.setState({
51+
operators,
52+
error: undefined
53+
});
5054
} catch (e) {
51-
this.setState({error: e.message});
55+
this.setState({
56+
error: e.message
57+
});
58+
if (IsUnauthorized(e)) {
59+
this.props.doLogout();
60+
}
5261
}
5362
this.props.setTimeout(this.reloadOperators, 10000);
5463
}
5564

5665
render() {
5766
if (this.state.operators) {
58-
return <OperatorsView
67+
return <OperatorsView
5968
error={this.state.error}
60-
deployment={this.state.operators.deployment}
61-
storage={this.state.operators.storage}
62-
pod={this.state.operators.pod}
63-
namespace={this.state.operators.namespace}
64-
/>;
69+
deployment={this.state.operators.deployment}
70+
storage={this.state.operators.storage}
71+
pod={this.state.operators.pod}
72+
namespace={this.state.operators.namespace}
73+
/>;
6574
}
6675
return (<LoadingView/>);
6776
}
6877
}
6978

70-
export default ReactTimeout(App);
79+
export default ReactTimeout(withAuth(App));

dashboard/src/App.test.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

dashboard/src/api/api.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1+
export function IsUnauthorized(e) {
2+
return (e.status === 401);
3+
}
4+
15
export default {
26
token: '',
37

48
async decodeResults(result) {
59
const decoded = await result.json();
6-
if (result.status === 401) {
7-
throw Error(decoded.error || "Unauthorized")
8-
}
910
if (result.status !== 200) {
10-
throw Error(`Unexpected status ${result.status}`);
11+
let message = decoded.error;
12+
if (!message) {
13+
if (result.status === 401) {
14+
message = "Unauthorized";
15+
} else {
16+
message = `Unexpected status ${result.status}`;
17+
}
18+
}
19+
throw Object.assign(new Error(message), { status: result.status });
1120
}
1221
return decoded;
1322
},

dashboard/src/auth/Auth.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ import { getSessionItem, setSessionItem } from "../util/Storage.js";
77

88
const tokenSessionKey = "auth-token";
99

10+
11+
// withAuth adds a doLogout property to the given component.
12+
export function withAuth(WrappedComponent) {
13+
return function AuthAwareComponent(props) {
14+
return (
15+
<LogoutContext.Consumer>
16+
{doLogout => <WrappedComponent {...props} doLogout={doLogout} />}
17+
</LogoutContext.Consumer>
18+
);
19+
}
20+
}
21+
1022
class Auth extends Component {
1123
state = {
1224
authenticated: false,

dashboard/src/deployment/DeploymentDetails.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import ReactTimeout from 'react-timeout';
22
import React, { Component } from 'react';
3-
import api from '../api/api.js';
3+
import api, { IsUnauthorized } from '../api/api.js';
44
import Loading from '../util/Loading.js';
55
import MemberList from './MemberList.js';
66
import styled from 'react-emotion';
77
import { Loader } from 'semantic-ui-react';
8+
import { withAuth } from '../auth/Auth.js';
89

910
const LoaderBox = styled('span')`
1011
float: right;
@@ -39,7 +40,9 @@ class DeploymentDetails extends Component {
3940

4041
reloadDeployment = async() => {
4142
try {
42-
this.setState({loading:true});
43+
this.setState({
44+
loading: true
45+
});
4346
const result = await api.get(`/api/deployment/${this.props.name}`);
4447
this.setState({
4548
deployment: result,
@@ -51,6 +54,10 @@ class DeploymentDetails extends Component {
5154
loading: false,
5255
error: e.message
5356
});
57+
if (IsUnauthorized(e)) {
58+
this.props.doLogout();
59+
return;
60+
}
5461
}
5562
this.props.setTimeout(this.reloadDeployment, 5000);
5663
}
@@ -65,8 +72,8 @@ class DeploymentDetails extends Component {
6572
<LoaderBox><Loader size="mini" active={this.state.loading} inline/></LoaderBox>
6673
<MemberGroupsView memberGroups={d.member_groups} namespace={d.namespace}/>
6774
</div>
68-
);
75+
);
6976
}
7077
}
7178

72-
export default ReactTimeout(DeploymentDetails);
79+
export default ReactTimeout(withAuth(DeploymentDetails));

dashboard/src/deployment/DeploymentList.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Icon, Loader, Popup, Table } from 'semantic-ui-react';
22
import { Link } from "react-router-dom";
3-
import api from '../api/api.js';
3+
import api, { IsUnauthorized } from '../api/api.js';
44
import CommandInstruction from '../util/CommandInstruction.js';
55
import Loading from '../util/Loading.js';
66
import React, { Component } from 'react';
77
import ReactTimeout from 'react-timeout';
88
import styled from 'react-emotion';
9+
import { withAuth } from '../auth/Auth.js';
910

1011
const LoaderBox = styled('span')`
1112
float: right;
@@ -172,6 +173,10 @@ class DeploymentList extends Component {
172173
});
173174
} catch (e) {
174175
this.setState({error: e.message, loading: false});
176+
if (IsUnauthorized(e)) {
177+
this.props.doLogout();
178+
return;
179+
}
175180
}
176181
this.props.setTimeout(this.reloadDeployments, 5000);
177182
}
@@ -188,4 +193,4 @@ class DeploymentList extends Component {
188193
}
189194
}
190195

191-
export default ReactTimeout(DeploymentList);
196+
export default ReactTimeout(withAuth(DeploymentList));

dashboard/src/storage/StorageList.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Accordion, Header, Icon, Loader, Popup, Table } from 'semantic-ui-react';
2-
import api from '../api/api.js';
2+
import api, { IsUnauthorized } from '../api/api.js';
33
import CommandInstruction from '../util/CommandInstruction.js';
44
import VolumeList from './VolumeList.js';
55
import Loading from '../util/Loading.js';
66
import React, { Component } from 'react';
77
import ReactTimeout from 'react-timeout';
88
import styled from 'react-emotion';
9+
import { withAuth } from '../auth/Auth.js';
910

1011
const LoaderBox = styled('span')`
1112
float: right;
@@ -170,29 +171,38 @@ class StorageList extends Component {
170171

171172
reloadStorages = async() => {
172173
try {
173-
this.setState({loading: true});
174+
this.setState({
175+
loading: true
176+
});
174177
const result = await api.get('/api/storage');
175178
this.setState({
176179
items: result.storages,
177180
loading: false,
178181
error: undefined
179182
});
180183
} catch (e) {
181-
this.setState({error: e.message, loading: false});
184+
this.setState({
185+
error: e.message,
186+
loading: false
187+
});
188+
if (IsUnauthorized(e)) {
189+
this.props.doLogout();
190+
return;
191+
}
182192
}
183193
this.props.setTimeout(this.reloadStorages, 5000);
184194
}
185195

186196
render() {
187197
const items = this.state.items;
188198
if (!items) {
189-
return (<Loading/>);
199+
return (<Loading />);
190200
}
191201
if (items.length === 0) {
192-
return (<EmptyView/>);
202+
return (<EmptyView />);
193203
}
194-
return (<ListView items={items} loading={this.state.loading}/>);
204+
return (<ListView items={items} loading={this.state.loading} />);
195205
}
196206
}
197207

198-
export default ReactTimeout(StorageList);
208+
export default ReactTimeout(withAuth(StorageList));

dashboard/src/storage/VolumeList.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Icon, Loader, Popup, Table } from 'semantic-ui-react';
2-
import api from '../api/api.js';
2+
import api, { IsUnauthorized } from '../api/api.js';
33
import CommandInstruction from '../util/CommandInstruction.js';
44
import Loading from '../util/Loading.js';
55
import React, { Component } from 'react';
66
import ReactTimeout from 'react-timeout';
77
import styled from 'react-emotion';
8+
import { withAuth } from '../auth/Auth.js';
89

910
const LoaderBox = styled('span')`
1011
float: right;
@@ -123,15 +124,24 @@ class VolumeList extends Component {
123124

124125
reloadVolumes = async() => {
125126
try {
126-
this.setState({loading: true});
127+
this.setState({
128+
loading: true
129+
});
127130
const result = await api.get(`/api/storage/${this.props.storageName}`);
128131
this.setState({
129132
items: result.volumes,
130133
loading: false,
131134
error: undefined
132135
});
133136
} catch (e) {
134-
this.setState({error: e.message, loading: false});
137+
this.setState({
138+
error: e.message,
139+
loading: false
140+
});
141+
if (IsUnauthorized(e)) {
142+
this.props.doLogout();
143+
return;
144+
}
135145
}
136146
this.props.setTimeout(this.reloadVolumes, 5000);
137147
}
@@ -148,4 +158,4 @@ class VolumeList extends Component {
148158
}
149159
}
150160

151-
export default ReactTimeout(VolumeList);
161+
export default ReactTimeout(withAuth(VolumeList));

0 commit comments

Comments
 (0)