package de.fau.amos.virtualledger.server.auth;

import org.keycloak.KeycloakPrincipal;
import org.keycloak.KeycloakSecurityContext;
import org.keycloak.representations.AccessToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;

@Component
public class KeycloakUtilizer {

    public String getEmail() throws ServletException {
        AccessToken token = getAccessToken();
        return token.getEmail();
    }

    private AccessToken getAccessToken() throws ServletException {
        AccessToken token;
        try {
            KeycloakPrincipal principal = (KeycloakPrincipal<KeycloakSecurityContext>) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            token = principal.getKeycloakSecurityContext().getToken();
        } catch (Exception ex) {
            throw new ServletException("Failure at getting data about the user by the identity token!");
        }
        return token;
    }
}

@@ -388,104 +388,324 @@
     
-public String getEmail() throws ServletException %7B%0A        AccessToken token = getAccessToken();
+/**%0A     * extracts the email adress from the keycloak context.%0A     * returns null if not successful.%0A     * @return%0A     */%0A    public String getEmail() %7B%0A        AccessToken token = null;%0A        try %7B%0A            token = getAccessToken();%0A        %7D catch (ServletException e) %7B%0A            return null;%0A        %7D
 %0A   

return null on KeycoakUtilizer.getEmail() instead of throwing exception