blob: 088016d8506be464943071b7daaeb0ef6ae415cc [file] [log] [blame]
<?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>ShiroSecurityContext.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">Apache Shiro :: Test Coverage</a> &gt; <a href="../index.html" class="el_bundle">shiro-jaxrs</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.web.jaxrs</a> &gt; <span class="el_source">ShiroSecurityContext.java</span></div><h1>ShiroSecurityContext.java</h1><pre class="source lang-java linenums">/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* &quot;License&quot;); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.shiro.web.jaxrs;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.core.SecurityContext;
import java.security.Principal;
/**
* A Shiro based {@link SecurityContext} that exposes the current Shiro {@link Subject} as a {@link Principal}.
* The {@link #isUserInRole(String)} method returns the result of {@link Subject#hasRole(String)}.
*
* @since 1.4
*/
public class ShiroSecurityContext implements SecurityContext {
final private ContainerRequestContext containerRequestContext;
final private SecurityContext originalSecurityContext;
<span class="fc" id="L40"> public ShiroSecurityContext(ContainerRequestContext containerRequestContext) {</span>
<span class="fc" id="L41"> this.containerRequestContext = containerRequestContext;</span>
<span class="fc" id="L42"> this.originalSecurityContext = containerRequestContext.getSecurityContext();</span>
<span class="fc" id="L43"> }</span>
@Override
public Principal getUserPrincipal() {
Principal result;
<span class="fc" id="L50"> Subject subject = getSubject();</span>
<span class="fc" id="L51"> PrincipalCollection shiroPrincipals = subject.getPrincipals();</span>
<span class="fc bfc" id="L52" title="All 2 branches covered."> if (shiroPrincipals != null) {</span>
<span class="fc" id="L53"> result = shiroPrincipals.oneByType(Principal.class);</span>
<span class="fc bfc" id="L55" title="All 2 branches covered."> if (result == null) {</span>
<span class="fc" id="L56"> result = new ObjectPrincipal(shiroPrincipals.getPrimaryPrincipal());</span>
}
}
else {
<span class="fc" id="L60"> result = originalSecurityContext.getUserPrincipal();</span>
}
<span class="fc" id="L63"> return result;</span>
}
@Override
public boolean isUserInRole(String role) {
<span class="fc" id="L68"> return getSubject().hasRole(role);</span>
}
@Override
public boolean isSecure() {
<span class="fc" id="L73"> return containerRequestContext.getSecurityContext().isSecure();</span>
}
@Override
public String getAuthenticationScheme() {
<span class="fc" id="L78"> return containerRequestContext.getSecurityContext().getAuthenticationScheme();</span>
}
private Subject getSubject() {
<span class="fc" id="L82"> return SecurityUtils.getSubject();</span>
}
/**
* Java Principal wrapper around any Shiro Principal object.s
*/
private class ObjectPrincipal implements Principal {
<span class="fc" id="L90"> private Object object = null;</span>
<span class="fc" id="L92"> public ObjectPrincipal(Object object) {</span>
<span class="fc" id="L93"> this.object = object;</span>
<span class="fc" id="L94"> }</span>
public Object getObject() {
<span class="fc" id="L97"> return object;</span>
}
public String getName() {
<span class="fc" id="L101"> return getObject().toString();</span>
}
@Override
public boolean equals(Object o) {
<span class="pc bpc" id="L106" title="1 of 2 branches missed."> if (this == o) return true;</span>
<span class="pc bpc" id="L107" title="2 of 4 branches missed."> if (o == null || getClass() != o.getClass()) return false;</span>
<span class="fc" id="L109"> ObjectPrincipal that = (ObjectPrincipal) o;</span>
<span class="fc" id="L111"> return object.equals(that.object);</span>
}
public int hashCode() {
<span class="nc" id="L116"> return object.hashCode();</span>
}
public String toString() {
<span class="nc" id="L120"> return object.toString();</span>
}
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.3.201901230119</span></div></body></html>