Vaden Security
VadenSecurity is a security module for the Vaden backend framework, built with Dart. It handles authentication and authorization with zero boilerplate, inspired by the power and simplicity of Spring Security.
It works by scanning your classes using a custom build system, enabling automatic configuration with just one line of code.
Getting Startedβ
- Add the dependency to your
pubspec.yaml
:
dependencies:
vaden_security: any
- Register your module in the
app_module
:
([VadenSecurity])
class AppModule {}
- Create a Security Configuration class:
import 'package:vaden/vaden.dart';
import 'package:vaden_security/vaden_security.dart';
()
class SecurityConfiguration {
()
PasswordEncoder passwordEncoder() {
return BCryptPasswordEncoder(cost: 10);
}
()
JwtService jwtService(ApplicationSettings settings) {
return JwtService.withSettings(settings);
}
()
HttpSecurity httpSecurity() {
return HttpSecurity([
RequestMatcher('/auth/**').permitAll(),
RequestMatcher('/docs/**').permitAll(),
AnyRequest().authenticated(),
]);
}
}
- Add configurations to your
application.yaml
:
security:
secret: AisuSijfdiflkDkldkeokdokDKodk
tokenValidity: 3600
refreshTokenValidity: 604800
issuer: "vaden"
audiences:
- "vaden"
- Implements the
UserDetailsService
interface to load user details:
import 'package:vaden/vaden.dart';
import 'package:vaden_security/vaden_security.dart';
()
class UserDetailsServiceImpl implements UserDetailsService {
Future<UserDetails> loadUserByUsername(String username) async {
...
}
}
Return a UserDetails
object with the user information and roles.
We can retrieve the UserDetails
in any @Controller()
by using the @Context()
in any handler:
('/byUser')
List<Product> getProductsByUser(() UserDetails user) {
return productService.getProductsByUser(user);
}
HttpSecurityβ
The HttpSecurity
class defines a list of rules for your API endpoints,
similar to the behavior found in Spring Security.
Each rule is defined using a RequestMatcher
, which supports path globbing and HTTP method filtering.
RequestMatcher('/admin/**', HttpMethod.get).hasRole('admin');
The matcher supports the following security actions:
Method | Description |
---|---|
permitAll() | Allows any request (no authentication) |
denyAll() | Blocks all requests |
authenticated() | Requires a valid authentication token |
hasRole('role') | Requires the user to have a specific role |
hasAnyRole(['r1', 'r2']) | Requires at least one role from a list |
π§ Exampleβ
()
HttpSecurity httpSecurity() {
return HttpSecurity([
// Public access to auth and docs
RequestMatcher('/auth/**').permitAll(),
RequestMatcher('/docs/**').permitAll(),
// Allow only GET to "/public"
RequestMatcher('/public', HttpMethod.get).permitAll(),
// Restrict DELETE on /admin/** to "admin" role
RequestMatcher('/admin/**', HttpMethod.delete).hasRole('admin'),
// Secure everything else
AnyRequest().authenticated(),
]);
}
Glob Supportβ
The path
parameter accepts Glob patterns. Examples:
/api/**
β matches everything under/api/
/users/*.json
β matches all.json
files in/users/
Roadmapβ
β’ β
JWT Support
β’ π OAuth2 / OpenID Providers
β’ π RBAC / Fine-grained policies
β’ π Session-based auth (Cookie)
β’ π Multi-tenancy support