Page 1 of 1

zimbra-api php delegateAuth

Posted: Fri Jan 26, 2018 5:06 pm
by undertoe
Trying to build a web app to create an appoint as any user.

The following works great in a single user scenario if i have the users password. However I would like to auth as admin and then create the appointment as if the user I pick created it. I was reading about delegateAuth but can't find any examples on how to successfully implement. Can someone help or post an example

Code: Select all

use Zimbra\Enum\AccountBy;
use Zimbra\Enum\AddressType;
use Zimbra\Enum\AlarmAction;
use Zimbra\Enum\FreeBusyStatus;
use Zimbra\Enum\InviteStatus;
use Zimbra\Enum\InviteClass;
use Zimbra\Enum\ParticipationStatus;
use Zimbra\Enum\Transparency;

use Zimbra\Mail\MailFactory;
use Zimbra\Mail\Struct\AlarmInfo;
use Zimbra\Mail\Struct\AlarmTriggerInfo;
use Zimbra\Mail\Struct\CalendarAttendee;
use Zimbra\Mail\Struct\CalOrganizer;
use Zimbra\Mail\Struct\DtTimeInfo;
use Zimbra\Mail\Struct\DurationInfo;
use Zimbra\Mail\Struct\EmailAddrInfo;
use Zimbra\Mail\Struct\InvitationInfo;
use Zimbra\Mail\Struct\InviteComponent;
use Zimbra\Mail\Struct\MimePartInfo;
use Zimbra\Mail\Struct\Msg;

use Zimbra\Struct\AccountSelector;

$api = MailFactory::instance('https://zimbraserver/service/soap');
try {
    $account = new AccountSelector(AccountBy::NAME(), 'test@mailserver.com');
    $api->auth($account, 'xxxx');

    $trigger = new AlarmTriggerInfo();
    $trigger->setRelative(new DurationInfo(true, null, null, null, 5, null, 'START'));
    $alarm = new AlarmInfo(AlarmAction::DISPLAY(), $trigger);

    $comp = new InviteComponent();
    $comp->setName('Meet celebrities for free')
        ->setLocation('My home')
        ->setFreeBusy(FreeBusyStatus::FREE())
        ->setStatus(InviteStatus::COMPLETED())
        ->setCalClass(InviteClass::PUB())
        ->setTransparency(Transparency::OPAQUE())
        ->setIsAllDay(false)
        ->setIsDraft(false)
        ->addAttendee(new CalendarAttendee('test@mailserver.com', null, 'User1'))
        ->setDtStart(new DtTimeInfo('20180130T160000', 'America/New_York'))
        ->setDtEnd(new DtTimeInfo('20180130T170000', 'America/New_York'))
        ->addAlarm($alarm);

    $inv = new InvitationInfo();
    $inv->setInviteComponent($comp);

    $mp = new MimePartInfo(null, 'multipart/alternative');
    $mp->addMimePart(new MimePartInfo(null, 'text/plain', 'Meet celebrities for free'));

    $msg = new Msg();
    $msg->setSubject('Meet celebrities for free')
        ->setFolderId('10')
        ->addEmail(new EmailAddrInfo('test@mailserver.com', AddressType::TO(), 'User1'))
        ->setInvite($inv)
        ->setMimePart($mp);
    $result = $api->createAppointment($msg);
    var_dump($result);
}
catch(Exception $ex) {
    $client = $api->getClient();
    echo $client->lastResponse();
}

Re: zimbra-api php delegateAuth

Posted: Wed Jan 31, 2018 4:36 pm
by bnystrom
To get a delegate auth token, you need to make a SOAP request with a <DelegateAuthRequest/> with an account name you choose. In this request you must specify an admin authentication token in the SOAP Envelope header.

I'm not sure how the syntax would work using the zimbra-api library, but I'm sure you must call the `auth` function with the admin credentials then some delegateAuth function with the user credentials. Then make the call on behalf of the user. I hope this helps. Good luck :)

Re: zimbra-api php delegateAuth

Posted: Wed Jan 31, 2018 6:27 pm
by undertoe
Sorry forgot to update it. i ended up figuring it out. Here is the solution if anyone else needs it

You need to generate a preauth token on your zimbra server
https://wiki.zimbra.com/wiki/Preauth

Code: Select all

$account = new AccountSelector(AccountBy::NAME(), 'test@domain.com');
$preAuth = new PreAuth(time() * 1000, null, 0);
$preAuth->computeValue($account, INSERT_GENERATE_PREAUTH_TOKEN_HERE );
$api = MailFactory::instance('https://domain.com/service/soap');
$result = $api->auth($account, null, $preAuth);