Skip to content

Commit

Permalink
Added self-host options, fixed bugs and more
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianFun123 committed Mar 3, 2022
1 parent 392bdde commit 10a8d9f
Show file tree
Hide file tree
Showing 18 changed files with 290 additions and 33 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ DATABASE_HOST=
DATABASE_PORT=3306

SERVER_NAME=http://example.com


# Optional
PASTEFY_INFO_CUSTOM_LOGO=https://urltoimage
PASTEFY_INFO_CUSTOM_NAME=Custom Name
PASTEFY_INFO_CUSTOM_FOOTER=WEBSITE=https://example.org,SEPERATED BY COMMA=https://example.org

# Requires login for read and creation of pastes
PASTEFY_LOGIN_REQUIRED=false
# Login-requirements for specific access types
PASTEFY_LOGIN_REQUIRED_CREATE=false
# This will disable the raw mode as well for browser users
PASTEFY_LOGIN_REQUIRED_READ=false

# Check the encryption checkbox by default
PASTEFY_ENCRYPTION_DEFAULT=false
```
### Adding login
You can choose between [INTERAAPPS](https://accounts.interaapps.de/developers/projects) (best integration), [GOOGLE](https://support.google.com/cloud/answer/6158849?hl=en), [GITHUB](https://docs.github.com/en/developers/apps/building-oauth-apps/creating-an-oauth-app), [DISCORD](https://discord.com/developers/docs/topics/oauth2) or [TWITCH](https://dev.twitch.tv/docs/authentication) for the provider (You can use all of them at the same time).
Expand Down
69 changes: 69 additions & 0 deletions backend/dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.interaapps.pastefy</groupId>
<artifactId>core</artifactId>
<version>6.0</version>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>static/**/*.*</include>
</includes>
</resource>
</resources>
<finalName>backend</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>de.interaapps.pastefy.Pastefy</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
</project>

5 changes: 5 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
<artifactId>http-server</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.javawebstack</groupId>
<artifactId>abstract-data</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.javawebstack</groupId>
<artifactId>http-client</artifactId>
Expand Down
41 changes: 41 additions & 0 deletions backend/src/main/java/de/interaapps/pastefy/Pastefy.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
Expand All @@ -48,6 +49,9 @@ public static Pastefy getInstance() {
private Passport passport;
private OAuth2Strategy oAuth2Strategy;
private HTTPServer httpServer;
private boolean loginRequired = false;
private boolean loginRequiredForRead = false;
private boolean loginRequiredForCreate = false;

public Pastefy(){
config = new Config();
Expand Down Expand Up @@ -107,12 +111,27 @@ protected void setupConfig() {
map.put("OAUTH2_DISCORD_CLIENT_ID", "oauth2.discord.id");
map.put("OAUTH2_DISCORD_CLIENT_SECRET", "oauth2.discord.secret");

map.put("PASTEFY_INFO_CUSTOM_LOGO", "pastefy.info.custom.logo");
map.put("PASTEFY_INFO_CUSTOM_NAME", "pastefy.info.custom.name");
map.put("PASTEFY_INFO_CUSTOM_FOOTER", "pastefy.info.custom.footer");

map.put("PASTEFY_ENCRYPTION_DEFAULT", "pastefy.encryption.default");

map.put("PASTEFY_LOGIN_REQUIRED", "pastefy.loginrequired");
map.put("PASTEFY_LOGIN_REQUIRED_CREATE", "pastefy.loginrequired.create");
map.put("PASTEFY_LOGIN_REQUIRED_READ", "pastefy.loginrequired.read");


File file = new File(".env");
if (file.exists()) {
config.add(new EnvFile(file).withVariables(), map);
} else {
config.add(new EnvFile().withVariables(), map);
}

loginRequired = config.get("pastefy.loginrequired", "false").toLowerCase(Locale.ROOT).equals("true");
loginRequiredForCreate = loginRequired || config.get("pastefy.loginrequired.create", "false").toLowerCase(Locale.ROOT).equals("true");
loginRequiredForRead = loginRequired || config.get("pastefy.loginrequired.read", "false").toLowerCase(Locale.ROOT).equals("true");
}

protected void setupModels(){
Expand Down Expand Up @@ -166,6 +185,16 @@ protected void setupServer() {
return new ExceptionResponse(throwable);
});
httpServer.middleware("auth", new AuthMiddleware());
httpServer.middleware("auth-login-required-read", exchange -> {
if (loginRequiredForRead && exchange.attrib("user") == null)
throw new AuthenticationException();
return null;
});
httpServer.middleware("auth-login-required-create", exchange -> {
if (loginRequiredForCreate && exchange.attrib("user") == null)
throw new AuthenticationException();
return null;
});

if (getConfig().has("ratelimiter.millis"))
httpServer.middleware("rate-limiter", new RateLimitMiddleware(getConfig().getInt("ratelimiter.millis", 5000), getConfig().getInt("ratelimiter.limit", 5)).createAutoDeadRateLimitsRemover(1000*60*10));
Expand Down Expand Up @@ -239,4 +268,16 @@ public OAuth2Strategy getOAuth2Strategy() {
public Config getConfig() {
return config;
}

public boolean isLoginRequired() {
return loginRequired;
}

public boolean isLoginRequiredForCreate() {
return loginRequiredForCreate;
}

public boolean isLoginRequiredForRead() {
return loginRequiredForRead;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package de.interaapps.pastefy.controller;

import de.interaapps.pastefy.Pastefy;
import de.interaapps.pastefy.model.responses.app.AppInfoResponse;
import org.javawebstack.httpserver.router.annotation.PathPrefix;
import org.javawebstack.httpserver.router.annotation.verbs.Get;

@PathPrefix("/api/v2/app")
public class AppController extends HttpController {
@Get("/info")
public AppInfoResponse appInfo(){
return new AppInfoResponse(Pastefy.getInstance());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public CreateFolderResponse createFolder(Exchange exchange, @Body CreateFolderRe
}

@Get("/{id}")
@With("auth-login-required-read")
public FolderResponse getFolder(Exchange exchange, @Path("id") String id, @Attrib("user") User user) {
Folder folder = Repo.get(Folder.class).where("key", id).first();
if (folder == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class PasteController extends HttpController {

@Post
@With("rate-limiter")
@With({"rate-limiter", "auth-login-required-create"})
public CreatePasteResponse create(Exchange exchange, @Body CreatePasteRequest request, @Attrib("user") User user, @Path("id") String pasteId) {
CreatePasteResponse response = new CreatePasteResponse();

Expand Down Expand Up @@ -79,6 +79,7 @@ public ActionResponse putPaste(@Body EditPasteRequest request, @Path("id") Strin
}

@Get("/{id}")
@With("auth-login-required-read")
public PasteResponse getPaste(Exchange exchange, @Path("id") String id, @Attrib("user") User user) {
Paste paste = Repo.get(Paste.class).where("key", id).first();
if (paste == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import de.interaapps.pastefy.model.database.Paste;
import org.javawebstack.httpserver.Exchange;
import org.javawebstack.httpserver.helper.MimeType;
import org.javawebstack.httpserver.router.annotation.With;
import org.javawebstack.httpserver.router.annotation.params.Path;
import org.javawebstack.httpserver.router.annotation.verbs.Get;
import org.javawebstack.orm.Repo;

public class RawController extends HttpController {

@Get("/{id}/raw")
@With("auth-login-required-read")
public String getPasteRaw(Exchange exchange, @Path("id") String id) {
Paste paste = Repo.get(Paste.class).where("key", id).first();
exchange.contentType(MimeType.PLAIN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Paste extends Model {
@Column
private String title;

@Column(size = 0) // Sets MySQL Field type to TEXT
@Column(size = 16777215)
private String content;

@Column(size = 8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class User extends Model {
@Column
public AuthenticationProvider authProvider;

@Column
public Type type = Type.USER;

@Column
public Timestamp createdAt;

Expand Down Expand Up @@ -104,6 +107,13 @@ public String getName() {
}
}

public enum Type {
USER,
ADMIN,
BLOCKED,
AWAITING_ACCESS
}

public String getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package de.interaapps.pastefy.model.responses.app;

import de.interaapps.pastefy.Pastefy;
import org.javawebstack.webutils.config.Config;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class AppInfoResponse {

public String customLogo;
public String customName;
public Map<String, String> customFooter;
public boolean loginRequiredForRead;
public boolean loginRequiredForCreate;

public boolean encryptionIsDefault;

public AppInfoResponse(Pastefy pastefy) {
Config config = pastefy.getConfig();

if (config.has("pastefy.info.custom.logo"))
customLogo = config.get("pastefy.info.custom.logo");

if (config.has("pastefy.info.custom.name"))
customName = config.get("pastefy.info.custom.name");

encryptionIsDefault = config.get("pastefy.encryption.default", "false").toLowerCase(Locale.ROOT).equals("true");

loginRequiredForRead = pastefy.isLoginRequiredForRead();
loginRequiredForCreate = pastefy.isLoginRequiredForCreate();

String footerString = config.get("pastefy.info.custom.footer", "");
customFooter = new HashMap<>();
for (String str : footerString.split(",")) {
if (str.contains("=")) {
String[] split = str.split("=");
customFooter.put(split[0], split[1]);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class UserResponse {
public String profilePicture;
public String authType;
public List<String> authTypes = new ArrayList<>(Pastefy.getInstance().getOAuth2Strategy().getProviders().keySet());
public User.Type type;

public UserResponse(User user) {
if (user == null)
Expand All @@ -27,5 +28,6 @@ public UserResponse(User user) {
profilePicture = user.getAvatar();
id = user.getId();
loggedIn = true;
type = user.type;
}
}
Loading

0 comments on commit 10a8d9f

Please sign in to comment.