|
21 | 21 | package eu.openanalytics.containerproxy.ui; |
22 | 22 |
|
23 | 23 | import eu.openanalytics.containerproxy.api.BaseController; |
| 24 | +import eu.openanalytics.containerproxy.api.dto.ApiResponse; |
24 | 25 | import eu.openanalytics.containerproxy.auth.IAuthenticationBackend; |
25 | 26 | import eu.openanalytics.containerproxy.auth.impl.OpenIDAuthenticationBackend; |
26 | 27 | import eu.openanalytics.containerproxy.auth.impl.SAMLAuthenticationBackend; |
|
30 | 31 | import org.springframework.context.MessageSource; |
31 | 32 | import org.springframework.context.i18n.LocaleContextHolder; |
32 | 33 | import org.springframework.core.env.Environment; |
| 34 | +import org.springframework.http.MediaType; |
| 35 | +import org.springframework.http.ResponseEntity; |
| 36 | +import org.springframework.security.authentication.AnonymousAuthenticationToken; |
| 37 | +import org.springframework.security.core.Authentication; |
| 38 | +import org.springframework.security.core.context.SecurityContextHolder; |
33 | 39 | import org.springframework.stereotype.Controller; |
34 | 40 | import org.springframework.ui.ModelMap; |
| 41 | +import org.springframework.web.bind.annotation.GetMapping; |
35 | 42 | import org.springframework.web.bind.annotation.RequestMapping; |
36 | 43 | import org.springframework.web.bind.annotation.RequestMethod; |
37 | 44 | import org.springframework.web.bind.annotation.RequestParam; |
| 45 | +import org.springframework.web.bind.annotation.ResponseBody; |
38 | 46 | import org.springframework.web.servlet.support.ServletUriComponentsBuilder; |
39 | 47 | import org.springframework.web.servlet.view.RedirectView; |
40 | 48 |
|
41 | 49 | import javax.inject.Inject; |
42 | 50 | import java.util.Locale; |
| 51 | +import java.util.Map; |
43 | 52 | import java.util.Optional; |
44 | 53 |
|
45 | 54 | @Controller |
@@ -68,7 +77,7 @@ public Object getLoginPage(@RequestParam Optional<String> error, ModelMap map) { |
68 | 77 | if (error.get().equals("expired")) { |
69 | 78 | map.put("error", messageSource.getMessage("auth.simple.expired_error", null, locale)); |
70 | 79 | } else { |
71 | | - map.put("error", messageSource.getMessage("auth.simple.credentials_error", null, locale)); |
| 80 | + map.put("error", messageSource.getMessage("auth.simple.credentials_error", null, locale)); |
72 | 81 | } |
73 | 82 | } |
74 | 83 |
|
@@ -119,4 +128,23 @@ public String getLogoutSuccessPage(ModelMap map) { |
119 | 128 | return "logout-success"; |
120 | 129 | } |
121 | 130 |
|
| 131 | + |
| 132 | + @ResponseBody |
| 133 | + @GetMapping(value = "/user/me", produces = MediaType.APPLICATION_JSON_VALUE) |
| 134 | + public ResponseEntity<ApiResponse<Map<String, Object>>> getUserMetadata() { |
| 135 | + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 136 | + boolean isLoggedIn = authentication != null && !(authentication instanceof AnonymousAuthenticationToken) && authentication.isAuthenticated(); |
| 137 | + if (!isLoggedIn) { |
| 138 | + return ApiResponse.success( |
| 139 | + Map.of("authenticated", false) |
| 140 | + ); |
| 141 | + } |
| 142 | + return ApiResponse.success( |
| 143 | + Map.of( |
| 144 | + "authenticated", true, |
| 145 | + "username", authentication.getName() |
| 146 | + ) |
| 147 | + ); |
| 148 | + } |
| 149 | + |
122 | 150 | } |
0 commit comments