Security
Authentication and Authorization
Authentication
The main logic is located in the system/jwt.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
EdDSA
for 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_id
in theuser_tokens
table. - Transport:
- For
HTTP
, the tokens are sent in theauthorization
header asBearer
tokens. - For
gRPC
, the tokens are sent in themetadata
asauthorization
tokens.
- 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:
Remember to change the JWT_KEY
environment variable to the new public key if you are using the Next.js implementation!
Generating Tokens
Validating Tokens
Next.js specific implementation
Because of the limitaions of middleware in Next.js (Edge environment), the public key is stored in the environment variable JWT_KEY
as string.
Authorization
The main logic is located in the system/access.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
CheckUserAttr
function:
And then check the access:
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 the authorization
header or the metadata
.
Then, in the service layer, validate the token and check the access:
Need help?
Visit our discord server to ask any questions, make suggestions and give feedback :).
Was this page helpful?