import ch.nevis.idm.client.IdmRestClient
import ch.nevis.idm.client.IdmRestClientFactory


String login_info_update_dto = '''
{
    "success": true,
    "credentialExtId": "{{credentialExtId}}"
}
'''

IdmRestClient idmRestClient = IdmRestClientFactory.get(parameters)
def sess = request.getAuthSession(true)

String baseUrl = parameters.get("baseUrl")
String clientExtId = parameters.get("clientExtId")
String endPoint = "$baseUrl/api/core/v1"

String userExtId = sess.getAttribute("ch.nevis.idm.User.extId")
String linkingCredentialExtId = sess.getAttribute("agov.eid.linkingCredentialExtId")

String requestUrl = "$endPoint/$clientExtId/users/$userExtId/login-info"

String restRequest = login_info_update_dto.replaceAll("\\{\\{credentialExtId}}", linkingCredentialExtId)

try {
    idmRestClient.post(requestUrl, restRequest)
    
}catch(Exception e) {
    LOG.error("Failed to Update Linking Credential info: ${e}")
    response.setResult('error')
    return
}

response.setResult('ok')
return