Skip to content

Commit 780e005

Browse files
authored
Merge pull request #4 from APSN4/feature/password
feature: encrypt code snippets with a password
2 parents 23f7db8 + aa8b981 commit 780e005

File tree

12 files changed

+201
-10
lines changed

12 files changed

+201
-10
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ out/
3737
.vscode/
3838

3939
### H2 Database ###
40-
*.db
40+
*.db
41+
42+
### macOS files ###
43+
.DS_Store

src/main/java/codes/sharing/sharingcodes/controller/GetController.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ public GetController(@Autowired CodeService codeService) {
2020
}
2121

2222
@GetMapping("/code/{N}")
23-
public String getNthCode(@PathVariable String N, Model model) {
23+
public String getNthCode(@PathVariable String N, @RequestParam(value = "password", required = false) String password, Model model) {
2424
try {
2525
Code currentCode = codeService.getById(N);
26+
27+
if (password == null) password = "";
28+
if (!password.equals(currentCode.getPassword())) {
29+
return "password";
30+
}
31+
codeService.refreshCode(currentCode);
32+
2633
DateDTO dateDTO = codeService.formatDate(currentCode.getDate());
2734
model.addAttribute("pieceOfCode", currentCode);
2835
model.addAttribute("dateDTO", dateDTO);

src/main/java/codes/sharing/sharingcodes/controller/PostController.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package codes.sharing.sharingcodes.controller;
22

3+
import codes.sharing.sharingcodes.dto.PasswordDTO;
34
import codes.sharing.sharingcodes.model.Code;
45
import codes.sharing.sharingcodes.service.CodeService;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.http.MediaType;
8+
import org.springframework.http.ResponseEntity;
79
import org.springframework.web.bind.annotation.*;
810
import org.springframework.stereotype.Controller;
911

12+
import java.util.HashMap;
1013
import java.util.Map;
1114

1215
@Controller
@@ -40,4 +43,15 @@ public Map<String, String> createApiCode(@RequestBody Code newCode) {
4043
public Object getLatestApiCodes() {
4144
return codeService.getLatestNCode(10);
4245
}
46+
47+
@PostMapping(value = "/api/code/password", produces = MediaType.APPLICATION_JSON_VALUE)
48+
@ResponseBody
49+
public ResponseEntity<Object> checkPassword(@RequestBody PasswordDTO passwordDTO) {
50+
Code currentCode = codeService.getById(passwordDTO.getId());
51+
if (currentCode.getPassword().equals(passwordDTO.getPassword())) {
52+
return ResponseEntity.ok().build();
53+
} else {
54+
return ResponseEntity.badRequest().build();
55+
}
56+
}
4357
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package codes.sharing.sharingcodes.dto;
2+
3+
public class PasswordDTO {
4+
5+
public PasswordDTO(String password, String id) {
6+
this.password = password;
7+
this.id = id;
8+
}
9+
10+
public PasswordDTO(String password) {
11+
this.password = password;
12+
}
13+
public PasswordDTO() {
14+
this.password = "";
15+
}
16+
17+
private String password;
18+
private String id;
19+
20+
public String getPassword() {
21+
return password;
22+
}
23+
24+
public void setPassword(String password) {
25+
this.password = password;
26+
}
27+
28+
public String getId() {
29+
return id;
30+
}
31+
32+
public void setId(String id) {
33+
this.id = id;
34+
}
35+
}

src/main/java/codes/sharing/sharingcodes/model/Code.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public class Code {
3636
@Column(name = "timelimit")
3737
private boolean timeLimit;
3838

39+
@Column(name = "password")
40+
private String password;
41+
3942
public Code() {
4043

4144
}
@@ -53,6 +56,7 @@ public Code(Code code) {
5356
if (this.time > 0) {
5457
this.timeLimit = true;
5558
}
59+
this.password = code.getPassword() == null ? "" : code.getPassword();
5660
}
5761

5862
public String getId() {
@@ -131,4 +135,12 @@ public String shortCode() {
131135
return getCode();
132136
}
133137
}
138+
139+
public String getPassword() {
140+
return password;
141+
}
142+
143+
public void setPassword(String password) {
144+
this.password = password;
145+
}
134146
}

src/main/java/codes/sharing/sharingcodes/service/CodeService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package codes.sharing.sharingcodes.service;
22

33
import codes.sharing.sharingcodes.dto.DateDTO;
4+
import codes.sharing.sharingcodes.dto.PasswordDTO;
45
import codes.sharing.sharingcodes.model.Code;
56

67
import java.util.List;
@@ -11,6 +12,8 @@ public interface CodeService {
1112

1213
public void putCode(Code newCode);
1314

15+
public void refreshCode(Code code);
16+
1417
public List<Code> getLatestNCode(int n);
1518

1619
public boolean isExist(String id);

src/main/java/codes/sharing/sharingcodes/service/CodeServiceImpl.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package codes.sharing.sharingcodes.service;
22

33
import codes.sharing.sharingcodes.dto.DateDTO;
4+
import codes.sharing.sharingcodes.dto.PasswordDTO;
45
import codes.sharing.sharingcodes.exceptions.NotFoundSnippet;
56
import codes.sharing.sharingcodes.model.Code;
67
import codes.sharing.sharingcodes.repository.CodeRepository;
@@ -24,10 +25,6 @@ public CodeServiceImpl(@Autowired CodeRepository repo) {
2425

2526
@Override
2627
public Code getById(String id) {
27-
Code code = repo.findById(id).orElseThrow(() -> new NotFoundSnippet(id));
28-
if (code.hasLimit()) {
29-
refresh(code);
30-
}
3128
return repo.findById(id).orElseThrow(() -> new NotFoundSnippet(id));
3229
}
3330

@@ -38,6 +35,14 @@ public void putCode(Code newCode) {
3835
repo.save(code);
3936
}
4037

38+
@Override
39+
public void refreshCode(Code code) {
40+
if (code.hasLimit()) {
41+
refresh(code);
42+
}
43+
}
44+
45+
4146
@Override
4247
public List<Code> getLatestNCode(int n) {
4348
List<Code> codes = (List<Code>) repo.findAll();
@@ -78,7 +83,7 @@ public List<Code> superGetAll() {
7883
private void refresh(Code code) {
7984
if (code.isViewsLimit() && code.getViews() >= 0) {
8085
code.setViews(code.getViews() - 1);
81-
if (code.getViews() < 0) {
86+
if (code.getViews() <= 0) {
8287
repo.delete(code);
8388
return;
8489
}

src/main/resources/static/js/myScript.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ function send() {
2222
let object = {
2323
"code": document.getElementById("code_snippet").value,
2424
"views": document.getElementById("views_restriction").value,
25-
"time": document.getElementById("time_restriction").value
25+
"time": document.getElementById("time_restriction").value,
26+
"password": document.getElementById("password_field").value
2627
};
2728

2829
let json = JSON.stringify(object);
@@ -55,4 +56,32 @@ function send() {
5556
errorCode.innerHTML = xhr.status + " " + xhr.statusText;
5657
return;
5758
}
58-
}
59+
}
60+
61+
function check() {
62+
const codeId = location.pathname.substring(6);
63+
var object = {
64+
"password": document.getElementById("password_field").value,
65+
"id": codeId
66+
};
67+
let json = JSON.stringify(object);
68+
69+
try {
70+
xhr = new XMLHttpRequest();
71+
xhr.open("POST", '/api/code/password', false)
72+
xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
73+
xhr.send(json);
74+
75+
var url = location.protocol + '//' + location.host + location.pathname + '?password=' + object.password;
76+
77+
if (xhr.status == 200) {
78+
window.location.href = url;
79+
} else if (xhr.status == 400){
80+
dangerMessage.style.display = "block";
81+
} else {
82+
window.location.href = url;
83+
}
84+
} catch (error) {
85+
console.error(error.message);
86+
}
87+
}

src/main/resources/templates/getcode.ftlh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<meta charset="UTF-8">
66
<meta http-equiv="X-UA-Compatible" content="IE=edge">
77
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<meta name="page-status" content="available"/>
89
<link rel="stylesheet" href="/css/bootstrap.min.css">
910
<link rel="stylesheet" href="/css/styles.css">
1011
<script src="https://kit.fontawesome.com/224143ea2d.js" crossorigin="anonymous"></script>

src/main/resources/templates/index.ftlh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,18 @@
6060
<small class="form-text text-muted">This will allow viewing a code snippet for a certain period of time, and after its expiration, the code snippet is deleted from the database.</small>
6161
</div>
6262
</div>
63+
<p></p>
6364
<button id="send_snippet" type="submit" class="btn btn-primary">Submit</button>
65+
<button id="set_password" type="button" class="btn btn-outline-primary" data-toggle="collapse" href="#collapsePassword" role="button" aria-expanded="false" aria-controls="collapsePassword">Set password</button>
66+
<p></p>
67+
<div class="collapse" id="collapsePassword">
68+
<div class="input-group mb-3">
69+
<div class="input-group-prepend">
70+
<span class="input-group-text" id="inputGroup-sizing-default">Password</span>
71+
</div>
72+
<input type="text" class="form-control" id="password_field" aria-label="Password input" aria-describedby="inputGroup-sizing-default">
73+
</div>
74+
</div>
6475
</div>
6576
</form>
6677
</div>

0 commit comments

Comments
 (0)