Stub out GuestAccountsController

pull/1240/head
Rick Carlino 2019-06-16 13:10:25 -05:00
parent da973c23f4
commit 05f122cec8
4 changed files with 75 additions and 38 deletions

View File

@ -0,0 +1,9 @@
module Api
class GuestAccountsController < Api::AbstractController
skip_before_action :authenticate_user!, only: :create
def create
raise "NOT IMPLEMENTED"
end
end
end

View File

@ -139,8 +139,6 @@ module Api
end
def deny
puts "==== #{action_name} ===="
pp params
render json: "deny", status: 403
end
@ -180,16 +178,21 @@ module Api
if farmbot_guest?
a, b, c = routing_key_param.split(".")
if a == GUEST_REGISTRY_ROOT
puts "=========="
if !b.include?("*")
if !b.include?("#")
if c == nil
yield
end
end
end
end
# First check- is it the correct root level?
return if a != GUEST_REGISTRY_ROOT
# Second check- is the user maliciously
# trying to subscribe to
# wildcard topics?
return if b.include?("*")
return if b.include?("#")
# Third check- Ensure subscription is only
# 2 levels deep.
return if c.present?
yield
return
end

View File

@ -27,6 +27,7 @@ FarmBot::Application.routes.draw do
# Singular API Resources:
{
guest_account: [:create],
device_cert: [:create],
device: [:create, :destroy, :show, :update],
fbos_config: [:destroy, :show, :update],

View File

@ -5,55 +5,79 @@ import { stopIE } from "../util/stop_ie";
import I from "i18next";
import React from "react";
import { uuid } from "farmbot";
import axios from "axios";
// TYPES AND INTERFACES ==========================
interface State {
client?: MqttClient;
connected: boolean;
secret: string;
error: Error | undefined;
}
// CONSTANTS =====================================
const WS_CONFIG = {
username: "farmbot_guest",
password: "required, but not used.",
// protocolId: "MQIsdp",
// protocolVersion: 3
};
const SECRET = uuid().split("-").join("");
const MQTT_CHAN = "guest_registry/" + SECRET;
const HTTP_URL = "/api/guest_account";
// APPLICATION CODE ==============================
export class DemoLoader extends React.Component<{}, State> {
state: State = {
client: undefined,
connected: false,
secret: uuid()
error: undefined
};
setError =
(error?: Error) => this.setState({ error });
componentWillMount() {
const client = connect(globalConfig.MQTT_WS, WS_CONFIG);
client.on("close", (x: {}) => console.log("close" + JSON.stringify(x)));
client.on("connect", (x: {}) => console.log("connect" + JSON.stringify(x)));
client.on("disconnect", (x: {}) => console.log("disconnect" + JSON.stringify(x)));
client.on("end", (x: {}) => console.log("end" + JSON.stringify(x)));
client.on("error", (x: {}) => console.log("error" + JSON.stringify(x)));
client.on("message", (x: {}) => console.log("message" + JSON.stringify(x)));
client.on("offline", (x: {}) => console.log("offline" + JSON.stringify(x)));
client.on("packetreceive", (x: {}) => console.log("packetreceive" + JSON.stringify(x)));
client.on("packetsend", (x: {}) => console.log("packetsend" + JSON.stringify(x)));
client.on("reconnect", (x: {}) => console.log("reconnect" + JSON.stringify(x)));
const channel = "guest_registry." + this.state.secret;
client.subscribe(channel, (error, ok) => {
if (error) { console.error(error); } else { console.dir(ok); }
});
const client =
connect(globalConfig.MQTT_WS, WS_CONFIG);
this.setState({ client });
client.on("message", this.handleMessage);
client.subscribe(MQTT_CHAN, this.setError);
}
handleMessage =
(chan: string, buffer: Buffer) => {
debugger;
}
requestAccount = () => {
axios
.post<string>(HTTP_URL, { secret: SECRET })
.then(console.dir)
.catch(this.setError);
};
ok = () => <button onClick={this.requestAccount}>
TRY FARMBOT
</button>;
no = () => {
const message =
// tslint:disable-next-line:no-null-keyword
JSON.stringify(this.state.error, null, 2);
return <pre>
{message}
</pre>;
}
render() {
return <div>Hello, world!</div>;
return this.state.error ?
this.no() : this.ok();
}
}
// BOOTSTRAPPING CODE ============================
stopIE();
console.log(shortRevision());