Skip to content

Commit

Permalink
Merge pull request #41 from X-FRI/dev
Browse files Browse the repository at this point in the history
[stoream]: pin login support, some bug fixes
  • Loading branch information
muqiuhan authored May 31, 2024
2 parents 8353f93 + 0b5b796 commit 41d8d64
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 39 deletions.
1 change: 1 addition & 0 deletions stoream-engine/src/Config/Config.fs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ open FSharp.Data
type Config =
JsonProvider<"""
{
"PIN": "294538",
"Account": {
"Username": "admin",
"Password": "admin"
Expand Down
29 changes: 29 additions & 0 deletions stoream-engine/src/Logger/Logger.fs
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
(* Copyright (c) 2024 The X-Files Research Institute
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Stoream nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*)

module Stoream.Engine.Logger.StoreamLogger

open System
Expand Down
65 changes: 65 additions & 0 deletions stoream-engine/src/PIN/PIN.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
(* Copyright (c) 2024 The X-Files Research Institute
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Stoream nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*)

module Stoream.Engine.PIN

open System
open Suave
open Suave.Filters
open Suave.Successful
open Suave.Operators
open Config
open API.Constraint
open API.Response
open Stoream.Engine.Logger.StoreamLogger

type PIN() =

(* Get the configuration file loaded at startup by the Stoream.Engine.Config module.
* SEE: Stoream.Engine.Config *)
static member inline public CONFIG = CONFIG.Pin

(* Implementing the API interface indicates that this type is an API service *)
interface IGetAPI with
static member public App = PIN.App

static member public App =
path "/pin" >=> GET >=> request PIN.PIN

static member private PIN (request: HttpRequest) =
StoreamLogger.Info $"request {PIN}"

request.queryParamOpt("value").Value
|> snd
|> _.Value
|> fun pin ->
if pin = PIN.CONFIG.ToString() then
Response.OK ()
else
Response.ERROR (ArgumentException $"Invalid PIN {pin}")
4 changes: 3 additions & 1 deletion stoream-engine/src/Server/Server.fs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ open Suave.Filters
open Suave.Operators
open Config
open Account
open PIN
open WebUI
open Stoream.Engine.Storage.Storage

Expand Down Expand Up @@ -68,7 +69,8 @@ type Server () =
(* Please add new services here. *)
WebUI.App
Account.App
Storage.GetApp ])
Storage.GetApp
PIN.App ])
POST
>=> fun context ->
context
Expand Down
1 change: 1 addition & 0 deletions stoream-engine/stoream-engine.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<Compile Include="src/Config/Config.fs" />
<Compile Include="src/API/API.fs" />
<Compile Include="src/API/Response.fs" />
<Compile Include="src/PIN/PIN.fs" />
<Compile Include="src/Account/Account.fs" />
<Compile Include="src/WebUI/WebUI.fs" />
<Compile Include="src/Storage/Secure.fs" />
Expand Down
1 change: 1 addition & 0 deletions stoream-engine/stoream-engine.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"PIN": "294538",
"Account": {
"Username": "admin",
"Password": "21232f297a57a5a743894a0e4a801fc3"
Expand Down
42 changes: 42 additions & 0 deletions stoream-webui/src/components/PreviewFile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/// Copyright (c) 2024 The X-Files Research Institute
///
/// All rights reserved.
///
/// Redistribution and use in source and binary forms, with or without modification,
/// are permitted provided that the following conditions are met:
///
/// * Redistributions of source code must retain the above copyright notice,
/// this list of conditions and the following disclaimer.
/// * Redistributions in binary form must reproduce the above copyright notice,
/// this list of conditions and the following disclaimer in the documentation
/// and/or other materials provided with the distribution.
/// * Neither the name of Stoream nor the names of its contributors
/// may be used to endorse or promote products derived from this software
/// without specific prior written permission.
///
/// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
/// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
/// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
/// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
/// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
/// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
/// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
/// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
/// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
/// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
/// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import React from "react"

interface PreviwFileProps {

}

const PreviewFile: React.FC<PreviwFileProps> = () => {
return (
<>
</>
)
}

export default PreviewFile
30 changes: 30 additions & 0 deletions stoream-webui/src/model/Pin.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// Copyright (c) 2024 The X-Files Research Institute
///
/// All rights reserved.
///
/// Redistribution and use in source and binary forms, with or without modification,
/// are permitted provided that the following conditions are met:
///
/// * Redistributions of source code must retain the above copyright notice,
/// this list of conditions and the following disclaimer.
/// * Redistributions in binary form must reproduce the above copyright notice,
/// this list of conditions and the following disclaimer in the documentation
/// and/or other materials provided with the distribution.
/// * Neither the name of Stoream nor the names of its contributors
/// may be used to endorse or promote products derived from this software
/// without specific prior written permission.
///
/// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
/// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
/// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
/// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
/// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
/// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
/// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
/// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
/// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
/// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
/// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

@genType.as("Pin")
type t = string
28 changes: 23 additions & 5 deletions stoream-webui/src/model/Request.res
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@ module Directory = {

module File = {
let cat = async (file: File.t): Fetch.Blob.t => {
await Fetch.fetch(
`/cat?path=${file.filepath}`,
{mode: #cors},
)->Promise.then(Fetch.Response.blob)
await Fetch.fetch(`/cat?path=${file.filepath}`, {mode: #cors})->Promise.then(
Fetch.Response.blob,
)
}

let upload = async (filename: string, directory: string, filevalue: Fetch.FormData.t) => {
Expand Down Expand Up @@ -151,11 +150,30 @@ module User = {
status => {
switch status {
| "OK" => ()
| _ => failwith("Wrong username or password")
| _ => Js.Exn.raiseError("Wrong username or password")
}
}
)
})
)
}
}

module Pin = {
let request = async (pin: Pin.t): unit => {
await Fetch.fetch(`${Config.config.engine}/pin?value=${pin}`, {mode: #cors})
->Promise.then(Fetch.Response.json)
->Promise.thenResolve(response => {
response
->Response.parseStatus
->(
status => {
switch status {
| "OK" => ()
| _ => Js.Exn.raiseError("Wrong PIN code")
}
}
)
})
}
}
94 changes: 94 additions & 0 deletions stoream-webui/src/pages/Account.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/// Copyright (c) 2024 The X-Files Research Institute
///
/// All rights reserved.
///
/// Redistribution and use in source and binary forms, with or without modification,
/// are permitted provided that the following conditions are met:
///
/// * Redistributions of source code must retain the above copyright notice,
/// this list of conditions and the following disclaimer.
/// * Redistributions in binary form must reproduce the above copyright notice,
/// this list of conditions and the following disclaimer in the documentation
/// and/or other materials provided with the distribution.
/// * Neither the name of Stoream nor the names of its contributors
/// may be used to endorse or promote products derived from this software
/// without specific prior written permission.
///
/// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
/// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
/// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
/// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
/// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
/// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
/// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
/// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
/// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
/// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
/// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import * as Request from "../model/Request.res.mjs"
import React from "react";
import { Stack, Input, Button, Title, Card, Space } from '@mantine/core';
import { notifications } from '@mantine/notifications';
import { useNavigate } from "react-router-dom";

const Account = () => {
const navigate = useNavigate()

const [username, setUsername] = React.useState("");
const [password, setPassword] = React.useState("");

const login = async (): Promise<void> => {
return await Request.User.request({ username, password }).then(() => {
localStorage.setItem("isLogin", "true")

notifications.show({
title: "login successful",
message: "Successfully logged in to user " + username,
color: "green"
})

navigate("/files")
}).catch(reason => {
console.log(reason.stack)
notifications.show({
title: "An error occurred during login",
message: String(reason),
color: "red"
})
})
};

return (
<Stack align="center" justify="center" gap="xl" onLoad={() => (localStorage.getItem("isLogin") === "true") ? navigate("/files") : null}>
<Space h="xl" />
<Space h="xl" />
<Space h="xl" />

<Card shadow="lg" padding="lg" radius="md" style={{ width: "20em", border: "1px solid orange" }} c="blue">
<Stack style={{ margin: "10% 10% 10% 10%" }} align="center" justify="center" gap="xl">
<Title order={2}>Login to Stoream</Title>

<Input
error={username === ""}
placeholder="Username"
style={{ width: "15em" }}
onChange={(value) => setUsername(value.target.value)}
/>

<Input
error={password === ""}
placeholder="Password" type="password"
style={{ width: "15em" }}
onChange={(value) => setPassword(value.target.value)}
/>

<Button onClick={async () => await login()} bg="orange" c="dark"> Login </Button>
</Stack>
</Card>

</Stack>
);
};

export default Account;
Loading

0 comments on commit 41d8d64

Please sign in to comment.