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 {

    /**
     * extracts the email adress from the keycloak context.
     * returns null if not successful.
     * @return
     */
    public String getEmail() {
        AccessToken token = null;
        try {
            token = getAccessToken();
        } catch (ServletException e) {
            return null;
        }
        return token.getEmail();
    }

    /**
     * extracts the first name from the keycloak context.
     * returns null if not successful.
     * @return
     */
    public String getFirstName() {
        AccessToken token = null;
        try {
            token = getAccessToken();
        } catch (ServletException e) {
            return null;
        }
        return token.getGivenName();
    }

    /**
     * extracts the last name from the keycloak context.
     * returns null if not successful.
     * @return
     */
    public String getLastName() {
        AccessToken token = null;
        try {
            token = getAccessToken();
        } catch (ServletException e) {
            return null;
        }
        return token.getFamilyName();
    }

    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;
    }
}

@@ -1470,24 +1470,280 @@
 e();%0A    %7D%0A%0A
+    public String getTokenString() %7B%0A        //noinspection unchecked%0A        return ((KeycloakPrincipal%3CKeycloakSecurityContext%3E) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getKeycloakSecurityContext().getTokenString();%0A    %7D%0A%0A
     private 
@@ -1797,32 +1797,38 @@
 ption %7B%0A        
+final 
 AccessToken toke

Add method to KeycloakUtilizer to get Token as String