Merge pull request #7188 from appwrite/refactor-textmagic-change

changes TextMagic to Textmagic in all places and uses email validator
This commit is contained in:
Jake Barnby 2023-11-21 23:03:36 +13:00 committed by GitHub
commit 90c47ea241
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 80 additions and 28 deletions

View file

@ -466,7 +466,7 @@
## Features
- Added Phone Authentication by @TorstenDittmann in https://github.com/appwrite/appwrite/pull/3357
- Added Twilio Support
- Added TextMagic Support
- Added Textmagic Support
- Added Telesign Support
- Added Endpoint to create Phone Session (`POST:/v1/account/sessions/phone`)
- Added Endpoint to confirm Phone Session (`PUT:/v1/account/sessions/phone`)

View file

@ -103,6 +103,16 @@ return [
'description' => 'This method was not fully implemented yet. If you believe this is a mistake, please upgrade your Appwrite server version.',
'code' => 405,
],
Exception::GENERAL_INVALID_EMAIL => [
'name' => Exception::GENERAL_INVALID_EMAIL,
'description' => 'Value must be a valid email address.',
'code' => 400,
],
Exception::GENERAL_INVALID_PHONE => [
'name' => Exception::GENERAL_INVALID_PHONE,
'description' => 'Value must be a valid phone number. Format this number with a leading \'+\' and a country code, e.g., +16175551212.',
'code' => 400,
],
/** User Errors */
Exception::USER_COUNT_EXCEEDED => [

View file

@ -440,7 +440,7 @@ return [
'variables' => [
[
'name' => '_APP_SMS_PROVIDER',
'description' => "Provider used for delivering SMS for Phone authentication. Use the following format: 'sms://[USER]:[SECRET]@[PROVIDER]'.\n\nEnsure `[USER]` and `[SECRET]` are URL encoded if they contain any non-alphanumeric characters.\n\nAvailable providers are twilio, textmagic, telesign, msg91, and vonage.",
'description' => "Provider used for delivering SMS for Phone authentication. Use the following format: 'sms://[USER]:[SECRET]@[PROVIDER]'.\n\nEnsure `[USER]` and `[SECRET]` are URL encoded if they contain any non-alphanumeric characters.\n\nAvailable providers are twilio, Textmagic, telesign, msg91, and vonage.",
'introduction' => '0.15.0',
'default' => '',
'required' => false,

View file

@ -256,7 +256,7 @@ App::post('/v1/messaging/providers/telesign')
});
App::post('/v1/messaging/providers/textmagic')
->desc('Create TextMagic provider')
->desc('Create Textmagic provider')
->groups(['api', 'messaging'])
->label('audits.event', 'provider.create')
->label('audits.resource', 'provider/{response.$id}')
@ -264,7 +264,7 @@ App::post('/v1/messaging/providers/textmagic')
->label('scope', 'providers.write')
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN, APP_AUTH_TYPE_KEY])
->label('sdk.namespace', 'messaging')
->label('sdk.method', 'createTextMagicProvider')
->label('sdk.method', 'createTextmagicProvider')
->label('sdk.description', '/docs/references/messaging/create-textmagic-provider.md')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
@ -689,7 +689,7 @@ App::patch('/v1/messaging/providers/mailgun/:providerId')
->param('name', '', new Text(128), 'Provider name.', true)
->param('enabled', null, new Boolean(), 'Set as enabled.', true)
->param('isEuRegion', null, new Boolean(), 'Set as EU region.', true)
->param('from', '', new Text(256), 'Sender email address.', true)
->param('from', '', new Email(), 'Sender email address.', true)
->param('apiKey', '', new Text(0), 'Mailgun API Key.', true)
->param('domain', '', new Text(0), 'Mailgun Domain.', true)
->inject('queueForEvents')
@ -764,7 +764,7 @@ App::patch('/v1/messaging/providers/sendgrid/:providerId')
->param('name', '', new Text(128), 'Provider name.', true)
->param('enabled', null, new Boolean(), 'Set as enabled.', true)
->param('apiKey', '', new Text(0), 'Sendgrid API key.', true)
->param('from', '', new Text(256), 'Sender email address.', true)
->param('from', '', new Email(), 'Sender email address.', true)
->inject('queueForEvents')
->inject('dbForProject')
->inject('response')
@ -950,7 +950,7 @@ App::patch('/v1/messaging/providers/telesign/:providerId')
});
App::patch('/v1/messaging/providers/textmagic/:providerId')
->desc('Update TextMagic provider')
->desc('Update Textmagic provider')
->groups(['api', 'messaging'])
->label('audits.event', 'provider.update')
->label('audits.resource', 'provider/{response.$id}')
@ -958,7 +958,7 @@ App::patch('/v1/messaging/providers/textmagic/:providerId')
->label('scope', 'providers.write')
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN, APP_AUTH_TYPE_KEY])
->label('sdk.namespace', 'messaging')
->label('sdk.method', 'updateTextMagicProvider')
->label('sdk.method', 'updateTextmagicProvider')
->label('sdk.description', '/docs/references/messaging/update-textmagic-provider.md')
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)

