Stephan Richter
3 years ago
13 changed files with 371 additions and 19 deletions
@ -0,0 +1,81 @@
@@ -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); |
||||
} |
||||
} |
@ -1,4 +1,4 @@
@@ -1,4 +1,4 @@
|
||||
package de.srsoftware.widerhall; |
||||
package de.srsoftware.widerhall.mail; |
||||
|
||||
import org.json.simple.JSONObject; |
||||
import org.slf4j.Logger; |
@ -1,4 +1,4 @@
@@ -1,4 +1,4 @@
|
||||
package de.srsoftware.widerhall; |
||||
package de.srsoftware.widerhall.mail; |
||||
|
||||
import javax.mail.Message; |
||||
import javax.mail.MessagingException; |
@ -1,18 +1,13 @@
@@ -1,18 +1,13 @@
|
||||
package de.srsoftware.widerhall; |
||||
package de.srsoftware.widerhall.mail; |
||||
|
||||
import org.json.simple.JSONObject; |
||||
import org.json.simple.parser.JSONParser; |
||||
import org.json.simple.parser.ParseException; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import javax.mail.*; |
||||
import javax.mail.internet.InternetAddress; |
||||
import javax.mail.internet.MimeMessage; |
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.io.UnsupportedEncodingException; |
||||
import java.nio.file.Files; |
||||
import java.util.Date; |
||||
import java.util.Map; |
||||
import java.util.Properties; |
@ -0,0 +1,43 @@
@@ -0,0 +1,43 @@
|
||||
package de.srsoftware.widerhall.web; |
||||
|
||||
import de.srsoftware.tools.Tag; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import javax.servlet.ServletException; |
||||
import javax.servlet.http.HttpServlet; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.IOException; |
||||
|
||||
public class Index extends HttpServlet { |
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Index.class); |
||||
|
||||
@Override |
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
||||
resp.setContentType("text/html"); |
||||
resp.setStatus(HttpServletResponse.SC_OK); |
||||
String auth = req.getHeader("Authorization"); |
||||
if (auth == null) { |
||||
resp.sendRedirect("login"); |
||||
return; |
||||
} |
||||
LOG.debug("Authorization: {}",auth); |
||||
|
||||
resp.getWriter().println(page(auth)); |
||||
|
||||
} |
||||
|
||||
private Tag head() { |
||||
return new Tag("meta") |
||||
.attr("charset","utf-8") |
||||
.addTo(new Tag("head")); |
||||
|
||||
} |
||||
|
||||
private Tag page(String auth) { |
||||
var body = new Tag("body").content(auth); |
||||
return body.addTo(head().addTo(new Tag("html"))); |
||||
} |
||||
} |
@ -0,0 +1,127 @@
@@ -0,0 +1,127 @@
|
||||
package de.srsoftware.widerhall.web; |
||||
|
||||
import de.srsoftware.widerhall.Configuration; |
||||
import de.srsoftware.widerhall.web.tags.Page; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import javax.net.ssl.HttpsURLConnection; |
||||
import javax.servlet.http.HttpServlet; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.BufferedReader; |
||||
import java.io.DataOutputStream; |
||||
import java.io.IOException; |
||||
import java.io.InputStreamReader; |
||||
import java.net.URLEncoder; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.Map; |
||||
import java.util.stream.Collectors; |
||||
|
||||
public class Login extends HttpServlet { |
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Login.class); |
||||
private final Configuration config; |
||||
|
||||
public Login(){ |
||||
this.config = Configuration.instance(); |
||||
LOG.debug("Creating new instance of Login.class"); |
||||
} |
||||
|
||||
@Override |
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
||||
var error = req.getParameter("error"); |
||||
if (error != null){ |
||||
var description = req.getParameter("error_description"); |
||||
sendError(resp,error+": "+description); |
||||
return; |
||||
} |
||||
LOG.debug("params: {}",req.getParameterMap()); |
||||
var code = req.getParameter("code"); |
||||
if (code != null){ |
||||
getTokenFor(code,resp); |
||||
resp.getWriter().println(new Page("rceived code: "+code)); |
||||
return; |
||||
} |
||||
resp.sendRedirect(loginUrl()); |
||||
} |
||||
|
||||
private static String urlEncode(Map<String, Object> data) { |
||||
String params = data.entrySet() |
||||
.stream() |
||||
.map(entry -> encode(entry.getKey()) + "=" + encode(entry.getValue())) |
||||
.collect(Collectors.joining("&")); |
||||
return params; |
||||
} |
||||
|
||||
private static String encode(Object value) { |
||||
return URLEncoder.encode(value.toString(),StandardCharsets.UTF_8); |
||||
} |
||||
|
||||
private void getTokenFor(String code, HttpServletResponse resp) throws IOException { |
||||
var url = config.tokenUrl(); |
||||
LOG.debug("Sending 'POST' request to URL '{}'",url); |
||||
HttpsURLConnection httpClient = (HttpsURLConnection) url.openConnection(); |
||||
|
||||
//add reuqest header
|
||||
httpClient.setRequestMethod("POST"); |
||||
httpClient.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded"); |
||||
httpClient.setRequestProperty( "Accept", "*/*" ); |
||||
//httpClient.setRequestProperty("User-Agent", "Mozilla/5.0");
|
||||
//httpClient.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
|
||||
|
||||
String urlParameters = urlEncode(Map.of( |
||||
"code",code, |
||||
"client_id",config.clientId(), |
||||
"client_secret",config.clientSecret(), |
||||
"grant_type","authorization_code")); |
||||
|
||||
LOG.debug("Posting parameters '{}'",urlParameters); |
||||
|
||||
// Send post request
|
||||
httpClient.setDoOutput(true); |
||||
httpClient.setDoInput(true); |
||||
try (DataOutputStream wr = new DataOutputStream(httpClient.getOutputStream())) { |
||||
wr.writeBytes(urlParameters); |
||||
wr.flush(); |
||||
} |
||||
|
||||
int responseCode = httpClient.getResponseCode(); |
||||
LOG.debug("Response Code: {}",responseCode); |
||||
|
||||
try (BufferedReader in = new BufferedReader(new InputStreamReader(httpClient.getInputStream()))) { |
||||
|
||||
String line; |
||||
StringBuilder response = new StringBuilder(); |
||||
|
||||
while ((line = in.readLine()) != null) { |
||||
response.append(line); |
||||
} |
||||
|
||||
//print result
|
||||
//System.out.println(response.toString());
|
||||
resp.getWriter().println(response); |
||||
|
||||
} |
||||
} |
||||
|
||||
private void sendError(HttpServletResponse resp, String error) throws IOException { |
||||
LOG.debug("error: {}",error); |
||||
resp.sendError(HttpServletResponse.SC_BAD_REQUEST,error); |
||||
} |
||||
|
||||
private String loginUrl() { |
||||
return config.loginUrl()+"?"+urlEncode(Map.of( |
||||
"response_type","code", |
||||
"client_id",config.clientId(), |
||||
"state",123456, |
||||
"redirect_uri",redirectUri(), |
||||
"scope","openid" |
||||
)); |
||||
} |
||||
|
||||
private String redirectUri() { |
||||
int port = config.serverPort(); |
||||
return config.baseUrl()+(port == 80 ? "" : ":"+port)+"/login"; |
||||
} |
||||
} |
@ -0,0 +1,21 @@
@@ -0,0 +1,21 @@
|
||||
package de.srsoftware.widerhall.web; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import javax.servlet.ServletException; |
||||
import javax.servlet.http.HttpServlet; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.IOException; |
||||
|
||||
public class Rest extends HttpServlet { |
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Rest.class); |
||||
@Override |
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
||||
String method = req.getMethod(); |
||||
LOG.debug("GET {}"+method); |
||||
|
||||
} |
||||
} |
@ -0,0 +1,10 @@
@@ -0,0 +1,10 @@
|
||||
package de.srsoftware.widerhall.web.tags; |
||||
|
||||
import de.srsoftware.tools.Tag; |
||||
|
||||
public class Header extends Tag { |
||||
public Header() { |
||||
super("head"); |
||||
new Tag("meta").attr("charset","utf-8").addTo(this); |
||||
} |
||||
} |
@ -0,0 +1,11 @@
@@ -0,0 +1,11 @@
|
||||
package de.srsoftware.widerhall.web.tags; |
||||
|
||||
import de.srsoftware.tools.Tag; |
||||
|
||||
public class Page extends Tag { |
||||
public Page(String content) { |
||||
super("html"); |
||||
new Header().addTo(this); |
||||
new Tag("body").content(content).addTo(this); |
||||
} |
||||
} |
Loading…
Reference in new issue