goauth

Fabric plugin for enhanced whitelists
git clone git://git.bain.cz/goauth.git
Log | Files | Refs | README | LICENSE

GoauthConfig.java (2357B)


      1 package cz.bain.plugins.goauth;
      2 
      3 import java.io.*;
      4 import java.util.ArrayList;
      5 import java.util.Arrays;
      6 import java.util.HashMap;
      7 import java.util.List;
      8 
      9 public class GoauthConfig {
     10 
     11     private final HashMap<String, String> options = new HashMap<>();
     12 
     13     public GoauthConfig(File file) throws IOException {
     14 
     15         if (!file.exists()) {
     16             try (FileWriter writer = new FileWriter(file)) {
     17                 writer.write("""
     18                         port=8001
     19                         key=<32-byte-key>
     20                         auth-link-text=Authenticate
     21 
     22                         # optional
     23                         # auth-link=https://bain.cz/
     24                                                 
     25                         # Shown as: "<welcome-text> <auth-link-text>", with <auth-link-text> being a link to
     26                         # <auth-link>?username=$username
     27                         # welcome-text=Welcome! Please authenticate:
     28                                                 
     29                         # shown after the chat message disappears
     30                         # reminder-text=Please authenticate in chat
     31                         """);
     32             }
     33             throw new IOException("goauth.properties didn't exist, please review its contents and restart the server");
     34         }
     35 
     36         BufferedReader reader = new BufferedReader(new FileReader(file));
     37         List<String> required = new ArrayList<>(Arrays.asList("port", "key", "auth-link-text"));
     38         String line;
     39         int line_no = 0;
     40         while ((line = reader.readLine()) != null) {
     41             line_no++;
     42             if (line.startsWith("#") || line.length() == 0) continue;
     43             String[] parsed = line.split("=");
     44             if (parsed.length != 2) {
     45                 reader.close();
     46                 throw new IOException("Could not parse config! Error on line " + line_no);
     47             }
     48             required.remove(parsed[0]);
     49             options.put(parsed[0], parsed[1]);
     50         }
     51         reader.close();
     52 
     53         if (!required.isEmpty()) {
     54             throw new IOException("Missing configuration options: " + Arrays.toString(required.toArray()));
     55         }
     56     }
     57 
     58     public String get(String key, String default_) {
     59         return options.getOrDefault(key, default_);
     60     }
     61 
     62     public String getRequired(String key) {
     63         return options.get(key);
     64     }
     65 }