View file

@ -415,6 +415,25 @@ App::post('/v1/users/:userId/targets')
}
}
switch ($providerType) {
case 'email':
$validator = new Email();
if (!$validator->isValid($identifier)) {
throw new Exception(Exception::GENERAL_INVALID_EMAIL);
}
break;
case 'sms':
$validator = new Phone();
if (!$validator->isValid($identifier)) {
throw new Exception(Exception::GENERAL_INVALID_PHONE);
}
break;
case 'push':
break;
default:
throw new Exception(Exception::PROVIDER_INCORRECT_TYPE);
}
$user = $dbForProject->getDocument('users', $userId);
if ($user->isEmpty()) {
@ -1270,6 +1289,27 @@ App::patch('/v1/users/:userId/targets/:targetId')
}
if ($identifier) {
$providerType = $target->getAttribute('providerType');
switch ($providerType) {
case 'email':
$validator = new Email();
if (!$validator->isValid($identifier)) {
throw new Exception(Exception::GENERAL_INVALID_EMAIL);
}
break;
case 'sms':
$validator = new Phone();
if (!$validator->isValid($identifier)) {
throw new Exception(Exception::GENERAL_INVALID_PHONE);
}
break;
case 'push':
break;
default:
throw new Exception(Exception::PROVIDER_INCORRECT_TYPE);
}
$target->setAttribute('identifier', $identifier);
}

View file

@ -55,6 +55,8 @@ class Exception extends \Exception
public const GENERAL_CODES_DISABLED = 'general_codes_disabled';
public const GENERAL_USAGE_DISABLED = 'general_usage_disabled';
public const GENERAL_NOT_IMPLEMENTED = 'general_not_implemented';
public const GENERAL_INVALID_EMAIL = 'general_invalid_email';
public const GENERAL_INVALID_PHONE = 'general_invalid_phone';
/** Users */
public const USER_COUNT_EXCEEDED = 'user_count_exceeded';

View file

