11 changed files with 250 additions and 45 deletions
@ -0,0 +1,88 @@
@@ -0,0 +1,88 @@
|
||||
/* © SRSoftware 2025 */ |
||||
package de.srsoftware.umbrella.core; |
||||
|
||||
import static de.srsoftware.tools.MimeType.MIME_FORM_URL; |
||||
import static de.srsoftware.tools.MimeType.MIME_JSON; |
||||
import static de.srsoftware.umbrella.core.Constants.*; |
||||
import static java.lang.System.Logger.Level.*; |
||||
import static java.lang.System.Logger.Level.WARNING; |
||||
import static java.nio.charset.StandardCharsets.UTF_8; |
||||
|
||||
import de.srsoftware.tools.Query; |
||||
import java.io.ByteArrayOutputStream; |
||||
import java.io.IOException; |
||||
import java.net.HttpURLConnection; |
||||
import java.net.URI; |
||||
import java.net.URL; |
||||
import java.util.Map; |
||||
import org.json.JSONObject; |
||||
|
||||
public class Util { |
||||
public static final System.Logger LOG = System.getLogger("Util"); |
||||
private Util(){} |
||||
|
||||
public static HttpURLConnection open(URL url) throws IOException { |
||||
var conn = (HttpURLConnection) url.openConnection(); |
||||
conn.setRequestProperty("Accept","*/*"); |
||||
conn.setRequestProperty("Host",url.getHost()); |
||||
conn.setRequestProperty("User-Agent","Umbrella/0.1"); |
||||
return conn; |
||||
} |
||||
|
||||
public static Object request(String location, Map<String,?> data, String postMime, String auth) throws UmbrellaException { |
||||
URL url; |
||||
try { |
||||
url = new URI(location).toURL(); |
||||
} catch (Exception e) { |
||||
LOG.log(WARNING,"{0} is not a valid url",location,e); |
||||
throw new UmbrellaException(500,"{0} is not a valid url",location).causedBy(e); |
||||
} |
||||
return request(url,data,postMime,auth); |
||||
} |
||||
|
||||
public static Object request(URL target, Map<String,?> data, String postMime, String auth) throws UmbrellaException { |
||||
String query = null; |
||||
if (data != null) { |
||||
query = switch (postMime){ |
||||
case MIME_FORM_URL -> Query.encode(data).orElse(null); |
||||
case null, default -> { |
||||
postMime = MIME_JSON; |
||||
yield new JSONObject(data).toString(); |
||||
} |
||||
}; |
||||
} |
||||
var method = query == null ? GET : POST; |
||||
try { |
||||
LOG.log(DEBUG,"sending {0} request ({1}) to {2}",method,postMime == null ? "empty" : postMime,target); |
||||
LOG.log(TRACE,"postData = {0}",query); |
||||
var conn = open(target); |
||||
conn.setRequestMethod(method); |
||||
conn.setRequestProperty(CONTENT_TYPE, postMime); |
||||
if (auth != null) conn.setRequestProperty(AUTHORIZATION,auth); |
||||
if (query != null) { |
||||
conn.setDoOutput(true); |
||||
var out = conn.getOutputStream(); |
||||
out.write(query.getBytes(UTF_8)); |
||||
out.flush(); |
||||
out.close(); |
||||
} |
||||
var bos = new ByteArrayOutputStream(); |
||||
if (conn.getResponseCode() == 200) { |
||||
var is = conn.getInputStream(); |
||||
is.transferTo(bos); |
||||
is.close(); |
||||
var content = bos.toString(UTF_8); |
||||
if (content.startsWith("{")) return new JSONObject(content); |
||||
return content; |
||||
} else { |
||||
var is = conn.getErrorStream(); |
||||
is.transferTo(bos); |
||||
is.close(); |
||||
throw new UmbrellaException(500,bos.toString(UTF_8)); |
||||
} |
||||
} catch (Exception e) { |
||||
LOG.log(WARNING,"Request to {0} failed: {1}",target,e.getMessage()); |
||||
throw new UmbrellaException(500,"Request to {0} failed!",target).causedBy(e); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,33 @@
@@ -0,0 +1,33 @@
|
||||
<script> |
||||
import { onMount } from 'svelte'; |
||||
import { t } from '../../translations.svelte.js'; |
||||
import { useTinyRouter } from 'svelte-tiny-router'; |
||||
import { checkUser } from '../../user.svelte.js'; |
||||
|
||||
const router = useTinyRouter(); |
||||
|
||||
let message = $state(t('user.processing_code')); |
||||
onMount(async () => { |
||||
let params = new URLSearchParams(location.search); |
||||
|
||||
if (params.get('code')){ |
||||
const url = `${location.protocol}//${location.host.replace('5173','8080')}/api/user/oidc/token`; |
||||
const resp = await fetch(url,{ |
||||
method : 'POST', |
||||
body: JSON.stringify(Object.fromEntries(params)), |
||||
credentials: 'include' |
||||
}); |
||||
if (resp.ok){ |
||||
let json = await resp.json(); |
||||
const redirect = json.redirect ? json.redirect : '/user'; |
||||
checkUser(); |
||||
router.navigate(redirect); |
||||
} else { |
||||
message = await resp.text(); |
||||
if (!message) message = t(resp); |
||||
} |
||||
} |
||||
}); |
||||
</script> |
||||
|
||||
{message} |
||||
Loading…
Reference in new issue