Advertisement
Guest User

Untitled

a guest
Jan 13th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. package davidhorak.authentication;
  2.  
  3. import davidhorak.Config;
  4.  
  5. import java.io.*;
  6. import java.util.ArrayList;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10. /**
  11.  * Trida pro prihlasovani uzivatele
  12.  */
  13. public class Authenticator {
  14.     private static ThreadLocal<Authenticator> instance = new ThreadLocal<>();
  15.  
  16.     public static Authenticator getInstance() {
  17.         if(instance.get() == null){
  18.             instance.set(new Authenticator());
  19.         }
  20.  
  21.         return instance.get();
  22.     }
  23.  
  24.     private String USER_FILE_URL = Config.getInstance().getUserFileUrl();
  25.     private ArrayList<User> users = new ArrayList<>();
  26.     private User currentUser = null;
  27.  
  28.     /**
  29.      * Vrati aktualne prihlaseneho uzivatele
  30.      */
  31.     public User getCurrentUser() {
  32.         return currentUser;
  33.     }
  34.  
  35.     private Authenticator(){ }
  36.  
  37.     /**
  38.      * Nacte uzivatele ze souboru
  39.      * @throws IOException
  40.      */
  41.     public void load() throws IOException {
  42.         FileInputStream file = new FileInputStream(USER_FILE_URL);
  43.         BufferedReader reader = new BufferedReader(new InputStreamReader(file));
  44.  
  45.         String line;
  46.         while((line = reader.readLine()) != null){
  47.             Pattern p = Pattern.compile("^(\\w+):(\\w+)$");
  48.             Matcher matcher = p.matcher(line);
  49.  
  50.             if(matcher.find()){
  51.                 String name = matcher.group(1);
  52.                 String password = matcher.group(2);
  53.  
  54.                 users.add(new User(name, password));
  55.             }
  56.         }
  57.  
  58.         reader.close();
  59.         file.close();
  60.     }
  61.  
  62.     /**
  63.      * Zapta se uzivatele na jmeno a heslo.
  64.      * Pokud se data shoduji prihlasi uzivatele, pokud ne zeptase uzivatele znovu
  65.      * @param inputStream InputStream ze socketu pro pro komunikaci s klientem
  66.      * @param outputStream OutputStream ze socketu pro pro komunikaci s klientem
  67.      * @throws IOException
  68.      */
  69.     public void authenticate(InputStream inputStream, OutputStream outputStream) throws IOException{
  70.         PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream), true);
  71.         BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  72.  
  73.         while(currentUser == null){
  74.             writer.println("Jmeno: ");
  75.             String name = reader.readLine();
  76.  
  77.             writer.println("Heslo: ");
  78.             String password = reader.readLine();
  79.  
  80.             for(User user : users){
  81.                 if(user.match(name, password)){
  82.                     currentUser = user;
  83.                     writer.println("Uspesne prihlasen");
  84.                     return;
  85.                 }
  86.             }
  87.  
  88.             writer.println("Nespravne jmeno nebo heslo");
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement