Skip to content

Commit

Permalink
- CHG: Changed all endpoints to use Response.* calls for return state…
Browse files Browse the repository at this point in the history
…ments.
  • Loading branch information
sebastian-raubach committed Jun 17, 2022
1 parent 9454dda commit 663fe08
Show file tree
Hide file tree
Showing 27 changed files with 460 additions and 648 deletions.
Binary file modified .gradle/6.7/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/6.7/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/6.7/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/6.7/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/6.7/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/6.7/javaCompile/classAnalysis.bin
Binary file not shown.
Binary file modified .gradle/6.7/javaCompile/javaCompile.lock
Binary file not shown.
Binary file modified .gradle/6.7/javaCompile/taskHistory.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified .gradle/checksums/checksums.lock
Binary file not shown.
Binary file modified .gradle/checksums/md5-checksums.bin
Binary file not shown.
Binary file modified .gradle/checksums/sha1-checksums.bin
Binary file not shown.
476 changes: 198 additions & 278 deletions .idea/workspace.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package raubach.fricklweb.server.resource;

import jakarta.ws.rs.core.*;
import raubach.fricklweb.server.computed.Settings;
import raubach.fricklweb.server.util.ServerProperty;
import raubach.fricklweb.server.util.watcher.PropertyWatcher;

import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;

@Path("settings")
public class SettingsResource extends ContextResource
{
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Settings getSettings()
public Response getSettings()
{
Settings settings = new Settings();
settings.setAuthEnabled(PropertyWatcher.authEnabled());
Expand All @@ -22,6 +22,6 @@ public Settings getSettings()
settings.setPlausibleHashMode(PropertyWatcher.getBoolean(ServerProperty.PLAUSIBLE_HASH_MODE));
settings.setPlausibleDomain(PropertyWatcher.get(ServerProperty.PLAUSIBLE_DOMAIN));

return settings;
return Response.ok(settings).build();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package raubach.fricklweb.server.resource;

import raubach.fricklweb.server.computed.DataScanResult;
import raubach.fricklweb.server.scanner.ImageScanner;

import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.*;
import raubach.fricklweb.server.scanner.ImageScanner;

/**
* @author Sebastian Raubach
Expand All @@ -15,8 +13,8 @@ public class StatusResource
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public DataScanResult getJson()
public Response getJson()
{
return ImageScanner.SCANRESULT;
return Response.ok(ImageScanner.SCANRESULT).build();
}
}
33 changes: 10 additions & 23 deletions src/main/java/raubach/fricklweb/server/resource/TokenResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,58 +37,46 @@ public class TokenResource extends ContextResource
@Secured
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public boolean deleteToken(LoginDetails user)
public Response deleteToken(LoginDetails user)
throws IOException
{
boolean enabled = PropertyWatcher.authEnabled();

if (!enabled)
{
resp.sendError(Response.Status.SERVICE_UNAVAILABLE.getStatusCode());
return false;
}
return Response.status(Response.Status.SERVICE_UNAVAILABLE).build();

if (user == null)
{
resp.sendError(Response.Status.NOT_FOUND.getStatusCode(), StatusMessage.NOT_FOUND_TOKEN);
return false;
}
return Response.status(Response.Status.NOT_FOUND).build();

AuthenticationFilter.UserDetails userDetails = (AuthenticationFilter.UserDetails) securityContext.getUserPrincipal();

if (userDetails == null || !Objects.equals(userDetails.getToken(), user.getPassword()))
{
resp.sendError(Response.Status.FORBIDDEN.getStatusCode(), StatusMessage.FORBIDDEN_ACCESS_TO_OTHER_USER);
return false;
}
return Response.status(Response.Status.FORBIDDEN.getStatusCode(), StatusMessage.FORBIDDEN_ACCESS_TO_OTHER_USER).build();

try
{
// Try and see if it's a valid UUID
UUID.fromString(user.getPassword());
AuthenticationFilter.removeToken(user.getPassword(), req, resp);
return true;
return Response.ok(true).build();
}
catch (Exception e)
{
e.printStackTrace();
return false;
return Response.ok(false).build();
}
}

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Token postToken(LoginDetails request)
public Response postToken(LoginDetails request)
throws IOException
{
boolean enabled = PropertyWatcher.authEnabled();

if (!enabled)
{
resp.sendError(Response.Status.SERVICE_UNAVAILABLE.getStatusCode());
return null;
}
return Response.status(Response.Status.SERVICE_UNAVAILABLE).build();

String username = PropertyWatcher.get(ServerProperty.ADMIN_USERNAME);
String password = PropertyWatcher.get(ServerProperty.ADMIN_PASSWORD);
Expand All @@ -106,10 +94,9 @@ public Token postToken(LoginDetails request)
}
else
{
resp.sendError(Response.Status.FORBIDDEN.getStatusCode(), StatusMessage.FORBIDDEN_INVALID_CREDENTIALS);
return null;
return Response.status(Response.Status.FORBIDDEN.getStatusCode(), StatusMessage.FORBIDDEN_INVALID_CREDENTIALS).build();
}

return new Token(token, imageToken, AuthenticationFilter.AGE, System.currentTimeMillis());
return Response.ok(new Token(token, imageToken, AuthenticationFilter.AGE, System.currentTimeMillis())).build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package raubach.fricklweb.server.resource.accesstoken;

import jakarta.ws.rs.*;
import jakarta.ws.rs.core.*;
import org.jooq.DSLContext;
import org.jooq.tools.StringUtils;
import raubach.fricklweb.server.Database;
Expand All @@ -8,12 +10,9 @@
import raubach.fricklweb.server.resource.PaginatedServerResource;
import raubach.fricklweb.server.util.watcher.PropertyWatcher;

import jakarta.annotation.security.PermitAll;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.*;
import java.io.IOException;
import java.sql.*;
import java.util.*;
import java.util.Objects;

import static raubach.fricklweb.server.database.tables.AccessTokens.*;
import static raubach.fricklweb.server.database.tables.AlbumAccessToken.*;
Expand All @@ -26,74 +25,68 @@ public class AccessTokenResource extends PaginatedServerResource
@Path("/{tokenId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public boolean deleteAccessToken(@PathParam("tokenId") Integer tokenId, AlbumAccessToken token)
public Response deleteAccessToken(@PathParam("tokenId") Integer tokenId, AlbumAccessToken token)
throws IOException, SQLException
{
if (tokenId == null || token == null || token.getTokenId() == null || !Objects.equals(tokenId, token.getTokenId()))
{
resp.sendError(Response.Status.BAD_REQUEST.getStatusCode());
return false;
}
return Response.status(Response.Status.BAD_REQUEST).build();

try (Connection conn = Database.getConnection();
DSLContext context = Database.getContext(conn))
{
return context.deleteFrom(ACCESS_TOKENS)
.where(ACCESS_TOKENS.ID.eq(tokenId))
.execute() > 0;
return Response.ok(context.deleteFrom(ACCESS_TOKENS)
.where(ACCESS_TOKENS.ID.eq(tokenId))
.execute() > 0)
.build();
}
}

@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public List<AlbumAccessToken> getAccessTokens()
public Response getAccessTokens()
throws IOException, SQLException
{
AuthenticationFilter.UserDetails userDetails = (AuthenticationFilter.UserDetails) securityContext.getUserPrincipal();
boolean auth = PropertyWatcher.authEnabled();

if (auth && StringUtils.isEmpty(userDetails.getToken()))
{
resp.sendError(Response.Status.FORBIDDEN.getStatusCode());
return null;
}
return Response.status(Response.Status.FORBIDDEN).build();

try (Connection conn = Database.getConnection();
DSLContext context = Database.getContext(conn))
{
return context.selectFrom(ALBUM_ACCESS_TOKEN)
.limit(pageSize)
.offset(pageSize * currentPage)
.fetch()
.into(AlbumAccessToken.class);
return Response.ok(context.selectFrom(ALBUM_ACCESS_TOKEN)
.limit(pageSize)
.offset(pageSize * currentPage)
.fetch()
.into(AlbumAccessToken.class))
.build();
}
}

@Path("/count")
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public int getAccessTokenCount()
public Response getAccessTokenCount()
throws IOException, SQLException
{
AuthenticationFilter.UserDetails userDetails = (AuthenticationFilter.UserDetails) securityContext.getUserPrincipal();
boolean auth = PropertyWatcher.authEnabled();

if (auth && StringUtils.isEmpty(userDetails.getToken()))
{
resp.sendError(Response.Status.FORBIDDEN.getStatusCode());
return 0;
}
return Response.status(Response.Status.FORBIDDEN).build();

try (Connection conn = Database.getConnection();
DSLContext context = Database.getContext(conn))
{
return context.selectCount()
.from(ALBUM_ACCESS_TOKEN)
.limit(pageSize)
.offset(pageSize * currentPage)
.fetchAny(0, int.class);
return Response.ok(context.selectCount()
.from(ALBUM_ACCESS_TOKEN)
.limit(pageSize)
.offset(pageSize * currentPage)
.fetchAny(0, int.class))
.build();
}
}
}
Loading

0 comments on commit 663fe08

Please sign in to comment.