Skip to content

Commit

Permalink
Improve SMTP oAuth implementation - closes #4084
Browse files Browse the repository at this point in the history
  • Loading branch information
freescout-help-desk committed Jun 19, 2024
1 parent bd59fa5 commit 4fe2a93
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
7 changes: 7 additions & 0 deletions app/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,13 @@ public function oauthGetParam($param)
return $this->meta['oauth'][$param] ?? '';
}

public function outOauthEnabled()
{
return $this->oauthEnabled()
&& !strstr($this->out_username, '@')
&& stristr($this->out_server, '.office365.com');
}

public function setEmailAttribute($value)
{
if ($value) {
Expand Down
6 changes: 3 additions & 3 deletions app/Misc/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ public static function setMailDriver($mailbox = null, $user_from = null, $conver
{
if ($mailbox) {
// Configure mail driver according to Mailbox settings.
$oauth = $mailbox->oauthEnabled();
$oauth = $mailbox->outOauthEnabled();

// Refresh Access Token.
if ($oauth && !strstr($mailbox->out_username, '@')) {
if ($oauth) {
if ((strtotime($mailbox->oauthGetParam('issued_on')) + (int)$mailbox->oauthGetParam('expires_in')) < time()) {
// Try to get an access token (using the authorization code grant)
$token_data = \MailHelper::oauthGetAccessToken(\MailHelper::OAUTH_PROVIDER_MICROSOFT, [
Expand Down Expand Up @@ -134,7 +134,7 @@ public static function setMailDriver($mailbox = null, $user_from = null, $conver
if ($mailbox->out_method == Mailbox::OUT_METHOD_SMTP) {
\Config::set('mail.host', $mailbox->out_server);
\Config::set('mail.port', $mailbox->out_port);
if ($oauth && !strstr($mailbox->out_username, '@')) {
if ($oauth) {
\Config::set('mail.auth_mode', 'XOAUTH2');
\Config::set('mail.username', $mailbox->email);
\Config::set('mail.password', $mailbox->oauthGetParam('a_token'));
Expand Down
10 changes: 5 additions & 5 deletions resources/views/mailboxes/connection.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@
</div>
</div>
@php
$oauth_fully_enabled = ($mailbox->oauthEnabled() && strstr($mailbox->out_username, '@'));
$out_oauth_enabled = $mailbox->outOauthEnabled();
@endphp
<div class="form-group{{ $errors->has('out_username') ? ' has-error' : '' }}">
<label for="out_username" class="col-sm-2 control-label">{{ __('Username') }}</label>

<div class="col-sm-6">
<input id="out_username" type="text" class="form-control input-sized @if ($oauth_fully_enabled) disabled @endif" name="out_username" value="{{ old('out_username', $mailbox->out_username) }}" maxlength="100" @if ($mailbox->out_method == App\Mailbox::OUT_METHOD_SMTP) @endif autofocus {{-- This added to prevent autocomplete in Chrome --}}autocomplete="new-password" @if ($oauth_fully_enabled) readonly @endif>
<input id="out_username" type="text" class="form-control input-sized @if ($out_oauth_enabled) disabled @endif" name="out_username" value="{{ old('out_username', $mailbox->out_username) }}" maxlength="100" @if ($mailbox->out_method == App\Mailbox::OUT_METHOD_SMTP) @endif autofocus {{-- This added to prevent autocomplete in Chrome --}}autocomplete="new-password" @if ($out_oauth_enabled) readonly @endif>

@include('partials/field_error', ['field'=>'out_username'])
</div>
Expand All @@ -100,15 +100,15 @@
<label for="out_password" class="col-sm-2 control-label">{{ __('Password') }}</label>

<div class="col-sm-6">
<input id="out_password" type="password" class="form-control input-sized @if ($oauth_fully_enabled) disabled @endif" name="out_password" value="{{ old('out_password', $mailbox->outPasswordSafe()) }}" maxlength="255" @if ($mailbox->out_method == App\Mailbox::OUT_METHOD_SMTP) @endif autofocus {{-- This added to prevent autocomplete in Chrome --}}autocomplete="new-password" @if ($oauth_fully_enabled) readonly @endif>
<input id="out_password" type="password" class="form-control input-sized @if ($out_oauth_enabled) disabled @endif" name="out_password" value="{{ old('out_password', $mailbox->outPasswordSafe()) }}" maxlength="255" @if ($mailbox->out_method == App\Mailbox::OUT_METHOD_SMTP) @endif autofocus {{-- This added to prevent autocomplete in Chrome --}}autocomplete="new-password" @if ($out_oauth_enabled) readonly @endif>

<p class="form-help">
<small @if ($mailbox->oauthGetParam('provider') == \MailHelper::OAUTH_PROVIDER_MICROSOFT) class="text-success" @endif>Microsoft Exchange</small>
<small @if ($mailbox->oauthGetParam('provider') == \MailHelper::OAUTH_PROVIDER_MICROSOFT && $out_oauth_enabled) class="text-success" @endif>Microsoft Exchange</small>
@if (!$mailbox->oauthEnabled())
@if ($mailbox->out_username && $mailbox->out_password && !strstr($mailbox->out_username, '@'))
– <a href="{{ route('mailboxes.oauth', ['id' => $mailbox->id, 'provider' => \MailHelper::OAUTH_PROVIDER_MICROSOFT, 'in_out' => 'out']) }}" target="_blank">{{ __('Connect') }}</a>
@endif
@elseif ($mailbox->oauthGetParam('provider') == \MailHelper::OAUTH_PROVIDER_MICROSOFT && $oauth_fully_enabled)
@elseif ($mailbox->oauthGetParam('provider') == \MailHelper::OAUTH_PROVIDER_MICROSOFT && $out_oauth_enabled)
– <a href="{{ route('mailboxes.oauth_disconnect', ['id' => $mailbox->id, 'provider' => \MailHelper::OAUTH_PROVIDER_MICROSOFT, 'in_out' => 'out']) }}">{{ __('Disconnect') }}</a>
@endif
<small>(<a href="{{ config('app.freescout_repo') }}/wiki/Connect-FreeScout-to-Microsoft-365-Exchange-via-oAuth" target="_blank">{{ __('Help') }}</a>)</small>
Expand Down

0 comments on commit 4fe2a93

Please sign in to comment.