Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement oidc #628

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
604fa65
Implement oidc
Jeidnx Jun 18, 2023
375ee58
Better Error handling for oidc config
Jeidnx Jun 19, 2023
143711c
Save redirect in state
Jeidnx Jun 19, 2023
18d9317
Show warning message before oidc login
Jeidnx Jun 19, 2023
f4b9dff
Only show warning when not redirecting to configured frontend
Jeidnx Jun 27, 2023
53d9b9d
Merge branch 'master' into oidc
Jeidnx Jul 4, 2023
97889f3
Merge remote-tracking branch 'origin/master' into oidc
FireMasterK Aug 5, 2023
847f80c
Simplify config handling.
FireMasterK Aug 5, 2023
946ac45
Add missing newline.
FireMasterK Aug 5, 2023
0eb2351
Format all code.
FireMasterK Aug 5, 2023
9b7246a
Merge branch 'master' into oidc
Jeidnx Oct 24, 2023
e7f2187
Implement account deletion and cleanup some code
Jeidnx Oct 25, 2023
c1fde37
Refactor oidc logic into UserHandlers
Jeidnx Oct 26, 2023
024435f
Add database migration for username length change.
FireMasterK Oct 26, 2023
470efd8
Revert "Add database migration for username length change."
Jeidnx Oct 29, 2023
5f6a83a
Add code from the meeting.
FireMasterK Oct 29, 2023
868103c
Merge branch 'master' into oidc
Jeidnx Nov 6, 2024
074e4bc
chore: properly implement oidc
Jeidnx Nov 12, 2024
580eb7f
Simplify oidc hash generation.
FireMasterK Nov 17, 2024
9520a3c
Simplify error handling code a little.
FireMasterK Nov 17, 2024
b0725f8
Remove debug code and format.
FireMasterK Nov 17, 2024
74a6751
Move OidcData to db + some cleanup
Jeidnx Nov 20, 2024
f76f8e0
randomize username
Jeidnx Nov 20, 2024
e4ba195
add redirect to oidc delete; more cleanup
Jeidnx Nov 20, 2024
77cd736
explicitly reject empty hashes
Jeidnx Nov 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {
implementation 'org.bouncycastle:bcprov-jdk18on:1.78.1'
implementation 'com.github.FireMasterK.NewPipeExtractor:NewPipeExtractor:a64e202bb498032e817a702145263590829f3c1d'
implementation 'com.github.FireMasterK:nanojson:9f4af3b739cc13f3d0d9d4b758bbe2b2ae7119d7'
implementation 'com.nimbusds:oauth2-oidc-sdk:11.20.1'
implementation 'com.fasterxml.jackson.core:jackson-core:2.17.2'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.17.2'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.2'
Expand Down
14 changes: 14 additions & 0 deletions config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,17 @@ hibernate.connection.password:changeme
# Frontend configuration
#frontend.statusPageUrl:https://kavin.rocks
#frontend.donationUrl:https://kavin.rocks

# SSO via OIDC
# Each provider needs to have these three options specified. <NAME> is the
# friendly name which will be shown to the clients and used in the database.
# If you want to change the name later, you will have to update the database.
#oidc.provider.<NAME>.clientId:example_piped_client_id
#oidc.provider.<NAME>.clientSecret:example_piped_client_secret
#oidc.provider.<NAME>.issuer:https://idm.example.com

# Ask the provider to re-authenticate the user when account deletion is
# requested. This field is optional and you should only set this to false
# if your provider doesn't support the max_age parameter. You will know when
# trying to delete an account.
#oidc.provider.<NAME>.sendMaxAge = true
28 changes: 28 additions & 0 deletions src/main/java/me/kavin/piped/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import me.kavin.piped.utils.*;
import me.kavin.piped.utils.matrix.SyncRunner;
import me.kavin.piped.utils.obj.MatrixHelper;
import me.kavin.piped.utils.obj.db.OidcData;
import me.kavin.piped.utils.obj.db.PlaylistVideo;
import me.kavin.piped.utils.obj.db.PubSub;
import me.kavin.piped.utils.obj.db.Video;
Expand Down Expand Up @@ -253,5 +254,32 @@ public void run() {
}
}, 0, TimeUnit.MINUTES.toMillis(60));

new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {

var cb = s.getCriteriaBuilder();
var cd = cb.createCriteriaDelete(OidcData.class);
var root = cd.from(OidcData.class);
cd.where(cb.lessThan(root.get("start"), System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(3)));

var tr = s.beginTransaction();

var query = s.createMutationQuery(cd);

int affected = query.executeUpdate();

tr.commit();

if (affected > 0) {
System.out.printf("Cleanup: Removed %o orphaned oidc logins%n", affected);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, TimeUnit.MINUTES.toMillis(5));

}
}
51 changes: 51 additions & 0 deletions src/main/java/me/kavin/piped/consts/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.nimbusds.oauth2.sdk.GeneralException;
import io.minio.MinioClient;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import me.kavin.piped.utils.PageMixin;
import me.kavin.piped.utils.obj.OidcProvider;
import me.kavin.piped.utils.resp.ListLinkHandlerMixin;
import okhttp3.OkHttpClient;
import okhttp3.brotli.BrotliInterceptor;
Expand All @@ -21,8 +25,10 @@

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

