blob: 269b72ac4d63e3e706ef7eef888ea82ae083a97a [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="../.resources/report.css" type="text/css"/><link rel="shortcut icon" href="../.resources/report.gif" type="image/gif"/><title>TokenCallbackHandler.java</title><link rel="stylesheet" href="../.resources/prettify.css" type="text/css"/><script type="text/javascript" src="../.resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="right"><a href="../.sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Coverage Report</a> &gt; <a href="index.html" class="el_package">org.apache.rampart</a> &gt; <span class="el_source">TokenCallbackHandler.java</span></div><h1>TokenCallbackHandler.java</h1><pre class="source lang-java linenums">/*
* Copyright 2004,2005 The Apache Software Foundation.
*
* Licensed 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.rampart;
import org.apache.rahas.EncryptedKeyToken;
import org.apache.rahas.Token;
import org.apache.rahas.TokenStorage;
import org.apache.rahas.TrustException;
import org.apache.ws.security.WSPasswordCallback;
import org.w3c.dom.Element;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import java.io.IOException;
public class TokenCallbackHandler implements CallbackHandler {
private TokenStorage store;
private CallbackHandler handler;
private String tokenIdentifier;
<span class="fc" id="L39"> public TokenCallbackHandler(TokenStorage store, CallbackHandler handler) {</span>
<span class="fc" id="L40"> this.store = store;</span>
<span class="fc" id="L41"> this.handler = handler;</span>
<span class="fc" id="L42"> this.tokenIdentifier = null;</span>
<span class="fc" id="L43"> }</span>
public void handle(Callback[] callbacks)
throws IOException, UnsupportedCallbackException {
<span class="fc bfc" id="L50" title="All 2 branches covered."> for (int i = 0; i &lt; callbacks.length; i++) {</span>
<span class="pc bpc" id="L52" title="1 of 2 branches missed."> if (callbacks[i] instanceof WSPasswordCallback) {</span>
<span class="fc" id="L53"> WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];</span>
<span class="fc" id="L54"> String id = pc.getIdentifier();</span>
<span class="pc bpc" id="L56" title="2 of 6 branches missed."> if((pc.getUsage() == WSPasswordCallback.SECURITY_CONTEXT_TOKEN || </span>
pc.getUsage() == WSPasswordCallback.CUSTOM_TOKEN) &amp;&amp;
this.store != null) {
Token tok;
try {
//Pick up the token from the token store
<span class="fc" id="L62"> tok = this.store.getToken(id);</span>
<span class="pc bpc" id="L63" title="1 of 2 branches missed."> if(tok != null) {</span>
//Get the secret and set it in the callback object
<span class="fc" id="L65"> pc.setKey(tok.getSecret());</span>
<span class="fc" id="L66"> pc.setCustomToken((Element)tok.getToken());</span>
}
<span class="nc" id="L68"> } catch (Exception e) {</span>
<span class="nc" id="L69"> e.printStackTrace();</span>
<span class="nc" id="L70"> throw new IOException(e.getMessage());</span>
<span class="fc" id="L71"> }</span>
<span class="fc bfc" id="L72" title="All 2 branches covered."> } else if (pc.getUsage() == WSPasswordCallback.SECRET_KEY){</span>
try {
<span class="fc" id="L75"> String[] tokenIdentifiers = this.store.getTokenIdentifiers();</span>
Token tok;
<span class="fc bfc" id="L78" title="All 2 branches covered."> for (int j = 0 ; j &lt; tokenIdentifiers.length ; j++) {</span>
<span class="fc" id="L80"> tok = this.store.getToken(tokenIdentifiers[j]);</span>
<span class="fc bfc" id="L82" title="All 4 branches covered."> if (tok instanceof EncryptedKeyToken &amp;&amp;</span>
((EncryptedKeyToken)tok).getSHA1().equals(id)){
<span class="fc" id="L84"> pc.setKey(tok.getSecret());</span>
<span class="fc" id="L85"> pc.setCustomToken((Element)tok.getToken());</span>
<span class="fc" id="L87"> tokenIdentifier = tokenIdentifiers[j];</span>
}
}
<span class="nc" id="L91"> } catch (TrustException e) {</span>
<span class="nc" id="L92"> e.printStackTrace();</span>
<span class="nc" id="L93"> throw new IOException(e.getMessage());</span>
<span class="fc" id="L94"> }</span>
} else {
//Handle other types of callbacks with the usual handler
<span class="pc bpc" id="L97" title="1 of 2 branches missed."> if(this.handler != null) {</span>
<span class="fc" id="L98"> handler.handle(new Callback[]{pc});</span>
}
}
<span class="fc" id="L102"> } else {</span>
<span class="nc" id="L103"> throw new UnsupportedCallbackException(callbacks[i],</span>
&quot;Unrecognized Callback&quot;);
}
}
<span class="fc" id="L107"> }</span>
public void removeEncryptedToken() {
<span class="fc bfc" id="L111" title="All 2 branches covered."> if (tokenIdentifier != null) {</span>
<span class="fc" id="L112"> this.store.removeToken(tokenIdentifier);</span>
}
<span class="fc" id="L115"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.6.1.201212231917</span></div></body></html>