@ -17,7 +17,7 @@ use Utopia\Messaging\Adapters\SMS as SMSAdapter;
use Utopia\Messaging\Adapters\SMS\Mock;
use Utopia\Messaging\Adapters\SMS\Msg91;
use Utopia\Messaging\Adapters\SMS\Telesign;
use Utopia\Messaging\Adapters\SMS\TextMagic;
use Utopia\Messaging\Adapters\SMS\Textmagic;
use Utopia\Messaging\Adapters\SMS\Twilio;
use Utopia\Messaging\Adapters\SMS\Vonage;
use Utopia\Messaging\Adapters\Push as PushAdapter;
@ -303,7 +303,7 @@ class Messaging extends Action
return match ($provider->getAttribute('provider')) {
'mock' => new Mock('username', 'password'),
'twilio' => new Twilio($credentials['accountSid'], $credentials['authToken']),
'textmagic' => new TextMagic($credentials['username'], $credentials['apiKey']),
'textmagic' => new Textmagic($credentials['username'], $credentials['apiKey']),
'telesign' => new Telesign($credentials['username'], $credentials['password']),
'msg91' => new Msg91($credentials['senderId'], $credentials['authKey']),
'vonage' => new Vonage($credentials['apiKey'], $credentials['apiSecret']),

View file

@ -1830,8 +1830,8 @@ trait Base
}
}';
case self::$CREATE_TEXTMAGIC_PROVIDER:
return 'mutation createTextMagicProvider($providerId: String!, $name: String!, $from: String!, $username: String!, $apiKey: String!) {
messagingCreateTextMagicProvider(providerId: $providerId, name: $name, from: $from, username: $username, apiKey: $apiKey) {
return 'mutation createTextmagicProvider($providerId: String!, $name: String!, $from: String!, $username: String!, $apiKey: String!) {
messagingCreateTextmagicProvider(providerId: $providerId, name: $name, from: $from, username: $username, apiKey: $apiKey) {
_id
name
provider
@ -1944,8 +1944,8 @@ trait Base
}
}';
case self::$UPDATE_TEXTMAGIC_PROVIDER:
return 'mutation updateTextMagicProvider($providerId: String!, $name: String!, $username: String!, $apiKey: String!) {
messagingUpdateTextMagicProvider(providerId: $providerId, name: $name, username: $username, apiKey: $apiKey) {
return 'mutation updateTextmagicProvider($providerId: String!, $name: String!, $username: String!, $apiKey: String!) {
messagingUpdateTextmagicProvider(providerId: $providerId, name: $name, username: $username, apiKey: $apiKey) {
_id
name
provider

View file

@ -47,7 +47,7 @@ class MessagingTest extends Scope
'password' => 'my-password',
'from' => '+123456789',
],
'TextMagic' => [
'Textmagic' => [
'providerId' => ID::unique(),
'name' => 'Textmagic1',
'username' => 'my-username',
@ -134,7 +134,7 @@ class MessagingTest extends Scope
'username' => 'my-username',
'password' => 'my-password',
],
'TextMagic' => [
'Textmagic' => [
'providerId' => $providers[4]['_id'],
'name' => 'Textmagic2',
'username' => 'my-username',
@ -398,7 +398,7 @@ class MessagingTest extends Scope
'providerType' => 'email',
'userId' => $userId,
'providerId' => $providerId,
'identifier' => 'token',
'identifier' => 'random-email@mail.org',
],
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
@ -409,7 +409,7 @@ class MessagingTest extends Scope
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($userId, $response['body']['data']['usersCreateTarget']['userId']);
$this->assertEquals('token', $response['body']['data']['usersCreateTarget']['identifier']);
$this->assertEquals('random-email@mail.org', $response['body']['data']['usersCreateTarget']['identifier']);
$targetId = $response['body']['data']['usersCreateTarget']['_id'];

View file

@ -80,7 +80,7 @@ class UsersTest extends Scope
'userId' => $user['_id'],
'providerType' => 'email',
'providerId' => $providerId,
'identifier' => 'identifier',
'identifier' => 'random-email@mail.org',
]
];
@ -90,7 +90,7 @@ class UsersTest extends Scope
], $this->getHeaders()), $graphQLPayload);
$this->assertEquals(200, $target['headers']['status-code']);
$this->assertEquals('identifier', $target['body']['data']['usersCreateTarget']['identifier']);
$this->assertEquals('random-email@mail.org', $target['body']['data']['usersCreateTarget']['identifier']);
return $target['body']['data']['usersCreateTarget'];
}
@ -271,7 +271,7 @@ class UsersTest extends Scope
], $this->getHeaders()), $graphQLPayload);
$this->assertEquals(200, $target['headers']['status-code']);
$this->assertEquals('identifier', $target['body']['data']['usersGetTarget']['identifier']);
$this->assertEquals('random-email@mail.org', $target['body']['data']['usersGetTarget']['identifier']);
}
public function testUpdateUserStatus()
@ -470,7 +470,7 @@ class UsersTest extends Scope
'variables' => [
'userId' => $target['userId'],
'targetId' => $target['_id'],
'identifier' => 'newidentifier',
'identifier' => 'random-email1@mail.org',
],
];
@ -480,7 +480,7 @@ class UsersTest extends Scope
], $this->getHeaders()), $graphQLPayload);
$this->assertEquals(200, $target['headers']['status-code']);
$this->assertEquals('newidentifier', $target['body']['data']['usersUpdateTarget']['identifier']);
$this->assertEquals('random-email1@mail.org', $target['body']['data']['usersUpdateTarget']['identifier']);
}
public function testDeleteUserSessions()

View file

@ -319,7 +319,7 @@ trait MessagingBase
'targetId' => ID::unique(),
'providerType' => 'email',
'providerId' => $provider['body']['$id'],
'identifier' => 'my-token',
'identifier' => 'random-email@mail.org',
]);
$this->assertEquals(201, $target['headers']['status-code']);

View file

@ -1245,11 +1245,11 @@ trait UsersBase
'targetId' => ID::unique(),
'providerId' => $provider['body']['$id'],
'providerType' => 'email',
'identifier' => 'my-token',
'identifier' => 'random-email@mail.org',
]);
$this->assertEquals(201, $response['headers']['status-code']);
$this->assertEquals($provider['body']['$id'], $response['body']['providerId']);
$this->assertEquals('my-token', $response['body']['identifier']);
$this->assertEquals('random-email@mail.org', $response['body']['identifier']);
return $response['body'];
}
@ -1262,10 +1262,10 @@ trait UsersBase
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'identifier' => 'my-updated-token',
'identifier' => 'random-email1@mail.org',
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('my-updated-token', $response['body']['identifier']);
$this->assertEquals('random-email1@mail.org', $response['body']['identifier']);
return $response['body'];
}