1
0
Fork 0
mirror of synced 2024-05-19 12:12:36 +12:00
appwrite/src/Appwrite/Auth/Auth.php

303 lines
6.9 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
namespace Appwrite\Auth;
2019-05-09 18:54:39 +12:00
2021-09-30 23:32:24 +13:00
use Utopia\Database\Document;
2021-10-08 08:27:23 +13:00
use Utopia\Database\Validator\Authorization;
2019-05-09 18:54:39 +12:00
class Auth
{
/**
* User Roles.
2019-05-09 18:54:39 +12:00
*/
2021-06-12 06:23:16 +12:00
const USER_ROLE_ALL = 'all';
2021-01-14 03:11:07 +13:00
const USER_ROLE_GUEST = 'guest';
const USER_ROLE_MEMBER = 'member';
const USER_ROLE_ADMIN = 'admin';
const USER_ROLE_DEVELOPER = 'developer';
const USER_ROLE_OWNER = 'owner';
const USER_ROLE_APP = 'app';
const USER_ROLE_SYSTEM = 'system';
2019-05-09 18:54:39 +12:00
/**
* Token Types.
2019-05-09 18:54:39 +12:00
*/
2021-02-20 01:12:47 +13:00
const TOKEN_TYPE_LOGIN = 1; // Deprecated
2020-01-12 13:20:35 +13:00
const TOKEN_TYPE_VERIFICATION = 2;
const TOKEN_TYPE_RECOVERY = 3;
const TOKEN_TYPE_INVITE = 4;
2021-08-30 22:44:52 +12:00
const TOKEN_TYPE_MAGIC_URL = 5;
2019-05-09 18:54:39 +12:00
2021-02-19 23:02:02 +13:00
/**
* Session Providers.
*/
2021-02-20 01:12:47 +13:00
const SESSION_PROVIDER_EMAIL = 'email';
const SESSION_PROVIDER_ANONYMOUS = 'anonymous';
const SESSION_PROVIDER_MAGIC_URL = 'magic-url';
2021-02-19 23:02:02 +13:00
2019-05-09 18:54:39 +12:00
/**
* Token Expiration times.
2019-05-09 18:54:39 +12:00
*/
2019-12-27 02:15:47 +13:00
const TOKEN_EXPIRATION_LOGIN_LONG = 31536000; /* 1 year */
const TOKEN_EXPIRATION_LOGIN_SHORT = 3600; /* 1 hour */
const TOKEN_EXPIRATION_RECOVERY = 3600; /* 1 hour */
const TOKEN_EXPIRATION_CONFIRM = 3600 * 24 * 7; /* 7 days */
2019-05-09 18:54:39 +12:00
/**
* @var string
*/
public static $cookieName = 'a_session';
2019-05-09 18:54:39 +12:00
/**
* User Unique ID.
2019-05-09 18:54:39 +12:00
*
2020-11-19 11:08:54 +13:00
* @var string
2019-05-09 18:54:39 +12:00
*/
2020-11-19 11:08:54 +13:00
public static $unique = '';
2019-05-09 18:54:39 +12:00
/**
* User Secret Key.
2019-05-09 18:54:39 +12:00
*
* @var string
*/
public static $secret = '';
2019-05-09 18:54:39 +12:00
/**
* Set Cookie Name.
2019-05-09 18:54:39 +12:00
*
* @param $string
*
2019-05-09 18:54:39 +12:00
* @return string
*/
public static function setCookieName($string)
2019-05-09 18:54:39 +12:00
{
return self::$cookieName = $string;
}
/**
* Encode Session.
2019-05-09 18:54:39 +12:00
*
2020-11-19 11:08:54 +13:00
* @param string $id
2019-05-09 18:54:39 +12:00
* @param string $secret
*
2019-05-09 18:54:39 +12:00
* @return string
*/
public static function encodeSession($id, $secret)
2019-05-09 18:54:39 +12:00
{
return \base64_encode(\json_encode([
2019-05-09 18:54:39 +12:00
'id' => $id,
'secret' => $secret,
]));
}
/**
* Decode Session.
2019-05-09 18:54:39 +12:00
*
* @param string $session
*
2019-05-09 18:54:39 +12:00
* @return array
*
2019-05-09 18:54:39 +12:00
* @throws \Exception
*/
public static function decodeSession($session)
2019-05-09 18:54:39 +12:00
{
$session = \json_decode(\base64_decode($session), true);
$default = ['id' => null, 'secret' => ''];
2019-05-09 18:54:39 +12:00
if (!\is_array($session)) {
2019-05-09 18:54:39 +12:00
return $default;
}
return \array_merge($default, $session);
2019-05-09 18:54:39 +12:00
}
/**
* Encode.
2019-05-09 18:54:39 +12:00
*
* One-way encryption
*
* @param $string
*
2019-05-09 18:54:39 +12:00
* @return string
*/
public static function hash(string $string)
2019-05-09 18:54:39 +12:00
{
return \hash('sha256', $string);
2019-05-09 18:54:39 +12:00
}
/**
* Password Hash.
2019-05-09 18:54:39 +12:00
*
* One way string hashing for user passwords
*
* @param $string
*
* @return bool|string|null
2019-05-09 18:54:39 +12:00
*/
public static function passwordHash($string)
2019-05-09 18:54:39 +12:00
{
return \password_hash($string, PASSWORD_BCRYPT, array('cost' => 8));
2019-05-09 18:54:39 +12:00
}
/**
* Password verify.
2019-05-09 18:54:39 +12:00
*
* @param $plain
* @param $hash
*
2019-05-09 18:54:39 +12:00
* @return bool
*/
public static function passwordVerify($plain, $hash)
2019-05-09 18:54:39 +12:00
{
return \password_verify($plain, $hash);
2019-05-09 18:54:39 +12:00
}
/**
* Password Generator.
2019-05-09 18:54:39 +12:00
*
* Generate random password string
*
* @param int $length
*
2019-05-09 18:54:39 +12:00
* @return string
*
2019-05-09 18:54:39 +12:00
* @throws \Exception
*/
public static function passwordGenerator(int $length = 20):string
2019-05-09 18:54:39 +12:00
{
return \bin2hex(\random_bytes($length));
2019-05-09 18:54:39 +12:00
}
/**
* Token Generator.
2019-05-09 18:54:39 +12:00
*
* Generate random password string
*
* @param int $length
*
2019-05-09 18:54:39 +12:00
* @return string
*
2019-05-09 18:54:39 +12:00
* @throws \Exception
*/
public static function tokenGenerator(int $length = 128):string
2019-05-09 18:54:39 +12:00
{
return \bin2hex(\random_bytes($length));
2019-05-09 18:54:39 +12:00
}
/**
* Verify token and check that its not expired.
2019-05-09 18:54:39 +12:00
*
* @param array $tokens
* @param int $type
2019-05-09 18:54:39 +12:00
* @param string $secret
*
2019-12-29 05:37:39 +13:00
* @return bool|string
2019-05-09 18:54:39 +12:00
*/
public static function tokenVerify(array $tokens, int $type, string $secret)
2019-05-09 18:54:39 +12:00
{
foreach ($tokens as $token) { /** @var Document $token */
if ($token->isSet('type') &&
$token->isSet('secret') &&
$token->isSet('expire') &&
$token->getAttribute('type') == $type &&
$token->getAttribute('secret') === self::hash($secret) &&
$token->getAttribute('expire') >= \time()) {
return (string)$token->getId();
2019-05-09 18:54:39 +12:00
}
}
return false;
}
2021-02-20 01:12:47 +13:00
/**
* Verify session and check that its not expired.
*
* @param array $sessions
* @param string $secret
*
* @return bool|string
*/
public static function sessionVerify(array $sessions, string $secret)
{
foreach ($sessions as $session) { /** @var Document $session */
if ($session->isSet('secret') &&
$session->isSet('expire') &&
$session->isSet('provider') &&
$session->getAttribute('secret') === self::hash($secret) &&
$session->getAttribute('expire') >= \time()) {
return (string)$session->getId();
}
}
return false;
}
/**
* Is Privileged User?
2021-10-06 08:23:04 +13:00
*
* @param array $roles
2021-10-06 08:23:04 +13:00
*
* @return bool
*/
2021-03-02 10:04:53 +13:00
public static function isPrivilegedUser(array $roles): bool
{
2021-10-06 08:23:04 +13:00
if (
array_key_exists('role:'.self::USER_ROLE_OWNER, $roles) ||
array_key_exists('role:'.self::USER_ROLE_DEVELOPER, $roles) ||
array_key_exists('role:'.self::USER_ROLE_ADMIN, $roles)
) {
return true;
}
return false;
}
/**
* Is App User?
2021-10-06 08:23:04 +13:00
*
* @param array $roles
2021-10-06 08:23:04 +13:00
*
* @return bool
*/
public static function isAppUser(array $roles): bool
{
2021-10-06 08:23:04 +13:00
if (array_key_exists('role:'.self::USER_ROLE_APP, $roles)) {
return true;
}
return false;
}
/**
* Returns all roles for a user.
2021-10-06 08:23:04 +13:00
*
* @param Document $user
* @return array
*/
public static function getRoles(Document $user): array
{
2021-09-04 03:15:42 +12:00
$roles = [];
2021-09-04 03:18:42 +12:00
if (!self::isPrivilegedUser(Authorization::$roles) && !self::isAppUser(Authorization::$roles)) {
2021-09-04 03:07:09 +12:00
if ($user->getId()) {
$roles[] = 'user:'.$user->getId();
$roles[] = 'role:'.Auth::USER_ROLE_MEMBER;
} else {
return ['role:'.Auth::USER_ROLE_GUEST];
}
}
foreach ($user->getAttribute('memberships', []) as $node) {
if (isset($node['teamId']) && isset($node['roles'])) {
$roles[] = 'team:' . $node['teamId'];
foreach ($node['roles'] as $nodeRole) { // Set all team roles
$roles[] = 'team:' . $node['teamId'] . '/' . $nodeRole;
}
}
}
return $roles;
}
}