PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/camel-itest/src/test/java/org/apache/camel/itest/security/KeystorePasswordCallback.java

https://gitlab.com/matticala/apache-camel
Java | 112 lines | 75 code | 11 blank | 26 comment | 9 complexity | 71e5e02e01a240dd3e9970cf2314177e MD5 | raw file
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.camel.itest.security;
  18. import java.io.IOException;
  19. import java.lang.reflect.Method;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import javax.security.auth.callback.Callback;
  23. import javax.security.auth.callback.CallbackHandler;
  24. import javax.security.auth.callback.UnsupportedCallbackException;
  25. public class KeystorePasswordCallback implements CallbackHandler {
  26. private Map<String, String> passwords = new HashMap<>();
  27. public KeystorePasswordCallback() {
  28. passwords.put("alice", "password");
  29. passwords.put("jim", "jimspassword");
  30. passwords.put("bob", "bobspassword");
  31. }
  32. /**
  33. * It attempts to get the password from the private
  34. * alias/passwords map.
  35. */
  36. public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
  37. for (Callback callback : callbacks) {
  38. String pass = passwords.get(getIdentifier(callback));
  39. String type = getPasswordType(callback);
  40. if (type.endsWith("#PasswordDigest")) {
  41. if (pass != null) {
  42. setPassword(callback, pass);
  43. return;
  44. }
  45. }
  46. if (type.endsWith("#PasswordText")) {
  47. // Code for CXF 2.4.X
  48. if (getPassword(callback) == null) {
  49. setPassword(callback, pass);
  50. return;
  51. }
  52. }
  53. }
  54. }
  55. private void setPassword(Callback callback, String pass) {
  56. try {
  57. callback.getClass().getMethod("setPassword", String.class).invoke(callback, pass);
  58. } catch (Exception e) {
  59. throw new RuntimeException(e);
  60. }
  61. }
  62. private String getPassword(Callback callback) {
  63. try {
  64. return (String)callback.getClass().getMethod("getPassword").invoke(callback);
  65. } catch (Exception e) {
  66. throw new RuntimeException(e);
  67. }
  68. }
  69. private String getIdentifier(Callback cb) {
  70. try {
  71. return (String)cb.getClass().getMethod("getIdentifier").invoke(cb);
  72. } catch (Exception e) {
  73. throw new RuntimeException(e);
  74. }
  75. }
  76. /**
  77. * Add an alias/password pair to the callback mechanism.
  78. */
  79. public void setAliasPassword(String alias, String password) {
  80. passwords.put(alias, password);
  81. }
  82. private String getPasswordType(Callback pc) {
  83. try {
  84. Method getType = null;
  85. try {
  86. getType = pc.getClass().getMethod("getPasswordType");
  87. } catch (NoSuchMethodException ex) {
  88. // keep looking
  89. } catch (SecurityException ex) {
  90. // keep looking
  91. }
  92. if (getType == null) {
  93. getType = pc.getClass().getMethod("getType");
  94. }
  95. String result = (String)getType.invoke(pc);
  96. return result;
  97. } catch (Exception ex) {
  98. return null;
  99. }
  100. }
  101. }