Skip to content

Commit e0a1a62

Browse files
committed
Document platform authentication
1 parent bbd478c commit e0a1a62

3 files changed

Lines changed: 60 additions & 4 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
This repository contains the ModFest platform, alongside other related projects.
33

44
Platform is hosted at [https://platform.modfest.net](https://platform.modfest.net). If you want
5-
to interact with platform, you might be interested in the [swagger ui](https://platform.modfest.net/swagger-ui/index.html)
5+
to interact with platform, you might be interested in the [swagger ui](https://platform.modfest.net/swagger-ui/index.html).
6+
You might also be interested in how [authentication is done](https://modfest.github.io/platform/platform%20api/security/#authenticating).
67

78
For any other information on platform's internals, please visit the [platform docs](https://modfest.github.io/platform).
8-
It includes information on getting started and setting up a dev environment.
9+
It includes information on getting started and setting up a dev environment, as well as information about internals.

docs/platform api/security.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Security
2+
3+
## Permissions
4+
Associated with each request is a set of permissions. These are permissions such as `users.view_mc`.
5+
The list of permissions are defined in the [`Permissions.java`](https://github.com/ModFest/platform/blob/prod/platform_api/src/main/java/net/modfest/platform/security/Permissions.java) class, which also includes descriptions of what each route
6+
does. What permissions someone has depends on their group, defined in [`PermissionGroup.java`](https://github.com/ModFest/platform/blob/prod/platform_api/src/main/java/net/modfest/platform/security/PermissionGroup.java). Requests are associated
7+
with `UNPRIVILEGED_USERS` by default. Some groups inherit the permissions of other groups.
8+
9+
You can view the permissions associated with a request using the `/meta/me` route. Or you can use the `/debug whoami`
10+
command inside of discord.
11+
12+
## Authenticating
13+
14+
You can authenticate with platform in multiple ways.
15+
16+
### BotFest Token
17+
There's a shared token between BotFest and Platform. When BotFest wants to authenticate it provides this token
18+
through the `BotFest-Secret` header. It also provides a discord id through `BotFest-Target-User`. As long as the
19+
secret matches, the request will be associated with that user. *This means anyone with the BotFest token can impersonate any other user*.
20+
21+
BotFest can also set `BotFest-Target-User` to `@self` in which case the request is associated with BotFest itself, with
22+
the `BOTFEST` permission group.
23+
24+
In development, it might be handy to use this method of authentication for youself. In dev environments the shared token is set in the `gradle.properties`, with it defaulting to `1234abcd5678`. You can use it like so:
25+
26+
```sh
27+
curl -H "BotFest-Secret: 1234abcd5678" -H "BotFest-Target-User: ..." localhost:8070/meta/me
28+
```
29+
30+
# Modrinth token
31+
32+
Any modrinth token can be provided using the `Modrinth-Token` header. The token only needs permission to read the user's
33+
data (not email). When provided, the request will have the permissions of the modfest user associated with that modrinth
34+
account.
35+
36+
# Event token
37+
38+
Specific tokens are generated per event, and are used for syncing the minecraft whitelist. The tokens can be managed via
39+
the panel
40+
41+
## Internal implementation
42+
Security is implemented using Apache Shiro. It's a terrible way of doing things but it works now and is better than
43+
any other framework available for Spring. `SecurityConfig.java` adds a bunch of filters to Shiro. These filters
44+
intercept the various headers and then associate a token object with the request. They don't do any checks for validity,
45+
that is done in `ModFestRealm.java`. There the tokens are read, validated, and then a particular "principal" is associated
46+
with the request. Usually this is a user object, taken straight from the database. `ModFestRealm.java` also contains
47+
the code to associate a permission group with such an "principal".
48+
49+
Then various routes will use the `@RequiresPermissions` annotation or the `SecurityUtils.getSubject().isPermitted` function
50+
to check if the user has a specific permission. The "principal" can also be directly accessed through `SecurityUtils.getSubject().getPrincipal()`. If you use this method you need to deal with the fact that the request might be associated with something other than a user. For example when an event token is used, then the principal is that event. Or if BotFest logs in as itself, then the principal is a singleton object which represents BotFest. The `PermissionUtils.java` has some utils for the most common case (checking if the principal is one of the authors listed on a submission)

platform_api/src/main/java/net/modfest/platform/security/PermissionGroup.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,20 @@ public enum PermissionGroup {
3232
// BotFest needs to create users. It cannot perform actions on behalf of a user
3333
// if it doesn't exist yet.
3434
Permissions.Users.CREATE,
35-
// BotFest is able to subscribe to user data changing, to enable
36-
// it to give out roles
35+
// BotFest is able to subscribe to any changes in user data, and
36+
// also list all users. This is to compute the set of roles it
37+
// needs to assign to users
3738
Permissions.Users.LIST_ALL
3839
)),
3940
/**
4041
* Permissions given when the minecraft server logs in
4142
*/
4243
EVENT_MC_SERVER(null, Set.of(
44+
// The minecraft server needs to list all users in order to find anyone who
45+
// needs to be on the whitelist
4346
Permissions.Users.LIST_ALL,
47+
// The minecraft server needs to know the minecraft accounts associated with
48+
// modfest accounts in order to whitelist them
4449
Permissions.Users.VIEW_MINECRAFT
4550
));
4651

0 commit comments

Comments
 (0)