package com.sequenceiq.cloudbreak.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.stereotype.Service;
import com.sequenceiq.cloudbreak.domain.CbUser;
import com.sequenceiq.cloudbreak.service.user.UserDetailsService;
import com.sequenceiq.cloudbreak.service.user.UserFilterField;
@Service
public class AuthenticatedUserService {
@Autowired
private UserDetailsService userDetailsService;
public CbUser getCbUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
OAuth2Authentication oauth = (OAuth2Authentication) authentication;
if (oauth.getUserAuthentication() != null) {
String username = (String) authentication.getPrincipal();
return userDetailsService.getDetails(username, UserFilterField.USERNAME);
}
}
return null;
}
}