Commit d34908fc authored by Matija Obreza's avatar Matija Obreza

Fix: /me controller support for authentication with client credentials

parent 8a25a86f
......@@ -18,10 +18,13 @@ package org.genesys2.server.api.v0;
import java.util.List;
import org.genesys.blocks.oauth.service.OAuthClientDetailsService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.api.ModelValidationException;
import org.genesys2.server.api.OAuth2Cleanup;
import org.genesys2.server.component.security.SecurityUtils;
import org.genesys2.server.exception.AuthorizationException;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.impl.User;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.TeamService;
......@@ -32,6 +35,7 @@ import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -70,6 +74,9 @@ public class MeController extends ApiBaseController {
@Autowired(required = false)
private ConsumerTokenServices tokenServices;
@Autowired
private OAuthClientDetailsService oauthClientService;
/**
* Delete provided token
*
......@@ -105,9 +112,30 @@ public class MeController extends ApiBaseController {
@RequestMapping(value = "", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Object getProfile() throws AuthorizationException {
// TODO current user may be the OAuth client itself if it has client_credentials grant. Fix this.
final User user = userService.getMe();
return OAuth2Cleanup.clean(user);
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication instanceof OAuth2Authentication) {
OAuth2Authentication oauthAuth = (OAuth2Authentication) authentication;
System.err.println(oauthAuth);
if (oauthAuth.isClientOnly()) {
LOG.debug("OAuth client-only authentication: {}", oauthAuth);
Object principal = oauthAuth.getPrincipal();
LOG.debug("OAuth client-only principal: {} {}", principal.getClass(), principal);
return oauthClientService.getClient(oauthAuth.getName());
}
Authentication userAuth = oauthAuth.getUserAuthentication();
if (userAuth != null) {
System.err.println(userAuth);
LOG.debug("OAuth user authentication: {} {}", userAuth.getClass(), userAuth);
Object principal = userAuth.getPrincipal();
LOG.debug("OAuth user principal: {} {}", principal.getClass(), principal);
return userService.loadUserByUsername(userAuth.getName());
}
}
throw new NotFoundElement("You don't exist");
}
@RequestMapping(value = "/institutes", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment