View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.wss4j.stax.test;
20  
21  import java.io.IOException;
22  import java.security.cert.X509Certificate;
23  
24  import javax.security.auth.callback.Callback;
25  import javax.security.auth.callback.CallbackHandler;
26  import javax.security.auth.callback.UnsupportedCallbackException;
27  
28  import org.apache.wss4j.common.ext.WSPasswordCallback;
29  import org.apache.wss4j.common.saml.builder.SAML1Constants;
30  
31  public class CallbackHandlerImpl implements CallbackHandler {
32  
33      private String username = "default";
34      private byte[] secret;
35  
36      public enum Statement {
37          AUTHN, ATTR, AUTHZ
38      }
39  
40      private String subjectName = "uid=joe,ou=people,ou=saml-demo,o=example.com";
41      private String subjectQualifier = "www.example.com";
42      private String confirmationMethod = SAML1Constants.CONF_SENDER_VOUCHES;
43      private X509Certificate[] certs;
44      private byte[] ephemeralKey;
45      private String issuer;
46  
47      public CallbackHandlerImpl() {
48      }
49  
50      public CallbackHandlerImpl(String username) {
51          if (username != null) {
52              this.username = username;
53          }
54      }
55  
56      public CallbackHandlerImpl(byte[] secret) {
57          this.secret = secret;
58      }
59  
60      @Override
61      public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
62          if (callbacks[0] instanceof WSPasswordCallback) {
63              WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
64  
65              if (pc.getUsage() == WSPasswordCallback.DECRYPT
66                      || pc.getUsage() == WSPasswordCallback.SIGNATURE
67                      || pc.getUsage() == WSPasswordCallback.USERNAME_TOKEN
68                      ) {
69                  pc.setPassword(username);
70              } else if (pc.getUsage() == WSPasswordCallback.SECRET_KEY
71                      || pc.getUsage() == WSPasswordCallback.SECURITY_CONTEXT_TOKEN) {
72                  pc.setKey(secret);
73              } else if (pc.getUsage() == WSPasswordCallback.PASSWORD_ENCRYPTOR_PASSWORD) {
74                  pc.setPassword("this-is-a-secret");
75              } else {
76                  throw new UnsupportedCallbackException(pc, "Unrecognized CallbackHandlerImpl");
77              }
78          }
79      }
80  
81      public String getUsername() {
82          return username;
83      }
84  
85      public void setUsername(String username) {
86          this.username = username;
87      }
88  
89      public String getSubjectName() {
90          return subjectName;
91      }
92  
93      public void setSubjectName(String subjectName) {
94          this.subjectName = subjectName;
95      }
96  
97      public String getSubjectQualifier() {
98          return subjectQualifier;
99      }
100 
101     public void setSubjectQualifier(String subjectQualifier) {
102         this.subjectQualifier = subjectQualifier;
103     }
104 
105     public String getConfirmationMethod() {
106         return confirmationMethod;
107     }
108 
109     public void setConfirmationMethod(String confirmationMethod) {
110         this.confirmationMethod = confirmationMethod;
111     }
112 
113     public X509Certificate[] getCerts() {
114         return certs;
115     }
116 
117     public void setCerts(X509Certificate[] certs) {
118         this.certs = certs;
119     }
120 
121     public byte[] getEphemeralKey() {
122         return ephemeralKey;
123     }
124 
125     public void setEphemeralKey(byte[] ephemeralKey) {
126         this.ephemeralKey = ephemeralKey;
127     }
128 
129     public String getIssuer() {
130         return issuer;
131     }
132 
133     public void setIssuer(String issuer) {
134         this.issuer = issuer;
135     }
136 
137     public byte[] getSecret() {
138         return secret;
139     }
140 
141     public void setSecret(byte[] secret) {
142         this.secret = secret;
143     }
144 
145 }