Expand Down Expand Up @@ -103,6 +109,7 @@ public class Constants {
public static String YOUTUBE_COUNTRY;

public static final String VERSION;
public static final List<OidcProvider> OIDC_PROVIDERS;

public static final ObjectMapper mapper = JsonMapper.builder()
.addMixIn(Page.class, PageMixin.class)
Expand Down Expand Up @@ -170,12 +177,39 @@ public class Constants {
MATRIX_SERVER = getProperty(prop, "MATRIX_SERVER", "https://matrix-client.matrix.org");
MATRIX_TOKEN = getProperty(prop, "MATRIX_TOKEN");
GEO_RESTRICTION_CHECKER_URL = getProperty(prop, "GEO_RESTRICTION_CHECKER_URL");

OIDC_PROVIDERS = new ObjectArrayList<>();

Map<String, Map<String, String>> oidcProviderConfig = new Object2ObjectOpenHashMap<>();
ArrayNode providerNames = frontendProperties.putArray("oidcProviders");
prop.forEach((_key, _value) -> {
String key = String.valueOf(_key), value = String.valueOf(_value);
if (key.startsWith("hibernate"))
hibernateProperties.put(key, value);
else if (key.startsWith("frontend."))
frontendProperties.put(StringUtils.substringAfter(key, "frontend."), value);
else if (key.startsWith("oidc.provider")) {
String[] split = key.split("\\.");
if (split.length != 4) return;
oidcProviderConfig
.computeIfAbsent(split[2], k -> new Object2ObjectOpenHashMap<>())
.put(split[3], value);
}
});
oidcProviderConfig.forEach((provider, config) -> {
try {
OIDC_PROVIDERS.add(new OidcProvider(
provider,
getRequiredMapValue(config, "clientId"),
getRequiredMapValue(config, "clientSecret"),
getRequiredMapValue(config, "issuer"),
getOptionalMapValue(config, "sendMaxAge", "true")
));
} catch (GeneralException | IOException e) {
System.err.println("Failed to get configuration for '" + provider + "': " + e);
System.exit(1);
}
providerNames.add(provider);
});
frontendProperties.put("imageProxyUrl", IMAGE_PROXY_PART);
frontendProperties.putArray("countries").addAll(
Expand Down Expand Up @@ -220,4 +254,21 @@ private static String getProperty(final Properties prop, String key, String def)

return prop.getProperty(key, def);
}

private static String getRequiredMapValue(final Map<?, String> map, Object key) {
String value = map.get(key);
if (StringUtils.isBlank(value)) {
System.err.println("Missing '" + key + "' in sub-configuration");
System.exit(1);
}
return value;
}

private static String getOptionalMapValue(final Map<?, String> map, Object key, String def) {
String value = map.get(key);
if (StringUtils.isBlank(value)) {
return def;
}
return value;
}
}
36 changes: 36 additions & 0 deletions src/main/java/me/kavin/piped/server/ServerLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
import me.kavin.piped.server.handlers.auth.FeedHandlers;
import me.kavin.piped.server.handlers.auth.StorageHandlers;
import me.kavin.piped.server.handlers.auth.UserHandlers;
import me.kavin.piped.utils.ErrorResponse;
import me.kavin.piped.utils.*;
import me.kavin.piped.utils.obj.OidcProvider;
import me.kavin.piped.utils.resp.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.hibernate.Session;
import org.jetbrains.annotations.NotNull;

import java.net.InetSocketAddress;
import java.net.URI;
import java.util.Objects;
import java.util.concurrent.Executor;

Expand Down Expand Up @@ -258,6 +261,22 @@ AsyncServlet mainServlet(Executor executor) {
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
})).map(GET, "/oidc/:provider/:function", AsyncServlet.ofBlocking(executor, request -> {
try {
String function = request.getPathParameter("function");
OidcProvider provider = getOidcProvider(request.getPathParameter("provider"));
if (provider == null)
return HttpResponse.ofCode(500).withHtml("Can't find the provider on the server");

return switch (function) {
case "login" -> UserHandlers.oidcLoginRequest(provider, request.getQueryParameter("redirect"));
case "callback" -> UserHandlers.oidcLoginCallback(provider, URI.create(request.getFullUrl()));
case "delete" -> UserHandlers.oidcDeleteCallback(provider, URI.create(request.getFullUrl()));
default -> HttpResponse.ofCode(500).withHtml("Invalid function `" + function + "`");
};
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
})).map(POST, "/login", AsyncServlet.ofBlocking(executor, request -> {
try {
LoginRequest body = mapper.readValue(request.loadBody().getResult().asArray(),
Expand Down Expand Up @@ -469,6 +488,14 @@ AsyncServlet mainServlet(Executor executor) {
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
})).map(GET, "/user/delete", AsyncServlet.ofBlocking(executor, request -> {
try {
String session = request.getQueryParameter("session");
String redirect = request.getQueryParameter("redirect");
return UserHandlers.oidcDeleteRequest(session, redirect);
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
})).map(POST, "/logout", AsyncServlet.ofBlocking(executor, request -> {
try {
return getJsonResponse(UserHandlers.logoutResponse(request.getHeader(AUTHORIZATION)), "private");
Expand Down Expand Up @@ -506,6 +533,15 @@ AsyncServlet mainServlet(Executor executor) {
return new CustomServletDecorator(router);
}

private static OidcProvider getOidcProvider(String provider) {
for (int i = 0; i < Constants.OIDC_PROVIDERS.size(); i++) {
OidcProvider curr = Constants.OIDC_PROVIDERS.get(i);
if (curr == null || !curr.name.equals(provider)) continue;
return curr;
}
return null;
}

private static String[] getArray(String s) {

if (s == null) {
Expand Down
Loading
Loading