summaryrefslogtreecommitdiff
path: root/frontend/src/auth.js
blob: 2f32c33a50d4afdfb8669dc873e3193a279bbd15 (plain)
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
import React from 'react';
import { Redirect, generatePath } from 'react-router-dom';
import { Spinner } from 'react-bootstrap';
import { isExpired } from 'react-jwt';

const UserContext = React.createContext();

class Login extends React.Component {
	constructor(props) {
		super(props);
		this.state = {};
	}
	async componentDidMount() {
		let url = await this.get_oauth_url();
		this.setState({url: url});
		window.location = url;
	}
	render() {
		if (this.state.url) {
			return <div>Redirecting to <a href={this.state.url}>{this.state.url}</a></div>;
		}
		return <Spinner animation="border">
			<span className="sr-only">Logging in…</span>
		</Spinner>;
	}
	async get_oauth_url() {
		let redirect_uri = window.location.origin + generatePath('/oauth-callback');
		let response = await fetch('/api/auth/request', {
			method: 'POST',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded',
			},
			body: 'redirect_uri=' + encodeURIComponent(redirect_uri),
		});
		let data = await response.json();
		if (response.ok && data.url) {
			return data.url;
		}
	}
}

class Logout extends React.Component {
	static contextType = UserContext;
	constructor(props) {
		super(props);
		this.state = {done: false};
	}
	async componentDidMount() {
		await this.context.logout();
		this.setState({done: true});
	}
	render() {
		if (this.state.done) {
			return <Redirect to='/' />;
		}
		return <Spinner animation="border">
			<span className="sr-only">Logging out…</span>
		</Spinner>;
	}
}

class OauthCallback extends React.Component {
	static contextType = UserContext;
	constructor(props) {
		super(props);
		this.state = {
			error: null,
			done: false,
		};
	}
	async componentDidMount() {
		this.setState({error: null});
		let params = new URLSearchParams(this.props.location.search);
		let error = params.get('error');
		if (error) {
			this.setState({error: params.get('error_description')});
			return;
		}
		let redirect_uri = window.location.origin + generatePath('/oauth-callback');
		let response = await fetch('/api/auth/response', {
			method: 'POST',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded',
			},
			body: 'code=' + encodeURIComponent(params.get('code')) + '&state=' + encodeURIComponent(params.get('state'))
				+ '&redirect_uri=' + encodeURIComponent(redirect_uri),
		});
		let data = await response.json();
		if (!response.ok) {
			this.setState({error: data.message});
			return;
		}
		const user = await this.context.get_user();
		if (user) {
			this.setState({error: null, done: true});
			window.flash({header: 'Logged in', text: `You are now logged in as ${user.username}.`});
		} else {
			window.flash({header: 'Login failed', text: 'Something failed during authentication.'});
		}
	}
	render() {
		if (this.state.error) {
			return this.state.error;
		}
		if (this.state.done) {
			return <Redirect to='/' />;
		}
		return <Spinner animation="border">
			<span className="sr-only">Logging in…</span>
		</Spinner>;
	}
}

class AuthenticationProvider extends React.Component {
	constructor(props) {
		super(props);
		this.state = {
			username: null,
			is_logged_in: false,
			access_token: null,
		};
		this.logout = this.logout.bind(this);
		this.get_user = this.get_user.bind(this);
		this.get_token = this.get_token.bind(this);
	}
	async componentDidMount() {
		await this.get_user();
	}
	render() {
		return <UserContext.Provider value={{
				username: this.state.username,
				is_logged_in: this.state.is_logged_in,
				get_user: this.get_user,
				logout: this.logout,
				get_token: this.get_token}}>
			{this.props.children}
		</UserContext.Provider>;
	}
	async logout() {
		const was_logged_in = this.state.is_logged_in;
		await fetch('/api/auth/logout', {
			method: 'POST',
		});
		this.setState({username: null, is_logged_in: false, access_token: null});
		if (was_logged_in) {
			window.flash({header: 'Logged out', text: 'You are now logged out.'});
		}
	}
	async get_user() {
		let token = await this.get_token();
		if (!token) {
			return;
		}
		let response = await fetch('/api/user',{
			headers: {
				Authorization: `Bearer ${token}`,
			}
		});
		if (response.ok) {
			let data = await response.json();
			this.setState({username: data.username, is_logged_in: true});
			return data;
		} else {
			await this.logout();
		}
	}
	async refresh() {
		let response = await fetch('/api/auth/refresh', {
			method: 'POST',
			credentials: 'include',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded',
			},
			body: '',
		});
		if (response.ok) {
			let data = await response.json();
			return data.access_token;
		}
	}
	async get_token() {
		let access_token = this.state.access_token;
		let is_expired = !access_token || isExpired(access_token);
		if (is_expired) {
			access_token = await this.refresh();
			this.setState({access_token: access_token});
		}
		return access_token;
	}
}

export { UserContext, Login, Logout, OauthCallback, AuthenticationProvider };