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

Add CAS auth #707

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Edit configuration files as necessary. The following `pxls.conf` keys MUST be co
* `database.url`
* `host`
* `oauth` (necessary for any form of user authentication)
* [Reddit][redditapps], [Google][googleconsole], [Discord][discordapps], [VK][vkapps], and [Tumblr][tumblrapps] are current supported.
* [Reddit][redditapps], [Google][googleconsole], [Discord][discordapps], [VK][vkapps], [Tumblr][tumblrapps], and [CAS applications](https://apereo.github.io/cas/7.0.x/index.html) are current supported.

Run with `java -jar pxls*.jar`

Expand Down
12 changes: 12 additions & 0 deletions resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ oauth {
enabled: true
registrationEnabled: true
}

// Create your CAS app with documentation https://apereo.github.io/cas/7.0.x/index.html
// WARNING: Cas tokens cannot be revoked by the user through the provider website.
// This login method is only suitable for short-lived events.
cas {
name: "CAS" // Custom name for this login method
loginUrl: "" // https://example.com/cas/login
validateUrl: "" // https://example.com/cas/serviceValidate
usernameField: "user"
enabled: true
registrationEnabled: true
}
}

textFilter {
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/space/pxls/auth/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,15 @@ public InvalidAccountException(String s) {
}

public boolean use() {
return enabled && !App.getConfig().getString("oauth."+id+".key").isEmpty();
if (id.equals("cas")) {
return enabled
&& !App.getConfig().getString("oauth.cas.loginUrl").isEmpty()
&& !App.getConfig().getString("oauth.cas.validateUrl").isEmpty()
&& !App.getConfig().getString("oauth.cas.usernameField").isEmpty();
} else {
return enabled
&& !App.getConfig().getString("oauth."+id+".key").isEmpty();
}
}

public abstract String getName();
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/space/pxls/auth/CasAuthService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package space.pxls.auth;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import kong.unirest.UnirestException;
import space.pxls.App;

public class CasAuthService extends AuthService {
public CasAuthService(String id) {
super(id, App.getConfig().getBoolean("oauth.cas.enabled"), App.getConfig().getBoolean("oauth.cas.registrationEnabled"));
}

@Override
public String getRedirectUrl(String state) {
try {
state = URLEncoder.encode(state, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
return "";
}
String service = getCallbackUrl() + "?state=" + state;
try {
service = URLEncoder.encode(service, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
return "";
}
return App.getConfig().getString("oauth.cas.loginUrl") + "?service=" + service;
}

@Override
public String getToken(String token) throws UnirestException {
HttpResponse<String> response = Unirest.get(App.getConfig().getString("oauth.cas.validateUrl"))
.queryString("service", getCallbackUrl())
.queryString("ticket", token)
.asString();

if (response.getStatus() != 200) {
throw new UnirestException("CAS validation failed: " + response.getStatusText());
}

String username;
try {
String start_username_field = "<cas:" + App.getConfig().getString("oauth.cas.usernameField") + ">";
String end_username_field = "</cas:" + App.getConfig().getString("oauth.cas.usernameField") + ">";
username = response.getBody().split(start_username_field)[1].split(end_username_field)[0];
} catch (Exception e) {
throw new UnirestException("CAS validation failed: " + e.getMessage());
}

return username;
}

@Override
public String getIdentifier(String token) throws UnirestException {
return token;
}

public String getName() {
try {
String custom_name = App.getConfig().getString("oauth.cas.name");
if (custom_name != null && !custom_name.isEmpty()) {
return custom_name;
}
} catch (Exception e) {
// ignore
}
return "CAS";
}

@Override
public void reloadEnabledState() {
this.enabled = App.getConfig().getBoolean("oauth.cas.enabled");
this.registrationEnabled = App.getConfig().getBoolean("oauth.cas.registrationEnabled");
}
}
8 changes: 7 additions & 1 deletion src/main/java/space/pxls/server/WebHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public WebHandler() {
addServiceIfAvailable("vk", new VKAuthService("vk"));
addServiceIfAvailable("tumblr", new TumblrAuthService("tumblr"));
addServiceIfAvailable("twitch", new TwitchAuthService("twitch"));
addServiceIfAvailable("cas", new CasAuthService("cas"));
}

public void getRequestingUserFactions(HttpServerExchange exchange) throws Exception {
Expand Down Expand Up @@ -1687,7 +1688,12 @@ public void auth(HttpServerExchange exchange) throws UnirestException {
}

// Get the one-time authorization code from the request
String code = extractOAuthCode(exchange);
String code;
if ("cas".equals(id)) {
code = exchange.getQueryParameters().get("ticket").element();
} else {
code = extractOAuthCode(exchange);
}
if (code == null) {
if (redirect) {
redirect(exchange, doneBase + "?nologin=1");
Expand Down