@startuml
caption DDB CMS User authentification (Adgangsplatformen)
'Single Sign On

' Skinparams '
skinparam monochrome true

' Participants
actor Patron
boundary Browser
entity DDBCMS as "DDB CMS"
control login.bib.dk /' "AccessPlatform" '/
boundary BORCHK /' as "Borrower Check" '/
            
boundary CULR
boundary LMS /'as "Library Management System" '/


Patron->Browser: Log in
Browser->DDBCMS: Log in
DDBCMS->login.bib.dk: Request authentification of user

alt User is already logged in (login cookie)
    login.bib.dk->DDBCMS: Return GUID, session token, \nmuncipality ID and \nactive agencies for userID
            note right: If profile was created on login, \nlogin agency is the only returned for userID@Agency
    DDBCMS->Browser: User logged in
else Login process
    login.bib.dk->Browser: Get Agency, userID and PIN
    Browser->Patron: Enter Agency,\nuserID and PIN
    Patron->Browser: Agency, userID and PIN
    Browser->login.bib.dk: \nAgency, userID and PIN

    login.bib.dk->BORCHK: Agency, userID and PIN

    BORCHK->LMS: Authenticate: \nAgency, userID and PIN
    LMS-->BORCHK: TRUE/FALSE

    alt BORCHK: FALSE (User rejected)
        BORCHK->login.bib.dk: User rejected
        login.bib.dk->Browser: Login failed
    else BORCHK: TRUE (User authenticated)
        BORCHK->login.bib.dk: User authenticated
        login.bib.dk->CULR: Get profile for userID@Agency
                               
            'note right: Profile with new GUID for userID@Agency \nis created if user not found in CULR. \nMuncipalityNo requested from LMS
            alt Create user profile \nif not found in CULR
                CULR -> LMS: Get user profile
                CULR <-- LMS: UserID, MunicipalityNo
                CULR->CULR: Create profile (GUID) \nfor agency with MunicipalityNo
            end
        login.bib.dk<--CULR: Return GUID and \nactive agencies for userID
        login.bib.dk->Browser: Set login cookie (User Token)
        login.bib.dk->DDBCMS: Return GUID, session token\nand active agencies for userI
            note right:+ additional info configured (CPR, Local user ID)
        DDBCMS-->Browser: User logged in
            note right: Ready to get more user info from LMS
    end
end
@enduml
