Skip to content

Commit

Permalink
fixed client file access
Browse files Browse the repository at this point in the history
  • Loading branch information
catink123 committed Jan 21, 2024
1 parent c83cea5 commit e121204
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 184 deletions.
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ add_executable(
common_state.cpp
)

set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 17)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 20)

# boost windows-specific setting
if (WIN32)
Expand All @@ -37,7 +37,7 @@ target_link_libraries(

# password-hash generator
add_executable(password_hasher hash_generator.cpp)
set_property(TARGET password_hasher PROPERTY CXX_STANDARD 17)
set_property(TARGET password_hasher PROPERTY CXX_STANDARD 20)
target_link_libraries(password_hasher bcrypt)

# install the server binary and the password hasher to bin
Expand Down
18 changes: 18 additions & 0 deletions src/auth.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
#include "auth.hpp"

std::optional<AuthorizationType> get_endpoint_permissions(
std::string_view path
) {
for (const auto& pair : endpoint_map) {
const std::string& endpoint = pair.first;
const auto& permissions = pair.second;

if (path.starts_with(endpoint)) {
if (path.size() > endpoint.size() && path[endpoint.size()] != '/') {
return get_endpoint_permissions(path.substr(0, path.rfind('/') + 1));
}
return permissions;
}
}

return Blocked;
}

std::optional<std::unordered_map<std::string, auth_data>>
open_auth_table_from_file(fs::path file_path) {
std::ifstream file;
Expand Down
36 changes: 7 additions & 29 deletions src/auth.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#define AUTH_HPP

#include "common.hpp"
#include <unordered_map>
#include <optional>
#include <bcrypt.h>
#include <boost/beast/core/detail/base64.hpp>
#include <array>
#include <unordered_map>
#include <utility>
#include <vector>
#include <filesystem>
#include <fstream>

Expand All @@ -20,37 +20,15 @@ enum AuthorizationType {
Control = 2,
};

const std::unordered_map<std::string, std::optional<AuthorizationType>> endpoint_map = {
{ "/", std::nullopt },
const std::vector<std::pair<std::string, std::optional<AuthorizationType>>> endpoint_map = {
{ "/control", Control },
{ "/view", View }
{ "/view", View },
{ "/", std::nullopt }
};

template <class Body, class Allocator>
std::optional<AuthorizationType> get_endpoint_permissions(
const http::request<Body, http::basic_fields<Allocator>>& req
) {
auto& target = req.target();
std::size_t last_delimeter = target.rfind('/');
std::string endpoint;
if (last_delimeter == 0) {
if (target.size() > 1) {
endpoint = target;
}
else {
endpoint = "/";
}
}
else {
endpoint = target.substr(0, last_delimeter);
}

if (endpoint_map.find(endpoint) == endpoint_map.end()) {
return Blocked;
}

return endpoint_map.at(endpoint);
}
std::string_view path
);

struct auth_data {
AuthorizationType permissions;
Expand Down
57 changes: 3 additions & 54 deletions src/client/control/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,10 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gate Control</title>

<style>
body {
color: white;
background: hsl(221, 33%, 13%);
font-family: sans-serif;
display: grid;
place-items: center;
height: 100dvh;
margin: 0;
}

body > div {
display: grid;
place-items: center;
gap: 10px;
}

h1 {
margin: 0;
}
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="state-styles.css">

<style>
button {
border: 1px solid hsl(221, 33%, 30%);
padding: 10px 15px;
Expand Down Expand Up @@ -54,46 +37,12 @@
gap: 10px;
}

.state {
border-radius: 50px;
display: block;
width: 16px;
height: 16px;
transition-duration: .5s;
background: hsl(221, 33%, 30%);
}

.state.indeterminate {
background: rgb(255, 255, 82);
}

.state.raised {
background: rgb(79, 235, 79);
}

.state.lowered {
background: rgb(255, 70, 70);
}

.state-block {
display: flex;
align-items: center;
gap: 10px;
padding: 13px 15px;
border: 1px solid hsl(221, 33%, 30%);
border-radius: 15px;
}

.state-container {
display: flex;
align-items: center;
gap: 15px;
padding-top: 10px;
}

p {
margin: 0;
}
</style>

<script>
Expand Down
45 changes: 1 addition & 44 deletions src/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gate Control</title>

<style>
body {
color: white;
background: hsl(221, 33%, 13%);
font-family: sans-serif;
display: grid;
place-items: center;
height: 100dvh;
margin: 0;
}

body > div {
display: grid;
place-items: center;
gap: 10px;
}

h1 {
margin: 0;
}

a {
border: 1px solid hsl(221, 33%, 30%);
padding: 10px 15px;
color: white;
text-decoration: none;
border-radius: 10px;
transition-duration: .1s;
background:hsl(221, 33%, 15%);
}

a:hover {
background:hsl(221, 33%, 30%);
}

a:active {
background:hsl(221, 33%, 10%);
}

div.linkbar {
display: flex;
gap: 10px;
}
</style>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div>
Expand Down
29 changes: 29 additions & 0 deletions src/client/state-styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.state {
border-radius: 50px;
display: block;
width: 16px;
height: 16px;
transition-duration: .5s;
background: hsl(221, 33%, 30%);
}

.state.indeterminate {
background: rgb(255, 255, 82);
}

.state.raised {
background: rgb(79, 235, 79);
}

.state.lowered {
background: rgb(255, 70, 70);
}

.state-block {
display: flex;
align-items: center;
gap: 10px;
padding: 13px 15px;
border: 1px solid hsl(221, 33%, 30%);
border-radius: 15px;
}
42 changes: 42 additions & 0 deletions src/client/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
body {
color: white;
background: hsl(221, 33%, 13%);
font-family: sans-serif;
display: grid;
place-items: center;
height: 100dvh;
margin: 0;
}

body>div {
display: grid;
place-items: center;
gap: 10px;
}

h1, p {
margin: 0;
}

a {
border: 1px solid hsl(221, 33%, 30%);
padding: 10px 15px;
color: white;
text-decoration: none;
border-radius: 10px;
transition-duration: .1s;
background: hsl(221, 33%, 15%);
}

a:hover {
background: hsl(221, 33%, 30%);
}

a:active {
background: hsl(221, 33%, 10%);
}

div.linkbar {
display: flex;
gap: 10px;
}
56 changes: 2 additions & 54 deletions src/client/view/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,9 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gate Control</title>

<style>
body {
color: white;
background: hsl(221, 33%, 13%);
font-family: sans-serif;
display: grid;
place-items: center;
height: 100dvh;
margin: 0;
}
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="state-styles.css">

body > div {
display: grid;
place-items: center;
gap: 10px;
}

h1 {
margin: 0;
}

.state {
border-radius: 50px;
display: block;
width: 16px;
height: 16px;
transition-duration: .5s;
background: hsl(221, 33%, 30%);
}

.state.indeterminate {
background: rgb(255, 255, 82);
}

.state.raised {
background: rgb(79, 235, 79);
}

.state.lowered {
background: rgb(255, 70, 70);
}

.state-block {
display: flex;
align-items: center;
gap: 10px;
padding: 13px 15px;
border: 1px solid hsl(221, 33%, 30%);
border-radius: 15px;
}

p {
margin: 0;
}
</style>
<script>
const message_responses = {
raised: {
Expand Down
2 changes: 1 addition & 1 deletion src/http_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ http::message_generator handle_request(
}

// if the request target is not the root page...
if (const auto endpoint_perms = get_endpoint_permissions(req)) {
if (const auto endpoint_perms = get_endpoint_permissions(req.target())) {
// make sure the client has sufficient permissions
if (
req.find(http::field::authorization) == req.end()
Expand Down

0 comments on commit e121204

Please sign in to comment.