mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-13 23:14:53 +09:00
Add authentication plugin that does a simple challenge response protocol to verify that the player trying to log in is who they claim to be. The process goes as follows: 1. A player code is generated for a player that wants to log in and is displayed in-game. 2. The player code is input into the web interface and the master generates a verification code as a challenge back along with a token. 3. The player inputs the verification code in-game and is logged in to the web interface in doing so. To do the actual login the web interface polls a verify endpoint every 2 seconds until the code has either been verified or expired. The challenge comes from being unable to control the verification code and having to input it as a player logging into one of the servers in order to login. Some additional steps are taken in order to increase the difficulty for an attacker to subvert the challenge: - The login process must be started from in-game. - Generated codes are only valid for a limited timespan which is configurable and 2 minutes by default. - The verification code comes with a token that only works with one player code and verification code combo and must be provided to pass validation.
159 lines
4.2 KiB
React
159 lines
4.2 KiB
React
import React, { useEffect, useState } from "react";
|
|
|
|
import libPlugin from "@clusterio/lib/plugin";
|
|
import { notify, notifyErrorHandler } from "@clusterio/web_ui";
|
|
import { Button, Form, Input, Spin, Typography } from "antd";
|
|
|
|
import "./index.css";
|
|
|
|
const { Paragraph, Text } = Typography;
|
|
|
|
|
|
function LoginForm(props) {
|
|
let [servers, setServers] = useState(null);
|
|
let [playerCode, setPlayerCode] = useState(null);
|
|
let [playerCodeError, setPlayerCodeError] = useState(null);
|
|
let [verifyCode, setVerifyCode] = useState(null);
|
|
let [verifyToken, setVerifyToken] = useState(null);
|
|
|
|
useEffect(() => {
|
|
(async () => {
|
|
let response = await fetch(`${webRoot}api/player_auth/servers`);
|
|
if (response.ok) {
|
|
setServers(await response.json());
|
|
} else {
|
|
setServers([]);
|
|
}
|
|
})();
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (verifyToken) {
|
|
let checkLoop = setInterval(() => {
|
|
(async () => {
|
|
let response = await fetch(`${webRoot}api/player_auth/verify`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
verify_token: verifyToken,
|
|
verify_code: verifyCode,
|
|
player_code: playerCode,
|
|
}),
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(`Bad server response: ${response.status}`);
|
|
}
|
|
let json = await response.json();
|
|
if (json.error) {
|
|
// While there are other possibilites the most likely
|
|
// cause of an error response here is that the code expired.
|
|
setPlayerCodeError("Code expired");
|
|
setVerifyToken(null);
|
|
clearInterval(checkLoop);
|
|
return;
|
|
}
|
|
if (json.verified) {
|
|
props.setToken(json.token);
|
|
}
|
|
})().catch(err => {
|
|
clearInterval(checkLoop);
|
|
notifyErrorHandler("Error verifying code")(err);
|
|
});
|
|
}, 2000);
|
|
return () => { clearInterval(checkLoop); };
|
|
}
|
|
|
|
return undefined;
|
|
}, [verifyToken]);
|
|
|
|
if (!servers) {
|
|
return <Spin size="large" />;
|
|
}
|
|
|
|
if (servers.length === 0) {
|
|
return <Paragraph>
|
|
There are no servers in the cluster currently running that can complete
|
|
the login via Factorio. Please try again later.
|
|
</Paragraph>;
|
|
}
|
|
|
|
return <>
|
|
<Paragraph>Login using your Factorio account is a 3 step process:</Paragraph>
|
|
<style>
|
|
</style>
|
|
<ol className="factorio-login-steps">
|
|
<li>
|
|
<Paragraph>
|
|
Start Factorio, join one of the following multiplayer servers
|
|
and type <Text keyboard>/web-login</Text> into the chat:
|
|
</Paragraph>
|
|
<ul>
|
|
{servers.map(text => <li key={text}>{text}</li>)}
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<Paragraph>Enter the code displayed in the in-game dialog here:</Paragraph>
|
|
<Paragraph>
|
|
<Form
|
|
layout="inline"
|
|
onFinish={values => {
|
|
(async () => {
|
|
let response = await fetch(`${webRoot}api/player_auth/player_code`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
player_code: values.player_code,
|
|
}),
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(`Bad server response: ${response.status}`);
|
|
}
|
|
|
|
let json = await response.json();
|
|
if (json.error) {
|
|
setPlayerCodeError("Invalid Code");
|
|
setVerifyToken(null);
|
|
return;
|
|
}
|
|
|
|
setPlayerCodeError(null);
|
|
setPlayerCode(values.player_code);
|
|
setVerifyCode(json.verify_code);
|
|
setVerifyToken(json.verify_token);
|
|
})().catch(notifyErrorHandler("Error verifying code"));
|
|
}}
|
|
>
|
|
<Form.Item
|
|
name="player_code"
|
|
rules={[{ required: true, message: "Field is required" }]}
|
|
help={playerCodeError}
|
|
validateStatus={playerCodeError ? "error" : undefined}
|
|
>
|
|
<Input style={{ width: "10em" }} />
|
|
</Form.Item>
|
|
<Button type="primary" htmlType="submit">Verify</Button>
|
|
</Form>
|
|
</Paragraph>
|
|
</li>
|
|
<li>
|
|
<Paragraph>Enter this code into the in-game dialog:</Paragraph>
|
|
<Paragraph>
|
|
<Input disabled={verifyCode === null} style={{ width: "10em" }} value={verifyCode} />
|
|
</Paragraph>
|
|
</li>
|
|
</ol>
|
|
</>;
|
|
}
|
|
|
|
|
|
export class WebPlugin extends libPlugin.BaseWebPlugin {
|
|
async init() {
|
|
this.logger.info("Player Auth init");
|
|
this.loginForms = [{
|
|
name: "player_auth.factorio",
|
|
title: "Factorio",
|
|
Component: LoginForm,
|
|
}];
|
|
}
|
|
}
|