appwrite/src/Appwrite/Auth/Auth.php

469 lines
13 KiB
PHP
Raw Normal View History

2019-05-09 06:54:39 +00:00
<?php
namespace Appwrite\Auth;
2019-05-09 06:54:39 +00:00
use Appwrite\Auth\Hash\Argon2;
use Appwrite\Auth\Hash\Bcrypt;
use Appwrite\Auth\Hash\Md5;
use Appwrite\Auth\Hash\Phpass;
use Appwrite\Auth\Hash\Scrypt;
use Appwrite\Auth\Hash\Scryptmodified;
use Appwrite\Auth\Hash\Sha;
use Utopia\Database\Database;
2021-09-30 10:32:24 +00:00
use Utopia\Database\Document;
use Utopia\Database\DateTime;
2022-12-14 16:04:06 +00:00
use Utopia\Database\Helpers\Role;
2021-10-07 19:27:23 +00:00
use Utopia\Database\Validator\Authorization;
2022-10-14 11:23:20 +00:00
use Utopia\Database\Validator\Roles;
2019-05-09 06:54:39 +00:00
class Auth
{
2022-06-14 11:08:54 +00:00
public const SUPPORTED_ALGOS = [
'argon2',
'bcrypt',
'md5',
'sha',
'phpass',
'scrypt',
2022-06-16 09:21:35 +00:00
'scryptMod',
'plaintext'
2022-05-04 14:37:37 +00:00
];
2022-06-14 11:08:54 +00:00
public const DEFAULT_ALGO = 'argon2';
public const DEFAULT_ALGO_OPTIONS = ['type' => 'argon2', 'memoryCost' => 2048, 'timeCost' => 4, 'threads' => 3];
2022-05-04 14:37:37 +00:00
2019-05-09 06:54:39 +00:00
/**
* User Roles.
2019-05-09 06:54:39 +00:00
*/
2022-08-03 09:52:34 +00:00
public const USER_ROLE_ANY = 'any';
public const USER_ROLE_GUESTS = 'guests';
public const USER_ROLE_USERS = 'users';
2022-06-02 13:03:37 +00:00
public const USER_ROLE_ADMIN = 'admin';
public const USER_ROLE_DEVELOPER = 'developer';
public const USER_ROLE_OWNER = 'owner';
2022-08-15 07:20:10 +00:00
public const USER_ROLE_APPS = 'apps';
2022-06-02 13:03:37 +00:00
public const USER_ROLE_SYSTEM = 'system';
2019-05-09 06:54:39 +00:00
/**
* Token Types.
2019-05-09 06:54:39 +00:00
*/
2022-06-02 13:03:37 +00:00
public const TOKEN_TYPE_LOGIN = 1; // Deprecated
public const TOKEN_TYPE_VERIFICATION = 2;
public const TOKEN_TYPE_RECOVERY = 3;
public const TOKEN_TYPE_INVITE = 4;
public const TOKEN_TYPE_MAGIC_URL = 5;
2022-06-08 09:00:38 +00:00
public const TOKEN_TYPE_PHONE = 6;
2019-05-09 06:54:39 +00:00
2021-02-19 10:02:02 +00:00
/**
* Session Providers.
*/
2022-06-02 13:03:37 +00:00
public const SESSION_PROVIDER_EMAIL = 'email';
public const SESSION_PROVIDER_ANONYMOUS = 'anonymous';
public const SESSION_PROVIDER_MAGIC_URL = 'magic-url';
2022-06-08 09:00:38 +00:00
public const SESSION_PROVIDER_PHONE = 'phone';
2021-02-19 10:02:02 +00:00
2019-05-09 06:54:39 +00:00
/**
* Token Expiration times.
2019-05-09 06:54:39 +00:00
*/
2022-06-02 13:03:37 +00:00
public const TOKEN_EXPIRATION_LOGIN_LONG = 31536000; /* 1 year */
public const TOKEN_EXPIRATION_LOGIN_SHORT = 3600; /* 1 hour */
public const TOKEN_EXPIRATION_RECOVERY = 3600; /* 1 hour */
public const TOKEN_EXPIRATION_CONFIRM = 3600 * 24 * 7; /* 7 days */
2022-06-08 09:00:38 +00:00
public const TOKEN_EXPIRATION_PHONE = 60 * 15; /* 15 minutes */
2019-05-09 06:54:39 +00:00
/**
* @var string
*/
public static $cookieName = 'a_session';
2019-05-09 06:54:39 +00:00
/**
* User Unique ID.
2019-05-09 06:54:39 +00:00
*
2020-11-18 22:08:54 +00:00
* @var string
2019-05-09 06:54:39 +00:00
*/
2020-11-18 22:08:54 +00:00
public static $unique = '';
2019-05-09 06:54:39 +00:00
/**
* User Secret Key.
2019-05-09 06:54:39 +00:00
*
* @var string
*/
public static $secret = '';
2019-05-09 06:54:39 +00:00
/**
* Set Cookie Name.
2019-05-09 06:54:39 +00:00
*
* @param $string
*
2019-05-09 06:54:39 +00:00
* @return string
*/
public static function setCookieName($string)
2019-05-09 06:54:39 +00:00
{
return self::$cookieName = $string;
}
/**
* Encode Session.
2019-05-09 06:54:39 +00:00
*
2020-11-18 22:08:54 +00:00
* @param string $id
2019-05-09 06:54:39 +00:00
* @param string $secret
*
2019-05-09 06:54:39 +00:00
* @return string
*/
public static function encodeSession($id, $secret)
2019-05-09 06:54:39 +00:00
{
return \base64_encode(\json_encode([
2019-05-09 06:54:39 +00:00
'id' => $id,
'secret' => $secret,
]));
}
/**
* Decode Session.
2019-05-09 06:54:39 +00:00
*
* @param string $session
*
2019-05-09 06:54:39 +00:00
* @return array
*
2019-05-09 06:54:39 +00:00
* @throws \Exception
*/
public static function decodeSession($session)
2019-05-09 06:54:39 +00:00
{
$session = \json_decode(\base64_decode($session), true);
$default = ['id' => null, 'secret' => ''];
2019-05-09 06:54:39 +00:00
if (!\is_array($session)) {
2019-05-09 06:54:39 +00:00
return $default;
}
return \array_merge($default, $session);
2019-05-09 06:54:39 +00:00
}
/**
* Encode.
2019-05-09 06:54:39 +00:00
*
* One-way encryption
*
* @param $string
*
2019-05-09 06:54:39 +00:00
* @return string
*/
public static function hash(string $string)
2019-05-09 06:54:39 +00:00
{
return \hash('sha256', $string);
2019-05-09 06:54:39 +00:00
}
/**
* Password Hash.
2019-05-09 06:54:39 +00:00
*
* One way string hashing for user passwords
*
* @param string $string
* @param string $algo hashing algorithm to use
2022-06-21 13:59:52 +00:00
* @param array $options algo-specific options
*
* @return bool|string|null
2019-05-09 06:54:39 +00:00
*/
2022-06-21 13:59:52 +00:00
public static function passwordHash(string $string, string $algo, array $options = [])
2019-05-09 06:54:39 +00:00
{
2022-05-05 11:53:27 +00:00
// Plain text not supported, just an alias. Switch to recommended algo
2022-06-14 11:08:54 +00:00
if ($algo === 'plaintext') {
2022-05-05 11:53:27 +00:00
$algo = Auth::DEFAULT_ALGO;
$options = Auth::DEFAULT_ALGO_OPTIONS;
}
if (!\in_array($algo, Auth::SUPPORTED_ALGOS)) {
2022-05-05 11:53:27 +00:00
throw new \Exception('Hashing algorithm \'' . $algo . '\' is not supported.');
}
switch ($algo) {
case 'argon2':
$hasher = new Argon2($options);
return $hasher->hash($string);
case 'bcrypt':
$hasher = new Bcrypt($options);
return $hasher->hash($string);
case 'md5':
$hasher = new Md5($options);
return $hasher->hash($string);
case 'sha':
$hasher = new Sha($options);
return $hasher->hash($string);
case 'phpass':
$hasher = new Phpass($options);
return $hasher->hash($string);
case 'scrypt':
$hasher = new Scrypt($options);
return $hasher->hash($string);
2022-06-16 09:21:35 +00:00
case 'scryptMod':
$hasher = new Scryptmodified($options);
return $hasher->hash($string);
default:
throw new \Exception('Hashing algorithm \'' . $algo . '\' is not supported.');
}
2019-05-09 06:54:39 +00:00
}
/**
* Password verify.
2019-05-09 06:54:39 +00:00
*
* @param string $plain
* @param string $hash
* @param string $algo hashing algorithm used to hash
2022-06-21 13:59:52 +00:00
* @param array $options algo-specific options
*
2019-05-09 06:54:39 +00:00
* @return bool
*/
2022-06-21 13:59:52 +00:00
public static function passwordVerify(string $plain, string $hash, string $algo, array $options = [])
2019-05-09 06:54:39 +00:00
{
2022-05-05 11:53:27 +00:00
// Plain text not supported, just an alias. Switch to recommended algo
2022-06-14 11:08:54 +00:00
if ($algo === 'plaintext') {
2022-05-05 11:53:27 +00:00
$algo = Auth::DEFAULT_ALGO;
$options = Auth::DEFAULT_ALGO_OPTIONS;
}
if (!\in_array($algo, Auth::SUPPORTED_ALGOS)) {
throw new \Exception('Hashing algorithm \'' . $algo . '\' is not supported.');
}
2022-05-05 11:53:27 +00:00
switch ($algo) {
case 'argon2':
$hasher = new Argon2($options);
return $hasher->verify($plain, $hash);
case 'bcrypt':
$hasher = new Bcrypt($options);
return $hasher->verify($plain, $hash);
case 'md5':
$hasher = new Md5($options);
return $hasher->verify($plain, $hash);
case 'sha':
$hasher = new Sha($options);
return $hasher->verify($plain, $hash);
case 'phpass':
$hasher = new Phpass($options);
return $hasher->verify($plain, $hash);
case 'scrypt':
$hasher = new Scrypt($options);
return $hasher->verify($plain, $hash);
2022-06-16 09:21:35 +00:00
case 'scryptMod':
$hasher = new Scryptmodified($options);
return $hasher->verify($plain, $hash);
default:
throw new \Exception('Hashing algorithm \'' . $algo . '\' is not supported.');
}
2019-05-09 06:54:39 +00:00
}
/**
* Password Generator.
2019-05-09 06:54:39 +00:00
*
* Generate random password string
*
* @param int $length
*
2019-05-09 06:54:39 +00:00
* @return string
*/
2022-05-23 14:54:50 +00:00
public static function passwordGenerator(int $length = 20): string
2019-05-09 06:54:39 +00:00
{
return \bin2hex(\random_bytes($length));
2019-05-09 06:54:39 +00:00
}
/**
* Token Generator.
2019-05-09 06:54:39 +00:00
*
* Generate random password string
*
* @param int $length
*
2019-05-09 06:54:39 +00:00
* @return string
*/
2022-05-23 14:54:50 +00:00
public static function tokenGenerator(int $length = 128): string
2019-05-09 06:54:39 +00:00
{
return \bin2hex(\random_bytes($length));
2019-05-09 06:54:39 +00:00
}
/**
* Code Generator.
*
* Generate random code string
*
* @param int $length
*
* @return string
*/
public static function codeGenerator(int $length = 6): string
{
2022-08-14 18:57:23 +00:00
$value = '';
for ($i = 0; $i < $length; $i++) {
2022-08-14 19:01:58 +00:00
$value .= random_int(0, 9);
2022-08-14 18:57:23 +00:00
}
2022-08-14 19:01:58 +00:00
2022-08-14 18:57:23 +00:00
return $value;
}
2019-05-09 06:54:39 +00:00
/**
* Verify token and check that its not expired.
2019-05-09 06:54:39 +00:00
*
* @param array $tokens
* @param int $type
2019-05-09 06:54:39 +00:00
* @param string $secret
*
2019-12-28 16:37:39 +00:00
* @return bool|string
2019-05-09 06:54:39 +00:00
*/
public static function tokenVerify(array $tokens, int $type, string $secret)
2019-05-09 06:54:39 +00:00
{
2022-06-08 09:00:38 +00:00
foreach ($tokens as $token) {
/** @var Document $token */
2022-05-23 14:54:50 +00:00
if (
$token->isSet('type') &&
$token->isSet('secret') &&
$token->isSet('expire') &&
$token->getAttribute('type') == $type &&
$token->getAttribute('secret') === self::hash($secret) &&
2022-08-15 19:18:37 +00:00
DateTime::formatTz($token->getAttribute('expire')) >= DateTime::formatTz(DateTime::now())
2022-05-23 14:54:50 +00:00
) {
return (string)$token->getId();
2019-05-09 06:54:39 +00:00
}
}
return false;
}
2022-06-08 09:00:38 +00:00
public static function phoneTokenVerify(array $tokens, string $secret)
{
foreach ($tokens as $token) {
/** @var Document $token */
if (
$token->isSet('type') &&
$token->isSet('secret') &&
$token->isSet('expire') &&
$token->getAttribute('type') == Auth::TOKEN_TYPE_PHONE &&
2022-09-22 22:25:17 +00:00
$token->getAttribute('secret') === self::hash($secret) &&
2022-08-15 19:18:37 +00:00
DateTime::formatTz($token->getAttribute('expire')) >= DateTime::formatTz(DateTime::now())
2022-06-08 09:00:38 +00:00
) {
return (string) $token->getId();
}
}
return false;
}
2021-02-19 12:12:47 +00:00
/**
* Verify session and check that its not expired.
*
* @param array $sessions
* @param string $secret
2022-11-04 09:50:59 +00:00
* @param string $expires
2021-02-19 12:12:47 +00:00
*
* @return bool|string
*/
2022-11-04 09:50:59 +00:00
public static function sessionVerify(array $sessions, string $secret, int $expires)
2021-02-19 12:12:47 +00:00
{
2022-06-08 09:00:38 +00:00
foreach ($sessions as $session) {
/** @var Document $session */
2022-05-23 14:54:50 +00:00
if (
$session->isSet('secret') &&
2021-02-19 12:12:47 +00:00
$session->isSet('provider') &&
$session->getAttribute('secret') === self::hash($secret) &&
2022-11-04 09:50:59 +00:00
DateTime::formatTz(DateTime::addSeconds(new \DateTime($session->getCreatedAt()), $expires)) >= DateTime::formatTz(DateTime::now())
2022-05-23 14:54:50 +00:00
) {
2022-07-06 16:13:30 +00:00
return $session->getId();
2021-02-19 12:12:47 +00:00
}
}
return false;
}
/**
* Is Privileged User?
2021-10-05 19:23:04 +00:00
*
* @param array $roles
2021-10-05 19:23:04 +00:00
*
* @return bool
*/
2021-03-01 21:04:53 +00:00
public static function isPrivilegedUser(array $roles): bool
{
2021-10-05 19:23:04 +00:00
if (
2022-08-03 09:52:34 +00:00
in_array(self::USER_ROLE_OWNER, $roles) ||
in_array(self::USER_ROLE_DEVELOPER, $roles) ||
in_array(self::USER_ROLE_ADMIN, $roles)
) {
return true;
}
return false;
}
/**
* Is App User?
2021-10-05 19:23:04 +00:00
*
* @param array $roles
2021-10-05 19:23:04 +00:00
*
* @return bool
*/
public static function isAppUser(array $roles): bool
{
2022-08-15 07:20:10 +00:00
if (in_array(self::USER_ROLE_APPS, $roles)) {
return true;
}
return false;
}
/**
* Returns all roles for a user.
2021-10-05 19:23:04 +00:00
*
* @param Document $user
* @return array
*/
public static function getRoles(Document $user): array
{
2021-09-03 15:15:42 +00:00
$roles = [];
if (!self::isPrivilegedUser(Authorization::getRoles()) && !self::isAppUser(Authorization::getRoles())) {
2021-09-03 15:07:09 +00:00
if ($user->getId()) {
2022-08-19 04:04:33 +00:00
$roles[] = Role::user($user->getId())->toString();
$roles[] = Role::users()->toString();
$emailVerified = $user->getAttribute('emailVerification', false);
$phoneVerified = $user->getAttribute('phoneVerification', false);
if ($emailVerified || $phoneVerified) {
2022-10-14 11:23:20 +00:00
$roles[] = Role::user($user->getId(), Roles::DIMENSION_VERIFIED)->toString();
$roles[] = Role::users(Roles::DIMENSION_VERIFIED)->toString();
} else {
2022-10-14 11:23:20 +00:00
$roles[] = Role::user($user->getId(), Roles::DIMENSION_UNVERIFIED)->toString();
$roles[] = Role::users(Roles::DIMENSION_UNVERIFIED)->toString();
}
2021-09-03 15:07:09 +00:00
} else {
2022-08-19 04:04:33 +00:00
return [Role::guests()->toString()];
2021-09-03 15:07:09 +00:00
}
}
foreach ($user->getAttribute('memberships', []) as $node) {
if (!isset($node['confirm']) || !$node['confirm']) {
continue;
}
if (isset($node['$id']) && isset($node['teamId'])) {
2022-08-19 04:04:33 +00:00
$roles[] = Role::team($node['teamId'])->toString();
$roles[] = Role::member($node['$id'])->toString();
if (isset($node['roles'])) {
foreach ($node['roles'] as $nodeRole) { // Set all team roles
$roles[] = Role::team($node['teamId'], $nodeRole)->toString();
}
}
}
}
return $roles;
}
2022-06-08 09:00:38 +00:00
public static function isAnonymousUser(Document $user): bool
{
return (is_null($user->getAttribute('email'))
|| is_null($user->getAttribute('phone'))
) && is_null($user->getAttribute('password'));
}
}