Skip to content

Commit

Permalink
scheduler support & others
Browse files Browse the repository at this point in the history
  • Loading branch information
lanlin committed Jul 27, 2023
1 parent 67a9f1c commit adfb283
Show file tree
Hide file tree
Showing 40 changed files with 1,219 additions and 198 deletions.
11 changes: 7 additions & 4 deletions src/Authentication/Hosted.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public function __construct(Options $options)
/**
* Authenticate your user using Hosted Authentication
*
* @see https://developer.nylas.com/docs/api/#get/oauth/authorize
* @see https://developer.nylas.com/docs/api/v2/#get-/oauth/authorize
* @see https://developer.nylas.com/docs/developer-guide/authentication/authentication-scopes/#nylas-scopes
*
* @param array $params
*
Expand All @@ -62,8 +63,10 @@ public function authenticateUser(array $params): string
V::key('redirect_uri', V::url()),
V::key('response_type', V::in(['code', 'token'])),
V::keyOptional('state', V::stringType()::length(1, 255)),
V::keyOptional('provicer', V::in(['iCloud', 'gmail', 'office365', 'exchange', 'IMAP'])),
V::keyOptional('login_hint', V::email()),
V::keyOptional('redirect_on_error', V::boolType()) // default false
V::keyOptional('redirect_on_error', V::boolType()), // default false
V::keyOptional('disable_provider_selection', V::boolType()) // default false
), $params);

$query = http_build_query($params, '', '&', PHP_QUERY_RFC3986);
Expand All @@ -77,7 +80,7 @@ public function authenticateUser(array $params): string
/**
* Send authorization code. An access token will return as part of the response.
*
* @see https://developer.nylas.com/docs/api/#post/oauth/token
* @see https://developer.nylas.com/docs/api/v2/#post-/oauth/token
*
* @param string $code
*
Expand Down Expand Up @@ -106,7 +109,7 @@ public function sendAuthorizationCode(string $code): array
/**
* Revoke access tokens.
*
* @see https://developer.nylas.com/docs/api/#post/oauth/revoke
* @see https://developer.nylas.com/docs/api/v2/#post-/oauth/revoke
*
* @return array
* @throws GuzzleException
Expand Down
173 changes: 137 additions & 36 deletions src/Authentication/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Nylas\Authentication;

use function is_array;

use Nylas\Utilities\API;
use Nylas\Utilities\Options;
use Nylas\Utilities\Validator as V;
Expand Down Expand Up @@ -43,7 +45,7 @@ public function __construct(Options $options)
/**
* Send Authorization
*
* @see https://developer.nylas.com/docs/api/#post/connect/authorize
* @see https://developer.nylas.com/docs/api/v2/#post-/connect/authorize
*
* @param array $params
*
Expand All @@ -54,17 +56,7 @@ public function sendAuthorization(array $params): array
{
$params['client_id'] = $this->options->getClientId();

V::doValidate(V::keySet(
V::key('name', V::stringType()::notEmpty()),
V::key('provider', V::in(API::PROVIDERS)),
V::key('settings', $this->settingsRules($params)),
V::key('client_id', V::stringType()::notEmpty()),
V::key('email_address', V::email()),
V::keyOptional('scopes', V::stringType()::notEmpty()),

// re-authenticate existing account id
V::keyOptional('reauth_account_id', V::stringType()::notEmpty())
), $params);
V::doValidate($this->getAuthRules($params), $params);

return $this->options
->getSync()
Expand All @@ -77,7 +69,7 @@ public function sendAuthorization(array $params): array
/**
* Exchange the Token
*
* @see https://developer.nylas.com/docs/api/#post/connect/token
* @see https://developer.nylas.com/docs/api/v2/#post-/connect/token
*
* @param string $code
*
Expand Down Expand Up @@ -105,7 +97,7 @@ public function exchangeTheToken(string $code): array
/**
* Detect Provider
*
* @see https://developer.nylas.com/docs/api/#post/connect/detect-provider
* @see https://developer.nylas.com/docs/api/v2/#post-/connect/detect-provider
*
* @param string $email
*
Expand All @@ -131,20 +123,21 @@ public function detectProvider(string $email): array
// ------------------------------------------------------------------------------

/**
* validate settings params
* get auth validate rules
*
* @param array $params
*
* @return V
*/
private function settingsRules(array $params): V
private function getAuthRules(array $params): V
{
$provider = $params['provider'] ?? 'imap';

return match ($provider)
{
'nylas' => $this->nylasProviderRule(),
'gmail' => $this->gmailProviderRule(),
'graph' => $this->graphProviderRule(),
'outlook' => $this->outlookProviderRule(),
'exchange' => $this->exchangeProviderRule(),
'office365' => $this->office365ProviderRule(),
Expand All @@ -158,37 +151,52 @@ private function settingsRules(array $params): V
// ------------------------------------------------------------------------------

/**
* some known providers rules
*
* @return V
*/
private function nylasProviderRule(): V
private function knownProviderRule(): V
{
return V::equals([]);
$settings = V::keySet(V::key('password', V::stringType()::notEmpty()));

return $this->getBaseRules(['aol', 'yahoo', 'icloud', 'hotmail'], $settings);
}

// ------------------------------------------------------------------------------

/**
* outlook provider rules
*
* @return V
*/
private function knownProviderRule(): V
private function outlookProviderRule(): V
{
return V::keySet(V::key('password', V::stringType()::notEmpty()));
$settings = V::keySet(
V::key('username', V::stringType()::notEmpty()),
V::key('password', V::stringType()::notEmpty()),
V::key('exchange_server_host', V::domain())
);

return $this->getBaseRules('outlook', $settings);
}

// ------------------------------------------------------------------------------

/**
* outlook provider rules
* graph provider rule
*
* @return V
*/
private function outlookProviderRule(): V
private function graphProviderRule(): V
{
return V::keySet(
V::key('username', V::stringType()::notEmpty()),
V::key('password', V::stringType()::notEmpty()),
V::key('exchange_server_host', V::domain())
$settings = V::keySet(
V::key('redirect_uri', V::url()),
V::key('microsoft_client_id', V::stringType()::notEmpty()),
V::key('microsoft_client_secret', V::stringType()::notEmpty()),
V::key('microsoft_refresh_token', V::stringType()::notEmpty()),
);

return $this->getBaseRules('graph', $settings);
}

// ------------------------------------------------------------------------------
Expand All @@ -200,12 +208,14 @@ private function outlookProviderRule(): V
*/
private function office365ProviderRule(): V
{
return V::keySet(
$settings = V::keySet(
V::key('redirect_uri', V::url()),
V::key('microsoft_client_id', V::stringType()::notEmpty()),
V::key('microsoft_client_secret', V::stringType()::notEmpty()),
V::key('microsoft_refresh_token', V::stringType()::notEmpty()),
V::key('redirect_uri', V::url())
);

return $this->getBaseRules('office365', $settings);
}

// ------------------------------------------------------------------------------
Expand All @@ -217,7 +227,7 @@ private function office365ProviderRule(): V
*/
private function imapProviderRule(): V
{
return V::keySet(
$settings = V::keySet(
V::key('imap_host', V::domain()),
V::key('imap_port', V::intType()),
V::key('imap_username', V::stringType()::notEmpty()),
Expand All @@ -228,6 +238,8 @@ private function imapProviderRule(): V
V::key('smtp_password', V::stringType()::notEmpty()),
V::key('ssl_required', V::boolType())
);

return $this->getBaseRules('imap', $settings);
}

// ------------------------------------------------------------------------------
Expand All @@ -239,7 +251,7 @@ private function imapProviderRule(): V
*/
private function gmailProviderRule(): V
{
return V::oneOf(
$settings = V::oneOf(
V::keySet(
V::key('google_client_id', V::stringType()::notEmpty()),
V::key('google_client_secret', V::stringType()::notEmpty()),
Expand All @@ -248,16 +260,18 @@ private function gmailProviderRule(): V
V::keySet(V::key('service_account_json', V::keySet(
V::key('type', V::stringType()::notEmpty()),
V::key('project_id', V::stringType()::notEmpty()),
V::key('private_key_id', V::stringType()::notEmpty()),
V::key('private_key', V::stringType()::notEmpty()),
V::key('client_email', V::email()),
V::key('private_key_id', V::stringType()::notEmpty()),
V::key('client_id', V::stringType()::notEmpty()),
V::key('client_email', V::email()),
V::key('auth_uri', V::url()),
V::key('token_uri', V::url()),
V::key('auth_provider_x509_cert_url', V::url()),
V::key('client_x509_cert_url', V::url()),
V::key('auth_provider_x509_cert_url', V::url()),
)))
);

return $this->getBaseRules('gmail', $settings);
}

// ------------------------------------------------------------------------------
Expand All @@ -269,7 +283,7 @@ private function gmailProviderRule(): V
*/
private function exchangeProviderRule(): V
{
return V::oneOf(
$settings = V::oneOf(
V::keySet(
V::key('username', V::stringType()::notEmpty()),
V::key('password', V::stringType()::notEmpty()),
Expand All @@ -281,13 +295,100 @@ private function exchangeProviderRule(): V
V::key('service_account', V::boolType())
),
V::keySet(
V::key('username', V::stringType()::notEmpty()),
V::key('password', V::stringType()::notEmpty()),
V::keyOptional('eas_server_host', V::domain())
),
V::keySet(
V::key('redirect_uri', V::url()),
V::key('service_account', V::boolType()),
V::key('microsoft_client_id', V::stringType()::notEmpty()),
V::key('microsoft_client_secret', V::stringType()::notEmpty()),
V::key('microsoft_refresh_token', V::stringType()::notEmpty()),
V::key('redirect_uri', V::url()),
V::key('service_account', V::boolType())
),
);

return $this->getBaseRules('exchange', $settings);
}

// ------------------------------------------------------------------------------

/**
* @return V
*/
private function nylasProviderRule(): V
{
$scopes = V::oneOf(
V::stringType()::notEmpty(),
V::simpleArray(V::in([
'email',
'email.send',
'email.drafts',
'email.modify',
'email.metadata',
'email.read_only',
'email.folders_and_labels',
'calendar',
'calendar.read_only',
'calendar.free_busy',
'room_resources.read_only',
'contacts',
'contacts.read_only',
]))
);

return V::keySet(
V::key('name', V::stringType()::notEmpty()),
V::key('email', V::stringType()),
V::key('provider', V::equals('nylas')),
V::key('settings', V::equals([])),
V::key('client_id', V::stringType()::notEmpty()),
V::keyOptional('scopes', $scopes),
V::keyOptional('reauth_account_id', V::stringType()::notEmpty())
);
}

// ------------------------------------------------------------------------------

/**
* get base rules
*
* @param array|string $provider
* @param V $settings
*
* @return V
*/
private function getBaseRules(string|array $provider, V $settings): V
{
$scopes = V::oneOf(
V::stringType()::notEmpty(),
V::simpleArray(V::in([
'graph',
'email',
'email.send',
'email.drafts',
'email.modify',
'email.metadata',
'email.read_only',
'email.folders_and_labels',
'calendar',
'calendar.read_only',
'calendar.free_busy',
'room_resources.read_only',
'contacts',
'contacts.read_only',
]))
);

return V::keySet(
V::key('name', V::stringType()::notEmpty()),
V::key('provider', is_array($provider) ? V::in($provider) : V::equals($provider)),
V::key('settings', $settings),
V::key('client_id', V::stringType()::notEmpty()),
V::key('email_address', V::email()),
V::keyOptional('scopes', $scopes),
V::keyOptional('reauth_account_id', V::stringType()::notEmpty())
);
}

// ------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/Calendars/Availability.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(Options $options)
* Check multiple calendars to find available time slots for a single meeting.
* It checks the provider's primary calendar.
*
* @see https://developer.nylas.com/docs/api/#post/calendars/availability
* @see https://developer.nylas.com/docs/api/v2/#post-/calendars/availability
*
* @param array $params
*
Expand All @@ -70,7 +70,7 @@ public function availabilityForASingleMeeting(array $params = []): array
* Use this endpoint to build itineraries where participants with the same availability are combined.
* It checks the provider's primary calendar.
*
* @see https://developer.nylas.com/docs/api/#post/calendars/availability/consecutive
* @see https://developer.nylas.com/docs/api/v2/#post-/calendars/availability/consecutive
*
* @param array $params
*
Expand Down
Loading

0 comments on commit adfb283

Please sign in to comment.