working on oidc auth
This commit is contained in:
81
src/main/java/de/srsoftware/widerhall/Configuration.java
Normal file
81
src/main/java/de/srsoftware/widerhall/Configuration.java
Normal file
@@ -0,0 +1,81 @@
|
||||
package de.srsoftware.widerhall;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import static de.srsoftware.widerhall.Constants.*;
|
||||
public class Configuration {
|
||||
private static Configuration singleton = null;
|
||||
private JSONObject data;
|
||||
private final File file;
|
||||
|
||||
public Configuration(File configFile) throws IOException, ParseException {
|
||||
this.file = configFile;
|
||||
if (!configFile.exists()){
|
||||
setDefaults();
|
||||
save();
|
||||
}
|
||||
var content = Files.readString(configFile.toPath());
|
||||
data = (JSONObject) new JSONParser().parse(content);
|
||||
}
|
||||
|
||||
public static Configuration setFile(File file) throws IOException, ParseException {
|
||||
singleton = new Configuration(file);
|
||||
return singleton;
|
||||
}
|
||||
|
||||
public static Configuration instance() {
|
||||
return singleton;
|
||||
}
|
||||
|
||||
private void setDefaults() throws MalformedURLException {
|
||||
if (data == null) data = new JSONObject();
|
||||
serverPort();
|
||||
tokenUrl();
|
||||
loginUrl();
|
||||
baseUrl();
|
||||
clientId();
|
||||
clientSecret();
|
||||
}
|
||||
|
||||
private void save() throws IOException {
|
||||
Files.writeString(file.toPath(),data.toJSONString());
|
||||
}
|
||||
|
||||
public int serverPort() {
|
||||
if (!data.containsKey(PORT)) data.put(PORT,80L);
|
||||
var o = data.get(PORT);
|
||||
return (int) (long) o;
|
||||
}
|
||||
|
||||
public URL tokenUrl() throws MalformedURLException {
|
||||
if (!data.containsKey(TOKEN_URL)) data.put(TOKEN_URL,"http://localhost:"+serverPort()+"/oauth/token");
|
||||
return new URL((String) data.get(TOKEN_URL));
|
||||
}
|
||||
|
||||
public String loginUrl() {
|
||||
if (!data.containsKey(LOGIN_URL)) data.put(LOGIN_URL,"http://localhost:"+serverPort()+"/oauth/login");
|
||||
return (String) data.get(LOGIN_URL);
|
||||
}
|
||||
|
||||
public String baseUrl() {
|
||||
if (!data.containsKey(BASE_URL)) data.put(BASE_URL,"http://localhost");
|
||||
return (String) data.get(BASE_URL);
|
||||
}
|
||||
|
||||
public String clientId() {
|
||||
if (!data.containsKey(Constants.CLIENT_ID)) data.put(CLIENT_ID,"widerhall");
|
||||
return (String) data.get(CLIENT_ID);
|
||||
}
|
||||
|
||||
public Object clientSecret() {
|
||||
if (!data.containsKey(Constants.CLIENT_SECRET)) data.put(CLIENT_SECRET,"changeme");
|
||||
return (String) data.get(CLIENT_SECRET);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user