Skip to content

Commit

Permalink
add redirect to oidc delete; more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeidnx committed Nov 20, 2024
1 parent f76f8e0 commit e4ba195
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 12 deletions.
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));

}
}
5 changes: 3 additions & 2 deletions src/main/java/me/kavin/piped/server/ServerLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,9 @@ AsyncServlet mainServlet(Executor executor) {
}
})).map(GET, "/user/delete", AsyncServlet.ofBlocking(executor, request -> {
try {
var session = request.getQueryParameter("session");
return UserHandlers.oidcDeleteRequest(session);
String session = request.getQueryParameter("session");
String redirect = request.getQueryParameter("redirect");
return UserHandlers.oidcDeleteRequest(session, redirect);
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ public static HttpResponse oidcLoginCallback(OidcProvider provider, URI requestU
URI callback = new URI(Constants.PUBLIC_URL + "/oidc/" + provider.name + "/callback");
AuthorizationCode code = authResponse.getAuthorizationCode();

if (code == null) {
return HttpResponse.ofCode(400).withHtml(
"Your oidc provider sent an invalid code. Try again or contact your oidc admin"
);
}

AuthorizationGrant codeGrant = new AuthorizationCodeGrant(code, callback, data.getOidVerifier());

ClientAuthentication clientAuth = new ClientSecretBasic(provider.clientID, provider.clientSecret);
Expand Down Expand Up @@ -241,12 +247,16 @@ public static HttpResponse oidcLoginCallback(OidcProvider provider, URI requestU
return HttpResponse.redirect302(data.data + "?session=" + sessionId);
}

public static HttpResponse oidcDeleteRequest(String session) throws Exception {
public static HttpResponse oidcDeleteRequest(String session, String redirect) throws Exception {

if (StringUtils.isBlank(session)) {
return HttpResponse.ofCode(400).withHtml("session is a required parameter");
}

if (StringUtils.isBlank(redirect)) {
return HttpResponse.ofCode(400).withHtml("redirect is a required parameter");
}

OidcProvider provider = null;

try (Session s = DatabaseSessionFactory.createSession()) {
Expand Down Expand Up @@ -282,7 +292,7 @@ public static HttpResponse oidcDeleteRequest(String session) throws Exception {
CodeVerifier pkceVerifier = new CodeVerifier();

URI callback = URI.create(String.format("%s/oidc/%s/delete", Constants.PUBLIC_URL, provider.name));
OidcData data = new OidcData(session + "|" + Instant.now().getEpochSecond(), pkceVerifier);
OidcData data = new OidcData(session + "|" + redirect, pkceVerifier);
String state = data.getState();

DatabaseHelper.setOidcData(data);
Expand All @@ -297,7 +307,7 @@ public static HttpResponse oidcDeleteRequest(String session) throws Exception {
.nonce(data.getOidNonce());

if (provider.sendMaxAge) {
// This parameter is optional and the idp doesn't have to honor it.
// This parameter is optional and the idp doesn't have to honor it.
oidcRequestBuilder.maxAge(0);
}

Expand All @@ -316,11 +326,18 @@ public static HttpResponse oidcDeleteCallback(OidcProvider provider, URI request
);
}

long start = Long.parseLong(data.data.split("\\|")[1]);
String redirect = data.data.split("\\|")[1];
String session = data.data.split("\\|")[0];

URI callback = new URI(Constants.PUBLIC_URL + "/oidc/" + provider.name + "/delete");
AuthorizationCode code = sr.getAuthorizationCode();

if (code == null) {
return HttpResponse.ofCode(400).withHtml(
"Your oidc provider sent an invalid code. Try again or contact your oidc admin"
);
}

AuthorizationGrant codeGrant = new AuthorizationCodeGrant(code, callback, data.getOidVerifier());

ClientAuthentication clientAuth = new ClientSecretBasic(provider.clientID, provider.clientSecret);
Expand Down Expand Up @@ -355,7 +372,7 @@ public static HttpResponse oidcDeleteCallback(OidcProvider provider, URI request
return HttpResponse.ofCode(400).withHtml("Couldn't get the `auth_time` claim from the provided id token");
}

if (authTime < start) {
if (authTime <= data.start) {
return HttpResponse.ofCode(500).withHtml(
"Your oidc provider didn't verify your identity. Please try again or contact your oidc admin."
);
Expand All @@ -377,7 +394,7 @@ public static HttpResponse oidcDeleteCallback(OidcProvider provider, URI request
tr.commit();
}

return HttpResponse.redirect302(Constants.FRONTEND_URL + "/preferences?deleted=" + session);
return HttpResponse.redirect302(redirect + "?deleted=true");
}

public static byte[] deleteUserResponse(String session, String pass) throws IOException {
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/me/kavin/piped/utils/DatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,24 @@ public static void setOidcData(OidcData data) {
}

public static OidcData getOidcData(String state) {
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
try (Session s = DatabaseSessionFactory.createSession()) {

CriteriaBuilder cb = s.getCriteriaBuilder();
CriteriaQuery<OidcData> cr = cb.createQuery(OidcData.class);
Root<OidcData> root = cr.from(OidcData.class);
cr.select(root).where(cb.equal(root.get("state"), state));

return s.createQuery(cr).uniqueResult();
OidcData data = s.createQuery(cr).uniqueResult();

if (data == null){
return null;
}

var tr = s.beginTransaction();
s.remove(data);
tr.commit();

return data;
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/me/kavin/piped/utils/obj/db/OidcData.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public class OidcData implements Serializable {
public String state;

@Column(name = "start")
public long auth_start;
public long start;

public OidcData(String data, CodeVerifier pkceVerifier) {
this.nonce = new Nonce().toString();
this.verifierSecret = pkceVerifier.getValue();
this.data = data;
this.auth_start = System.currentTimeMillis() / 1000L;
this.start = System.currentTimeMillis() / 1000L;
this.state = getState();
}

Expand Down

0 comments on commit e4ba195

Please sign in to comment.