|
| 1 | +# Authentification |
| 2 | + |
| 3 | +## JWT |
| 4 | + |
| 5 | +JSON Web Tokens (JWT) are an open standard (RFC 7519) for securely transmitting information between parties as a JSON object. |
| 6 | + |
| 7 | +### Structure of a JWT |
| 8 | + |
| 9 | +A JWT consists of three parts, separated by dots (`.`): |
| 10 | + |
| 11 | +``` |
| 12 | +header.payload.signature |
| 13 | +``` |
| 14 | + |
| 15 | +#### 1. Header |
| 16 | +Contains the token type and the signing algorithm used: |
| 17 | +```json |
| 18 | +{ |
| 19 | + "alg": "RS256", |
| 20 | + "typ": "JWT" |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +#### 2. Payload |
| 25 | +Contains the claims (statements about the user): |
| 26 | +```json |
| 27 | +{ |
| 28 | + "sub": "userId123", |
| 29 | + "iat": 1707494400, |
| 30 | + "exp": 1707498000, |
| 31 | + "roles": ["student"] |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +#### 3. Signature |
| 36 | +Verifies the integrity of the token: |
| 37 | +``` |
| 38 | +RSASHA256( |
| 39 | + base64UrlEncode(header) + "." + base64UrlEncode(payload), |
| 40 | + privateKey |
| 41 | +) |
| 42 | +``` |
| 43 | + |
| 44 | +### Public/Private Key Signing |
| 45 | + |
| 46 | +The Schulcloud uses **asymmetric encryption**: |
| 47 | + |
| 48 | +| Key | Usage | |
| 49 | +|-----|-------| |
| 50 | +| **Private Key** | Signs the JWT (auth server only) | |
| 51 | +| **Public Key** | Verifies the JWT (all services) | |
| 52 | + |
| 53 | +### Benefits |
| 54 | + |
| 55 | +- **Stateless**: No server-side session storage required |
| 56 | +- **Scalable**: Each service can validate tokens independently |
| 57 | +- **Secure**: Signature prevents tampering |
| 58 | + |
| 59 | +### Example Flow |
| 60 | + |
| 61 | +``` |
| 62 | +1. Login → Server creates JWT with Private Key |
| 63 | +2. Client stores JWT |
| 64 | +3. Request + JWT → Service validates with Public Key |
| 65 | +4. Access granted/denied |
| 66 | +``` |
| 67 | + |
| 68 | +## Auto Logout |
| 69 | + |
| 70 | +To handle auto logouts and token refresh for user JWTs, we use Valkey as a "JWT-Session-Store". |
| 71 | +The user can refresh their JWT until the maximum lifetime of the JWT is reached. |
| 72 | + |
| 73 | +## Related Code |
| 74 | + |
| 75 | +Authentication |
| 76 | +- [Authentication Module](https://github.com/hpi-schul-cloud/schulcloud-server/tree/main/apps/server/src/modules/authentication) - Main authentication module |
| 77 | +- [Authentication Stategies](https://github.com/hpi-schul-cloud/schulcloud-server/tree/main/apps/server/src/modules/authentication/strategy) - Authentication stategies |
| 78 | +- [Login Controller](https://github.com/hpi-schul-cloud/schulcloud-client/blob/main/controllers/login.js) - Legacy-client-side login controller |
| 79 | + |
| 80 | +For OAuth |
| 81 | +- [OAuth Controller](https://github.com/hpi-schul-cloud/schulcloud-server/blob/main/apps/server/src/modules/oauth/api/oauth.controller.ts) - OAuth API controller |
| 82 | +- [OAuth Provider Controller](https://github.com/hpi-schul-cloud/schulcloud-server/blob/main/apps/server/src/modules/oauth-provider/api/oauth-provider.controller.ts) - OAuth provider API controller |
| 83 | +- [Releated OAuth Documentation](/docs/topics/oauth/concept) |
| 84 | + |
| 85 | +For Ldap |
| 86 | +tbd. |
| 87 | + |
| 88 | +Validation decorator |
| 89 | +- [Auth Guard Decorators](https://github.com/hpi-schul-cloud/schulcloud-server/tree/main/apps/server/src/infra/auth-guard) - Infra module for our NestJS decorators |
| 90 | +- [Auth Validation Strategies](https://github.com/hpi-schul-cloud/schulcloud-server/tree/main/apps/server/src/infra/auth-guard/strategy) - Validate authentication strategies |
| 91 | + |
| 92 | +## Authentification Stategies |
| 93 | + |
| 94 | +We have 3 strategies that can be create a SVS JWT |
| 95 | +1. Local - When login credentials are in the SVS. |
| 96 | +2. Ldap - We want to login over a external ldap system used in BRB. |
| 97 | +3. OAuth - We use a OAuth Flow to login over a external IDM like Moin.Schule (NBC), or TSP. |
| 98 | + |
| 99 | +We have 3 base validation strategies. |
| 100 | +1. For the jwt it self over http. |
| 101 | +2. For the jwt but used in web sockets. |
| 102 | +3. For x-api-key. |
| 103 | + |
| 104 | +The x-api-key stategy is nearly deprecated and will be replaced with system user logins based on the first strategy. |
| 105 | + |
| 106 | +## Decorator Example |
| 107 | + |
| 108 | +[MeController Example](https://github.com/hpi-schul-cloud/schulcloud-server/blob/main/apps/server/src/modules/me/api/me.controller.ts) |
| 109 | + |
| 110 | +```typescript |
| 111 | +import { JwtAuthentication } from '@infra/auth-guard'; |
| 112 | +import { Controller } from '@nestjs/common'; |
| 113 | + |
| 114 | +@JwtAuthentication() |
| 115 | +@Controller('me') |
| 116 | +export class MeController { |
| 117 | + // ... |
| 118 | +} |
| 119 | +``` |
0 commit comments