I'm tying to use Domain Driven Design in one of my applications and have some question about user authentication.
I have an aggregate root called User which has Value Objects like UserCredentials, Password, ActivationToken, etc. I also have few domain services for managing users. For example UserRegistration service looks like this:
public interface IUserRegistrationService
{
IEnumerable<string> Register(NewUserRequest request);
}
It checks business rules that are assigned to user registration process and persist user in the database.
Now I want to authenticate user, so I've created UserAuthentication domain service:
public interface UserAuthenticationService
{
IEnumerable<string> Authenticate(AuthRequest request);
}
It takes user from the repository, checks business rules, updates and persists user data changes like LastLoginDate.
But I have some doubts if authentication process belongs to domain itself or it should belong to application service, as for my domain it doesn't matter how user is authenticated. But on the other hand authentication rules, that are checked inside this service, belong to my domain rules, so they're integral part of my domain.
So where do you put authentication in your DDD based appllications and what is your solution to this issue?
1.Generally, authentication and authorization are su-domains in an application. You'd better build an abstraction in application layer/core domain to isolate them.
2.In Identity subdomain, the authentication algorithm could be placed in infrastructure layer:
There are excellent discussion and examples in Implementing Domain Driven Design. The author seperate authentication to an identity subdomain.