Files
project-auth-server/build/reports/jacoco/aggregate/html/com.project.auth.config.auth.security/KeycloakGrantedAuthoritiesConverter.java.html
T

50 lines
3.9 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>KeycloakGrantedAuthoritiesConverter.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">project-auth-server</a> &gt; <a href="index.source.html" class="el_package">com.project.auth.config.auth.security</a> &gt; <span class="el_source">KeycloakGrantedAuthoritiesConverter.java</span></div><h1>KeycloakGrantedAuthoritiesConverter.java</h1><pre class="source lang-java linenums">package com.project.auth.config.auth.security;
import org.springframework.core.convert.converter.Converter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
<span class="fc" id="L15">public class KeycloakGrantedAuthoritiesConverter implements Converter&lt;Jwt, Collection&lt;GrantedAuthority&gt;&gt; {</span>
private static final String CLAIM_REALM_ACCESS = &quot;realm_access&quot;;
private static final String CLAIM_ROLES = &quot;roles&quot;;
private static final String ROLE_PREFIX = &quot;ROLE_&quot;;
<span class="fc" id="L21"> private final JwtGrantedAuthoritiesConverter scopeConverter = new JwtGrantedAuthoritiesConverter();</span>
@Override
public Collection&lt;GrantedAuthority&gt; convert(Jwt jwt) {
<span class="fc" id="L25"> Set&lt;GrantedAuthority&gt; authorities = new LinkedHashSet&lt;&gt;(scopeConverter.convert(jwt));</span>
<span class="fc bfc" id="L26" title="All 2 branches covered."> for (String role : realmRoles(jwt)) {</span>
<span class="fc" id="L27"> authorities.add(new SimpleGrantedAuthority(ROLE_PREFIX + role));</span>
<span class="fc" id="L28"> }</span>
<span class="fc" id="L29"> return Set.copyOf(authorities);</span>
}
private static List&lt;String&gt; realmRoles(Jwt jwt) {
<span class="fc" id="L33"> Map&lt;String, Object&gt; realmAccess = jwt.getClaim(CLAIM_REALM_ACCESS);</span>
<span class="pc bpc" id="L34" title="1 of 2 branches missed."> if (realmAccess == null) {</span>
<span class="nc" id="L35"> return List.of();</span>
}
<span class="fc" id="L38"> Object roles = realmAccess.get(CLAIM_ROLES);</span>
<span class="pc bpc" id="L39" title="1 of 2 branches missed."> if (!(roles instanceof Collection&lt;?&gt; roleValues)) {</span>
<span class="nc" id="L40"> return List.of();</span>
}
<span class="fc" id="L43"> return roleValues.stream()</span>
<span class="fc" id="L44"> .filter(String.class::isInstance)</span>
<span class="fc" id="L45"> .map(String.class::cast)</span>
<span class="pc bpc" id="L46" title="1 of 2 branches missed."> .filter(role -&gt; !role.isBlank())</span>
<span class="fc" id="L47"> .toList();</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.14.202510111229</span></div></body></html>