2020import org .springframework .web .bind .annotation .RequestMethod ;
2121
2222
23- /**
24- * Admin checks login
25- */
2623@ Controller
2724public class AdminController {
2825 private String fail = "redirect:/" ;
2926
30- // helper
31- private boolean isAdmin (String auth )
32- {
27+ private boolean isAdmin (String auth ) {
3328 try {
3429 ByteArrayInputStream bis = new ByteArrayInputStream (Base64 .getDecoder ().decode (auth ));
3530 ObjectInputStream objectInputStream = new ObjectInputStream (bis );
3631 Object authToken = objectInputStream .readObject ();
37- return ((AuthToken ) authToken ).isAdmin ();
32+ if (authToken instanceof AuthToken ) {
33+ return ((AuthToken ) authToken ).isAdmin ();
34+ }
35+ return false ;
3836 } catch (Exception ex ) {
39- System .out .println (" cookie cannot be deserialized: " + ex .getMessage ());
37+ System .out .println (" cookie cannot be deserialized: " + ex .getMessage ());
4038 return false ;
4139 }
4240 }
4341
44- //
4542 @ RequestMapping (value = "/admin/printSecrets" , method = RequestMethod .POST )
4643 public String doPostPrintSecrets (HttpServletResponse response , HttpServletRequest request ) {
4744 return fail ;
4845 }
4946
50-
5147 @ RequestMapping (value = "/admin/printSecrets" , method = RequestMethod .GET )
5248 public String doGetPrintSecrets (@ CookieValue (value = "auth" , defaultValue = "notset" ) String auth , HttpServletResponse response , HttpServletRequest request ) throws Exception {
5349
@@ -56,7 +52,7 @@ public String doGetPrintSecrets(@CookieValue(value = "auth", defaultValue = "not
5652 }
5753
5854 String authToken = request .getSession ().getAttribute ("auth" ).toString ();
59- if (!isAdmin (authToken )) {
55+ if (!isAdmin (authToken )) {
6056 return fail ;
6157 }
6258
@@ -67,71 +63,45 @@ public String doGetPrintSecrets(@CookieValue(value = "auth", defaultValue = "not
6763 return null ;
6864 } catch (IOException ex ) {
6965 ex .printStackTrace ();
70- // redirect to /
7166 return fail ;
7267 }
7368 }
7469
75- /**
76- * Handle login attempt
77- * @param auth cookie value base64 encoded
78- * @param password hardcoded value
79- * @param response -
80- * @param request -
81- * @return redirect to company numbers
82- * @throws Exception
83- */
8470 @ RequestMapping (value = "/admin/login" , method = RequestMethod .POST )
8571 public String doPostLogin (@ CookieValue (value = "auth" , defaultValue = "notset" ) String auth , @ RequestBody String password , HttpServletResponse response , HttpServletRequest request ) throws Exception {
8672 String succ = "redirect:/admin/printSecrets" ;
8773
8874 try {
89- // no cookie no fun
9075 if (!auth .equals ("notset" )) {
91- if (isAdmin (auth )) {
92- request .getSession ().setAttribute ("auth" ,auth );
76+ if (isAdmin (auth )) {
77+ request .getSession ().setAttribute ("auth" , auth );
9378 return succ ;
9479 }
9580 }
9681
97- // split password=value
9882 String [] pass = password .split ("=" );
99- if (pass .length != 2 ) {
83+ if (pass .length != 2 ) {
10084 return fail ;
10185 }
102- // compare pass
103- if (pass [1 ] != null && pass [1 ].length ()>0 && pass [1 ].equals ("shiftleftsecret" ))
104- {
86+ if (pass [1 ] != null && pass [1 ].length () > 0 && pass [1 ].equals ("shiftleftsecret" )) {
10587 AuthToken authToken = new AuthToken (AuthToken .ADMIN );
10688 ByteArrayOutputStream bos = new ByteArrayOutputStream ();
10789 ObjectOutputStream oos = new ObjectOutputStream (bos );
10890 oos .writeObject (authToken );
10991 String cookieValue = new String (Base64 .getEncoder ().encode (bos .toByteArray ()));
110- response .addCookie (new Cookie ("auth" , cookieValue ));
92+ Cookie authCookie = new Cookie ("auth" , cookieValue );
93+ authCookie .setHttpOnly (true );
94+ authCookie .setSecure (true );
95+ response .addCookie (authCookie );
11196
112- // cookie is lost after redirection
113- request .getSession ().setAttribute ("auth" ,cookieValue );
97+ request .getSession ().setAttribute ("auth" , cookieValue );
11498
11599 return succ ;
116100 }
117101 return fail ;
118- }
119- catch (Exception ex )
120- {
102+ } catch (Exception ex ) {
121103 ex .printStackTrace ();
122- // no succ == fail
123104 return fail ;
124105 }
125106 }
126-
127- /**
128- * Same as POST but just a redirect
129- * @param response
130- * @param request
131- * @return redirect
132- */
133- @ RequestMapping (value = "/admin/login" , method = RequestMethod .GET )
134- public String doGetLogin (HttpServletResponse response , HttpServletRequest request ) {
135- return "redirect:/" ;
136- }
137107}
0 commit comments