forked from ShiftLeftSecurity/shiftleft-java-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminController.java
More file actions
137 lines (123 loc) · 4.33 KB
/
AdminController.java
File metadata and controls
137 lines (123 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package io.shiftleft.controller;
import io.shiftleft.model.AuthToken;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Admin checks login
*/
@Controller
public class AdminController {
private String fail = "redirect:/";
// helper
private boolean isAdmin(String auth)
{
try {
ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(auth));
ObjectInputStream objectInputStream = new ObjectInputStream(bis);
Object authToken = objectInputStream.readObject();
return ((AuthToken) authToken).isAdmin();
} catch (Exception ex) {
System.out.println(" cookie cannot be deserialized: "+ex.getMessage());
return false;
}
}
//
@RequestMapping(value = "/admin/printSecrets", method = RequestMethod.POST)
public String doPostPrintSecrets(HttpServletResponse response, HttpServletRequest request) {
return fail;
}
@RequestMapping(value = "/admin/printSecrets", method = RequestMethod.GET)
public String doGetPrintSecrets(@CookieValue(value = "auth", defaultValue = "notset") String auth, HttpServletResponse response, HttpServletRequest request) throws Exception {
if (request.getSession().getAttribute("auth") == null) {
return fail;
}
/*
String authToken = request.getSession().getAttribute("auth").toString();
if(!isAdmin(authToken)) {
return fail;
}
*/
ClassPathResource cpr = new ClassPathResource("static/calculations.csv");
try {
byte[] bdata = FileCopyUtils.copyToByteArray(cpr.getInputStream());
response.getOutputStream().println(new String(bdata, StandardCharsets.UTF_8));
return null;
} catch (IOException ex) {
ex.printStackTrace();
// redirect to /
return fail;
}
}
/**
* Handle login attempt
* @param auth cookie value base64 encoded
* @param password hardcoded value
* @param response -
* @param request -
* @return redirect to company numbers
* @throws Exception
*/
@RequestMapping(value = "/admin/login", method = RequestMethod.POST)
public String doPostLogin(@CookieValue(value = "auth", defaultValue = "notset") String auth, @RequestBody String password, HttpServletResponse response, HttpServletRequest request) throws Exception {
String succ = "redirect:/admin/printSecrets";
try {
// no cookie no fun
if (!auth.equals("notset")) {
if(isAdmin(auth)) {
request.getSession().setAttribute("auth",auth);
return succ;
}
}
// split password=value
String[] pass = password.split("=");
if(pass.length!=2) {
return fail;
}
// compare pass
if(pass[1] != null && pass[1].length()>0 && pass[1].equals("shiftleftsecret"))
{
AuthToken authToken = new AuthToken(AuthToken.ADMIN);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(authToken);
String cookieValue = new String(Base64.getEncoder().encode(bos.toByteArray()));
response.addCookie(new Cookie("auth", cookieValue ));
// cookie is lost after redirection
request.getSession().setAttribute("auth",cookieValue);
return succ;
}
return fail;
}
catch (Exception ex)
{
ex.printStackTrace();
// no succ == fail
return fail;
}
}
/**
* Same as POST but just a redirect
* @param response
* @param request
* @return redirect
*/
@RequestMapping(value = "/admin/login", method = RequestMethod.GET)
public String doGetLogin(HttpServletResponse response, HttpServletRequest request) {
return "redirect:/";
}
}