การสร้างโมเดลของ Domain Service อาจทำได้ง่ายมาก ทั้งนี้ขึ้นอยู่กับวัตถุประสงค์ของมัน คุณจะต้องตัดสินใจว่า Service ของคุณควรมี Separated Interface [Fowler, P of EAA] หรือไม่ ถ้าใช่ นี่อาจเป็นนิยามของ interface:

package com.saasovation.identityaccess.domain.model.identity;

public interface AuthenticationService {

    public UserDescriptor authenticate(
            TenantId aTenantId,
            String aUsername,
            String aPassword);
}

interface นี้ถูกประกาศไว้ใน Module (9) ซึ่งเป็น Module เดียวกับ Aggregates เฉพาะของ identity เช่น Tenant , User และ Group การทำเช่นนี้ก็เพราะ AuthenticationService เป็น concept ของ identity และในตอนนี้เราจัดวาง concept ทั้งหมดที่เกี่ยวกับ identity ไว้ใน identity Module นิยามของ interface นั้นเรียบง่ายมาก จำเป็นต้องมีเพียง operation เดียวเท่านั้นคือ authenticate()

อีกทางเลือกหนึ่งที่เราต้องตัดสินใจคือจะวาง implementation class ไว้ที่ใด หากคุณใช้ Dependency Inversion Principle (4) หรือ Hexagonal (4) คุณอาจตัดสินใจวาง implementation class ที่ค่อนข้างเป็นเชิงเทคนิคนี้นอก domain model ได้ ตัวอย่างเช่น implementation เชิงเทคนิคอาจถูกจัดเก็บไว้ใน Module ใน Infrastructure Layer

นี่คือ class:

package com.saasovation.identityaccess.infrastructure.services;

import com.saasovation.identityaccess.domain.model.DomainRegistry;
import com.saasovation.identityaccess.domain.model.identity. ↵
AuthenticationService;
import com.saasovation.identityaccess.domain.model.identity.Tenant;
import com.saasovation.identityaccess.domain.model.identity.TenantId;
import com.saasovation.identityaccess.domain.model. ↵
identity.User;
import com.saasovation.identityaccess.domain.model. ↵
identity.UserDescriptor;

public class DefaultEncryptionAuthenticationService
        implements AuthenticationService  {

    public DefaultEncryptionAuthenticationService() {
        super();
    }

    @Override
    public UserDescriptor authenticate(
            TenantId aTenantId,
            String aUsername,
            String aPassword) {
        if (aTenantId == null) {
            throw new IllegalArgumentException(
                    "TenantId must not be null.");
        }
        if (aUsername == null) {
            throw new IllegalArgumentException(
                    "Username must not be null.");
        }
        if (aPassword == null) {
            throw new IllegalArgumentException(
                    "Password must not be null.");
        }

        UserDescriptor userDescriptor = null;

        Tenant tenant =
            DomainRegistry
                .tenantRepository()
                .tenantOfId(aTenantId);

        if (tenant != null && tenant.isActive()) {
            String encryptedPassword =
                DomainRegistry
                    .encryptionService()
                    .encryptedValue(aPassword);

            User user =
                DomainRegistry
                    .userRepository()
                    .userFromAuthenticCredentials(
                            aTenantId,
                            aUsername,
                            encryptedPassword);

            if (user != null && user.isEnabled()) {
                userDescriptor = user.userDescriptor();
            }
        }

        return userDescriptor;
    }
}

method นี้คอย guard ค่า null ที่ถูกส่งเข้ามาเป็นพารามิเตอร์ หากกระบวนการ authentication ล้มเหลวภายใต้เงื่อนไขปกติ UserDescriptor ที่ถูกส่งคืนกลับออกไปก็จะเป็น null

ในการ authenticate เราเริ่มต้นด้วยการพยายามดึง Tenant ออกจาก Repository โดยใช้ identity ของมัน หาก Tenant มีอยู่จริงและยัง active อยู่ ขั้นต่อไปเราก็จะเข้ารหัส password ฉบับ plain text เราทำตรงนี้ทันทีก็เพราะเราจะต้องใช้ password ที่เข้ารหัสแล้ว เพื่อดึง User ออกมา แทนที่จะขอ User ด้วยเพียงแค่ TenantId และ username ที่ตรงกันเท่านั้น เรายังจับคู่กับ password ที่เข้ารหัสแล้วด้วย (ผลลัพธ์ของการเข้ารหัสจะเหมือนเดิมเสมอสำหรับ password ฉบับ plain text สองค่าที่เท่ากัน) Repository ถูกออกแบบมาให้ filter ด้วยทั้งสามค่านี้

หากผู้ใช้ซึ่งเป็นมนุษย์ป้อน tenant identity, username และ password ฉบับ plain text ที่ถูกต้อง ก็จะทำให้เกิดการดึง instance ของ User ตัวที่ตรงกันกลับคืนมา ถึงกระนั้น สิ่งนี้ก็ยังพิสูจน์ไม่ได้อย่างสมบูรณ์ว่าผู้ใช้เป็นของจริง ยังมีข้อกำหนดสุดท้าย อีกหนึ่งข้อที่ยังไม่ได้รับการจัดการอยู่:

• ผู้ใช้จะถูก authenticate ได้ก็ต่อเมื่อผู้ใช้ถูก enable อยู่เท่านั้น

ต่อให้ Repository พบ User instance ที่ผ่าน filter มาแล้ว instance ดังกล่าวก็อาจถูก disable ไปแล้วก็ได้ การเปิดโอกาสให้ disable User ได้ ทำให้ tenant ควบคุมการ authentication ของผู้ใช้ได้ในอีกระดับหนึ่ง ดังนั้น ในขั้นตอนสุดท้าย User instance จะต้องไม่เป็น null และถูก enable อยู่ ซึ่งจะให้ผลลัพธ์เป็น UserDescriptor ที่ถูกสร้างขึ้นจาก User