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

    public String getTokenString() {
        //noinspection unchecked
        return ((KeycloakPrincipal<KeycloakSecurityContext>) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getKeycloakSecurityContext().getTokenString();
    }

    private AccessToken getAccessToken() throws ServletException {
        final 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;
    }
}

@@ -487,39 +487,24 @@
 essful.%0A
-     * @return%0A
      */%0A
     publ
@@ -487,32 +487,32 @@
 essful.%0A     */%0A
+
     public Strin
@@ -526,32 +526,38 @@
 ail() %7B%0A        
+final 
 AccessToken toke
@@ -549,39 +549,32 @@
 ccessToken token
- = null
 ;%0A        try %7B%0A
@@ -620,32 +620,38 @@
        %7D catch (
+final 
 ServletException
@@ -832,39 +832,24 @@
 successful.%0A
-     * @return%0A
      */%0A    
@@ -879,32 +879,38 @@
 ame() %7B%0A        
+final 
 AccessToken toke
@@ -902,39 +902,32 @@
 ccessToken token
- = null
 ;%0A        try %7B%0A
@@ -973,32 +973,38 @@
        %7D catch (
+final 
 ServletException
@@ -1196,23 +1196,8 @@
 ul.%0A
-     * @return%0A
     
@@ -1242,16 +1242,22 @@
         
+final 
 AccessTo
@@ -1269,15 +1269,8 @@
 oken
- = null
 ;%0A  
@@ -1336,16 +1336,22 @@
  catch (
+final 
 ServletE
@@ -1826,16 +1826,53 @@
         
+@SuppressWarnings(%22unchecked%22) final 
 Keycloak
@@ -2072,32 +2072,32 @@
 t().getToken();%0A
-
         %7D catch 
@@ -2097,16 +2097,22 @@
  catch (
+final 
 Exceptio

Clean up KeycloakUtilizer.java