Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.62 KB | None | 0 0
  1. //-- auth.config --
  2.  
  3. "Index" {
  4.     lc.MyLoginModule required;
  5. };
  6.  
  7. //-- security.policy --
  8.  
  9. grant {
  10.     permission java.util.PropertyPermission "idea.launcher.*", "read";
  11.     permission java.lang.RuntimePermission "loadLibrary.*";
  12.     permission javax.security.auth.AuthPermission "doAsPrivileged";
  13.     permission javax.security.auth.AuthPermission "createLoginContext";
  14.     permission javax.security.auth.AuthPermission "modifyPrincipals";
  15.  
  16. };
  17.  
  18. grant principal lc.UserPrincipal "alice" {
  19.     permission java.io.FilePermission "/tmp/tempfile", "read,write";
  20. };
  21. grant principal lc.UserPrincipal "bob" {
  22.     permission java.io.FilePermission "/tmp/tempfile", "read";
  23. };
  24.  
  25.  
  26. //------------- App.java ---------------------------
  27.  
  28. package lc;
  29.  
  30. import javax.security.auth.Subject;
  31. import javax.security.auth.callback.Callback;
  32. import javax.security.auth.callback.CallbackHandler;
  33. import javax.security.auth.callback.NameCallback;
  34. import javax.security.auth.callback.PasswordCallback;
  35. import javax.security.auth.callback.UnsupportedCallbackException;
  36. import javax.security.auth.login.LoginContext;
  37. import javax.security.auth.login.LoginException;
  38. import java.io.*;
  39. import java.security.PrivilegedAction;
  40. import java.util.logging.Level;
  41. import java.util.logging.Logger;
  42.  
  43. public class App {
  44.     private static final Logger logger = Logger.getLogger(App.class.getName());
  45.  
  46.     public static void main(String[] args) throws LoginException {
  47.         LoginContext context = new LoginContext("Index", new CallbackHandler() {
  48.  
  49.             public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
  50.                 for (Callback callback : callbacks) {
  51.                     if (callback instanceof NameCallback) {
  52.                         NameCallback nc = (NameCallback) callback;
  53.                         System.err.print(nc.getPrompt());
  54.                         System.err.flush();
  55.                         nc.setName((new BufferedReader(new InputStreamReader(System.in))).readLine());
  56.                     } else if (callback instanceof PasswordCallback) {
  57.                         PasswordCallback pc = ((PasswordCallback) callback);
  58.                         System.err.print(pc.getPrompt());
  59.                         System.err.flush();
  60.                         pc.setPassword((new BufferedReader(new InputStreamReader(System.in))).readLine().toCharArray());
  61.                     }
  62.                 }
  63.             }
  64.         });
  65.         context.login();
  66.  
  67.         Subject.doAsPrivileged(context.getSubject(), new PrivilegedAction<Object>() {
  68.  
  69.             public Object run() {
  70.                 try {
  71.                     read();
  72.                 } catch (Exception e) {
  73.                     logger.log(Level.SEVERE, "Cannot read file", e);
  74.                 }
  75.                 try {
  76.                     write();
  77.                 } catch (Exception e) {
  78.                     logger.log(Level.SEVERE, "Cannot write file", e);
  79.                 }
  80.                 return new Object();
  81.             }
  82.  
  83.             private void read() {
  84.                 File file = new File("/tmp/tempfile");
  85.                 Reader reader = null;
  86.                 try {
  87.                     reader = new BufferedReader(new FileReader(file));
  88.  
  89.                     StringBuffer buffer = new StringBuffer();
  90.                     int c;
  91.                     while ((c = reader.read()) != -1) {
  92.                         buffer.appendCodePoint(c);
  93.                     }
  94.                     System.out.println(buffer);
  95.                 } catch (FileNotFoundException e) {
  96.                     logger.log(Level.SEVERE, null, e);
  97.                 } catch (IOException e) {
  98.                     logger.log(Level.SEVERE, null, e);
  99.                 } finally {
  100.                     if (reader != null) {
  101.                         try {
  102.                             reader.close();
  103.                         } catch (IOException e) {
  104.                             logger.log(Level.SEVERE, null, e);
  105.                         }
  106.                     }
  107.                 }
  108.             }
  109.  
  110.             private void write() {
  111.                 File file = new File("/tmp/tempfile");
  112.                 Writer writer = null;
  113.  
  114.                 try {
  115.                     writer = new BufferedWriter(new FileWriter(file));
  116.                     writer.write("Hello world");
  117.                 } catch (FileNotFoundException e) {
  118.                     logger.log(Level.SEVERE, null, e);
  119.                 } catch (IOException e) {
  120.                     logger.log(Level.SEVERE, null, e);
  121.                 } finally {
  122.                     if (writer != null) {
  123.                         try {
  124.                             writer.close();
  125.                         } catch (IOException e) {
  126.                             logger.log(Level.SEVERE, null, e);
  127.                         }
  128.                     }
  129.                 }
  130.  
  131.             }
  132.  
  133.         }, null);
  134.  
  135.         context.logout();
  136.     }
  137. }
  138.  
  139.  
  140. //-------------- MyLoginModule.java -------------------------
  141. package lc;
  142.  
  143. import javax.security.auth.Subject;
  144. import javax.security.auth.callback.Callback;
  145. import javax.security.auth.callback.CallbackHandler;
  146. import javax.security.auth.callback.NameCallback;
  147. import javax.security.auth.callback.PasswordCallback;
  148. import javax.security.auth.callback.UnsupportedCallbackException;
  149. import javax.security.auth.login.LoginException;
  150. import javax.security.auth.spi.LoginModule;
  151. import java.io.IOException;
  152. import java.util.Arrays;
  153. import java.util.HashMap;
  154. import java.util.Map;
  155. import java.util.logging.Level;
  156. import java.util.logging.Logger;
  157.  
  158. public class MyLoginModule implements LoginModule {
  159.     private static final Logger logger = Logger.getLogger(LoginModule.class.getName());
  160.     private Subject subject;
  161.     private CallbackHandler callbackHandler;
  162.     private Map<String, ?> sharedState;
  163.     private Map<String, ?> options;
  164.     private Map<String,char[]> users = new HashMap<String,char[]>();
  165.     private String login = null;
  166.     private char[] password = null;
  167.  
  168.     {
  169.         users.put("alice", "admin".toCharArray());
  170.         users.put("bob", "nimda".toCharArray());
  171.     }
  172.  
  173.     public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options) {
  174.         this.subject = subject;
  175.         this.callbackHandler = callbackHandler;
  176.         this.sharedState = sharedState;
  177.         this.options = options;
  178.     }
  179.  
  180.     public boolean login() throws LoginException {
  181.         String namePrompt = "Enter your name: ";
  182.         String passwordPrompt = "Enter your password: ";
  183.         Callback[] callbacks = new Callback[]{new NameCallback(namePrompt), new PasswordCallback(passwordPrompt, true)};
  184.         try {
  185.             callbackHandler.handle(callbacks);
  186.             for (Callback callback : callbacks) {
  187.                 if (callback instanceof NameCallback) {
  188.                     NameCallback nc = (NameCallback) callback;
  189.                     login = nc.getName();
  190.                 } else if (callback instanceof PasswordCallback) {
  191.                     PasswordCallback pc = ((PasswordCallback) callback);
  192.                     password = pc.getPassword();
  193.                 }
  194.             }
  195.             return true;
  196.         } catch (IOException e) {
  197.             logger.log(Level.SEVERE, null, e);
  198.         } catch (UnsupportedCallbackException e) {
  199.             logger.log(Level.SEVERE, null, e);
  200.         }
  201.         return false;
  202.     }
  203.  
  204.     public boolean commit() throws LoginException {
  205.         if (users.containsKey(login) && Arrays.equals(users.get(login), password)) {
  206.             subject.getPrincipals().clear();
  207.             subject.getPrincipals().add(new UserPrincipal(login));
  208.             this.login = null;
  209.             this.password = null;
  210.             return true;
  211.         }
  212.         return false;
  213.     }
  214.  
  215.     public boolean abort() throws LoginException {
  216.         return false;
  217.     }
  218.  
  219.     public boolean logout() throws LoginException {
  220.         if (subject.getPrincipals().isEmpty() == false) {
  221.             subject.getPrincipals().clear();
  222.             this.login = null;
  223.             this.password = null;
  224.             return true;
  225.         }
  226.         return false;
  227.     }
  228. }
  229.  
  230. //------------- UserPrincipal.java -----------------------
  231. package lc;
  232.  
  233. import java.security.Principal;
  234. import java.util.logging.Logger;
  235.  
  236. public class UserPrincipal implements Principal {
  237.  
  238.     private static final Logger logger = Logger.getLogger(UserPrincipal.class.getName());
  239.  
  240.     private String username;
  241.  
  242.     public UserPrincipal(String username) {
  243.         this.username = username;
  244.     }
  245.  
  246.     public String getName() {
  247.         return username;
  248.     }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement