Browse Source

Use sha hashes instead of primary key

master
Dylan Baker 5 years ago
parent
commit
0071a707c2

+ 49
- 0
Cargo.lock View File

@@ -121,6 +121,7 @@ dependencies = [
121 121
  "r2d2-diesel 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
122 122
  "rocket 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
123 123
  "rocket_contrib 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
124
+ "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
124 125
  "serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
125 126
  "serde_derive 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
126 127
  "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -346,6 +347,11 @@ name = "fuchsia-zircon-sys"
346 347
 version = "0.3.3"
347 348
 source = "registry+https://github.com/rust-lang/crates.io-index"
348 349
 
350
+[[package]]
351
+name = "gcc"
352
+version = "0.3.55"
353
+source = "registry+https://github.com/rust-lang/crates.io-index"
354
+
349 355
 [[package]]
350 356
 name = "generic-array"
351 357
 version = "0.12.0"
@@ -849,6 +855,27 @@ dependencies = [
849 855
  "r2d2 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)",
850 856
 ]
851 857
 
858
+[[package]]
859
+name = "rand"
860
+version = "0.3.23"
861
+source = "registry+https://github.com/rust-lang/crates.io-index"
862
+dependencies = [
863
+ "libc 0.2.53 (registry+https://github.com/rust-lang/crates.io-index)",
864
+ "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
865
+]
866
+
867
+[[package]]
868
+name = "rand"
869
+version = "0.4.6"
870
+source = "registry+https://github.com/rust-lang/crates.io-index"
871
+dependencies = [
872
+ "fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
873
+ "libc 0.2.53 (registry+https://github.com/rust-lang/crates.io-index)",
874
+ "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
875
+ "rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
876
+ "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
877
+]
878
+
852 879
 [[package]]
853 880
 name = "rand"
854 881
 version = "0.6.5"
@@ -1072,11 +1099,28 @@ dependencies = [
1072 1099
  "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
1073 1100
 ]
1074 1101
 
1102
+[[package]]
1103
+name = "rust-crypto"
1104
+version = "0.2.36"
1105
+source = "registry+https://github.com/rust-lang/crates.io-index"
1106
+dependencies = [
1107
+ "gcc 0.3.55 (registry+https://github.com/rust-lang/crates.io-index)",
1108
+ "libc 0.2.53 (registry+https://github.com/rust-lang/crates.io-index)",
1109
+ "rand 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)",
1110
+ "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)",
1111
+ "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
1112
+]
1113
+
1075 1114
 [[package]]
1076 1115
 name = "rustc-demangle"
1077 1116
 version = "0.1.14"
1078 1117
 source = "registry+https://github.com/rust-lang/crates.io-index"
1079 1118
 
1119
+[[package]]
1120
+name = "rustc-serialize"
1121
+version = "0.3.24"
1122
+source = "registry+https://github.com/rust-lang/crates.io-index"
1123
+
1080 1124
 [[package]]
1081 1125
 name = "rustc_version"
1082 1126
 version = "0.2.3"
@@ -1520,6 +1564,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1520 1564
 "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
1521 1565
 "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
1522 1566
 "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
1567
+"checksum gcc 0.3.55 (registry+https://github.com/rust-lang/crates.io-index)" = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2"
1523 1568
 "checksum generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c0f28c2f5bfb5960175af447a2da7c18900693738343dc896ffbcabd9839592"
1524 1569
 "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb"
1525 1570
 "checksum htmlescape 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e9025058dae765dee5070ec375f591e2ba14638c63feff74f13805a72e523163"
@@ -1580,6 +1625,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1580 1625
 "checksum quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "faf4799c5d274f3868a4aae320a0a182cbd2baee377b378f080e16a23e9d80db"
1581 1626
 "checksum r2d2 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)" = "9dd8a293251281a4d02848925fcdbbc9f466ddb4965981bb06680359b3d12091"
1582 1627
 "checksum r2d2-diesel 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9c29bad92da76d02bc2c020452ebc3a3fe6fa74cfab91e711c43116e4fb1a3"
1628
+"checksum rand 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)" = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c"
1629
+"checksum rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
1583 1630
 "checksum rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca"
1584 1631
 "checksum rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef"
1585 1632
 "checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
@@ -1601,7 +1648,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1601 1648
 "checksum rocket_codegen 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7d907d6d458c859651c1cf4c8fa99b77685082bde0561db6a4600b365058f710"
1602 1649
 "checksum rocket_contrib 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f73e161dad5730435f51c815a5c6831d2e57b6b4299b1bf609d31b09aa9a2fa7"
1603 1650
 "checksum rocket_http 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba9d4f2ce5bba6e1b6d3100493bbad63879e99bbf6b4365d61e6f781daab324d"
1651
+"checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a"
1604 1652
 "checksum rustc-demangle 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "ccc78bfd5acd7bf3e89cffcf899e5cb1a52d6fafa8dec2739ad70c9577a57288"
1653
+"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
1605 1654
 "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
1606 1655
 "checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7"
1607 1656
 "checksum safemem 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dca453248a96cb0749e36ccdfe2b0b4e54a61bfef89fb97ec621eb8e0a93dd9"

+ 1
- 0
Cargo.toml View File

@@ -12,6 +12,7 @@ htmlescape = "0.3.1"
12 12
 r2d2 = "0.8.3"
13 13
 r2d2-diesel = "1.0.0"
14 14
 rocket = "0.4.0"
15
+rust-crypto = "0.2.36"
15 16
 serde = "1.0.82"
16 17
 serde_derive = "1.0.82"
17 18
 serde_json = "1.0.33"

+ 1
- 0
migrations/2019-05-05-152044_add_uuid_to_snippets/down.sql View File

@@ -0,0 +1 @@
1
+ALTER TABLE snippets DROP COLUMN uuid;

+ 1
- 0
migrations/2019-05-05-152044_add_uuid_to_snippets/up.sql View File

@@ -0,0 +1 @@
1
+ALTER TABLE snippets ADD COLUMN uuid VARCHAR(255) NOT NULL;

+ 1
- 0
src/main.rs View File

@@ -1,6 +1,7 @@
1 1
 #![feature(proc_macro_hygiene, decl_macro)]
2 2
 
3 3
 extern crate chrono;
4
+extern crate crypto;
4 5
 #[macro_use]
5 6
 extern crate diesel;
6 7
 extern crate dotenv;

+ 8
- 8
src/routes.rs View File

@@ -8,7 +8,7 @@ use std::collections::HashMap;
8 8
 
9 9
 use crate::connection::DbConn;
10 10
 use crate::snippet;
11
-use crate::snippet::{InsertableSnippet, Snippet};
11
+use crate::snippet::{ApiSnippet, Snippet};
12 12
 
13 13
 fn error_response(error: Error) -> Status {
14 14
     match error {
@@ -29,10 +29,10 @@ struct Context {
29 29
     lines: Vec<String>,
30 30
 }
31 31
 
32
-#[get("/snippets/<id>")]
33
-pub fn show_snippet(id: i32, connection: DbConn) -> Template {
32
+#[get("/snippets/<uuid>")]
33
+pub fn show_snippet(uuid: String, connection: DbConn) -> Template {
34 34
     let mut context: HashMap<&str, Context> = HashMap::new();
35
-    if let Ok(snippet) = snippet::get(&connection, id) {
35
+    if let Ok(snippet) = snippet::get(&connection, &uuid) {
36 36
         let lines: Vec<String> = snippet
37 37
             .formatted_body
38 38
             .split('\n')
@@ -47,9 +47,9 @@ pub fn show_snippet(id: i32, connection: DbConn) -> Template {
47 47
     }
48 48
 }
49 49
 
50
-#[get("/snippets/<id>/raw")]
51
-pub fn show_raw_snippet(id: i32, connection: DbConn) -> String {
52
-    match snippet::get(&connection, id) {
50
+#[get("/snippets/<uuid>/raw")]
51
+pub fn show_raw_snippet(uuid: String, connection: DbConn) -> String {
52
+    match snippet::get(&connection, &uuid) {
53 53
         Ok(snippet) => snippet.body,
54 54
         Err(_) => String::from("Snippet not found"),
55 55
     }
@@ -57,7 +57,7 @@ pub fn show_raw_snippet(id: i32, connection: DbConn) -> String {
57 57
 
58 58
 #[post("/api/snippets", format = "application/json", data = "<snippet>")]
59 59
 pub fn create_snippet(
60
-    snippet: Json<InsertableSnippet>,
60
+    snippet: Json<ApiSnippet>,
61 61
     connection: DbConn,
62 62
 ) -> Result<status::Created<Json<Snippet>>, Status> {
63 63
     snippet::insert(snippet.into_inner(), &connection)

+ 1
- 0
src/schema.rs View File

@@ -6,5 +6,6 @@ table! {
6 6
         created_at -> Nullable<Timestamp>,
7 7
         filetype -> Nullable<Varchar>,
8 8
         formatted_body -> Text,
9
+        uuid -> Varchar,
9 10
     }
10 11
 }

+ 26
- 5
src/snippet.rs View File

@@ -1,4 +1,6 @@
1 1
 use chrono::NaiveDateTime;
2
+use crypto::digest::Digest;
3
+use crypto::sha2::Sha256;
2 4
 use diesel::pg::PgConnection as PGC;
3 5
 use diesel::prelude::*;
4 6
 use syntect::highlighting::ThemeSet;
@@ -6,6 +8,7 @@ use syntect::html::highlighted_html_for_string;
6 8
 use syntect::parsing::SyntaxSet;
7 9
 
8 10
 use crate::schema::snippets;
11
+use crate::schema::snippets::columns::uuid;
9 12
 
10 13
 #[derive(Queryable, Serialize, Deserialize)]
11 14
 pub struct Snippet {
@@ -15,15 +18,24 @@ pub struct Snippet {
15 18
     pub created_at: Option<NaiveDateTime>,
16 19
     pub filetype: Option<String>,
17 20
     pub formatted_body: String,
21
+    pub uuid: String,
18 22
 }
19 23
 
20
-#[derive(Insertable, AsChangeset, Serialize, Deserialize)]
24
+#[derive(Serialize, Deserialize)]
25
+pub struct ApiSnippet {
26
+    pub title: String,
27
+    pub body: String,
28
+    pub filetype: Option<String>,
29
+}
30
+
31
+#[derive(Insertable, AsChangeset)]
21 32
 #[table_name = "snippets"]
22 33
 pub struct InsertableSnippet {
23 34
     pub title: String,
24 35
     pub body: String,
25 36
     pub filetype: Option<String>,
26 37
     pub formatted_body: String,
38
+    pub uuid: String,
27 39
 }
28 40
 
29 41
 fn format_snippet(filetype: Option<String>, body: String) -> String {
@@ -47,16 +59,25 @@ fn format_snippet(filetype: Option<String>, body: String) -> String {
47 59
     )
48 60
 }
49 61
 
50
-pub fn get(connection: &PGC, id: i32) -> QueryResult<Snippet> {
51
-    snippets::table.find(id).get_result::<Snippet>(connection)
62
+fn generate_uuid(snippet: &ApiSnippet) -> String {
63
+    let mut hasher = Sha256::new();
64
+    hasher.input_str(&format!("{}{}", snippet.title, snippet.body));
65
+    hasher.result_str().to_string()
66
+}
67
+
68
+pub fn get(connection: &PGC, snippet_uuid: &str) -> QueryResult<Snippet> {
69
+    snippets::table
70
+        .filter(uuid.eq(snippet_uuid))
71
+        .first(connection)
52 72
 }
53 73
 
54
-pub fn insert(snippet: InsertableSnippet, connection: &PGC) -> QueryResult<Snippet> {
74
+pub fn insert(snippet: ApiSnippet, connection: &PGC) -> QueryResult<Snippet> {
55 75
     let body = htmlescape::encode_minimal(&snippet.body.clone());
56 76
     let formatted_snippet = InsertableSnippet {
57 77
         filetype: snippet.filetype.clone(),
58
-        title: snippet.title,
78
+        title: snippet.title.clone(),
59 79
         body: body.clone(),
80
+        uuid: generate_uuid(&snippet),
60 81
         formatted_body: format_snippet(snippet.filetype, body),
61 82
     };
62 83
     diesel::insert_into(snippets::table)

+ 1
- 1
templates/snippets/show.html.tera View File

@@ -42,7 +42,7 @@
42 42
     <div class="container">
43 43
       <h1 class="header">
44 44
         {{ snippet.title }}
45
-        <a class="header__raw-link" href="/snippets/{{ snippet.id }}/raw">
45
+        <a class="header__raw-link" href="/snippets/{{ snippet.uuid }}/raw">
46 46
           raw
47 47
         </a>
48 48
       </h1>

Loading…
Cancel
Save