Authentication
The main logic is located in theauth/auth.go file.
JWT-Based Authentication
- JWT Authentication: The system uses JSON Web Tokens (JWT) with refresh token rotation for managing user sessions.
- Encryption: JWTs are encrypted using
EdDSAfor enhanced security. - Server-Side Security: As much logic as possible is handled on the server side to maintain security.
- Token can be invalidated: Refresh tokens allow generating new JWTs and can be manually invalidated by clearing the
user_idin thetokenstable. - Transport:
- For
HTTP, the tokens are sent in theauthorizationheader asBearertokens. - For
gRPC, the tokens are sent in themetadataasauthorizationtokens.
- For
- Key Management:
- Private/Public Key Pair: The system uses a private/public key pair for token validation.
- Key Generation: To generate new keys, run the script:
Authorization
The main logic is located in theauth/auth.go file.
Role-Based Access Control (RBAC)
- The main authorization mechanism is based on Discord permissions.
- Each user has a set of permissions that are checked against the required permissions for a given action.
- Permissions are stored in a variable-length integer serialized into a string, and are calculated using bitwise operations.
- This allows for a high level of granularity in permissions, a very low storage overhead, and fast permission checks.
Permissions Constants
Checking Permissions
Attribute-Based Access Control (ABAC)
- The system also supports Attribute-Based Access Control (ABAC) for more complex access control scenarios.
- Define policies in the
CheckUserAttrfunction:
How to Use
Combine the two mechanisms to create a secure and flexible access control system. The flow start with extracting the token from the request, either from theauthorization header or the metadata.
Then, in the service layer, validate the token and check the access: