Advertisement
Guest User

Untitled

a guest
Jun 4th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.23 KB | None | 0 0
  1. package sample;
  2.  
  3. import javax.security.auth.callback.Callback;
  4. import javax.security.auth.callback.CallbackHandler;
  5. import javax.security.auth.callback.NameCallback;
  6. import javax.security.auth.callback.PasswordCallback;
  7. import javax.security.auth.callback.UnsupportedCallbackException;
  8. import javax.security.sasl.AuthorizeCallback;
  9. import javax.security.sasl.Sasl;
  10. import javax.security.sasl.SaslClient;
  11. import javax.security.sasl.SaslException;
  12. import javax.security.sasl.SaslServer;
  13. import java.io.IOException;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16.  
  17. public class App {
  18.  
  19.     private SaslServer saslServer;
  20.  
  21.     public static void main(String[] args) throws SaslException {
  22.         new App().init().start();
  23.     }
  24.  
  25.     private App init() {
  26.         try {
  27.             saslServer = Sasl.createSaslServer("CRAM-MD5", "my_server", "local", null, new ServerHandler());
  28.         } catch (SaslException e) {
  29.             e.printStackTrace();
  30.         }
  31.         return this;
  32.     }
  33.  
  34.     private static class ClientHandler implements CallbackHandler {
  35.  
  36.         private String username;
  37.         private char[] password;
  38.  
  39.  
  40.         public void setUsername(String username) {
  41.             this.username = username;
  42.         }
  43.  
  44.         public void setPassword(char[] password) {
  45.             this.password = password;
  46.         }
  47.  
  48.         public void handle(Callback[] cbs) throws IOException, UnsupportedCallbackException {
  49.             for (Callback cb : cbs) {
  50.                 if (cb instanceof NameCallback) {
  51.  
  52.                     System.out.println("Client - NameCallback");
  53.  
  54.                     NameCallback nc = (NameCallback) cb;
  55.                     nc.setName(username);
  56.                 } else if (cb instanceof PasswordCallback) {
  57.  
  58.                     System.out.println("Client - PasswordCallback");
  59.  
  60.                     PasswordCallback pc = (PasswordCallback) cb;
  61.                     pc.setPassword(password);
  62.                 }
  63.             }
  64.         }
  65.     }
  66.  
  67.     private static class ServerHandler implements CallbackHandler {
  68.  
  69.         private Map<String,String> authorizedUsers = new HashMap<String,String>();
  70.  
  71.         {
  72.             authorizedUsers.put("energy", "mypass");
  73.             authorizedUsers.put("user", "userpass");
  74.         }
  75.  
  76.  
  77.         public void handle(Callback[] cbs) throws IOException, UnsupportedCallbackException {
  78.             String user = null;
  79.             for (Callback cb : cbs) {
  80.                 if (cb instanceof AuthorizeCallback) {
  81.  
  82.                     System.out.println("Server - AuthorizeCallback");
  83.  
  84.                     AuthorizeCallback ac = (AuthorizeCallback) cb;
  85.                     ac.setAuthorized(true);
  86.  
  87.                 } else if (cb instanceof NameCallback) {
  88.  
  89.                     System.out.println("Server - NameCallback");
  90.  
  91.                     NameCallback nc = (NameCallback) cb;
  92.                     if (authorizedUsers.containsKey(nc.getDefaultName())) {
  93.                         user = nc.getDefaultName();
  94.                         nc.setName(nc.getDefaultName());
  95.                     }
  96.                 } else if (cb instanceof PasswordCallback) {
  97.                     System.out.println("Server - PasswordCallback");
  98.  
  99.                     PasswordCallback pc = (PasswordCallback) cb;
  100.                     if (user != null) {
  101.                         pc.setPassword(authorizedUsers.get(user).toCharArray());
  102.                     }
  103.                 }
  104.             }
  105.         }
  106.     }
  107.  
  108.     private void start() throws SaslException {
  109.  
  110.         byte[] challenge;
  111.         byte[] response;
  112.  
  113.         ClientHandler clientHandler = new ClientHandler();
  114.  
  115.         clientHandler.setUsername("energy");
  116.         clientHandler.setPassword("mypass".toCharArray());
  117.         SaslClient sc = Sasl.createSaslClient(new String[]{"CRAM-MD5"}, null, "my_server", "local", null, clientHandler);
  118.  
  119.         challenge = saslServer.evaluateResponse(new byte[0]);
  120.         response = sc.evaluateChallenge(challenge);
  121.  
  122.         try {
  123.             saslServer.evaluateResponse(response);
  124.             if (saslServer.isComplete()) {
  125.  
  126.                 System.out.println("Authentication successful.");
  127.             }
  128.         } catch (SaslException e) {
  129.             e.printStackTrace();    
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement