aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/OAuth2/Models/AccessToken.php
blob: 3c57973d86b898693ddb582a291f801977f43c2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php

namespace Studip\OAuth2\Models;

/**
 * @property int $id
 * @property string $user_id
 * @property string $client_id
 * @property string $scopes
 * @property bool $revoked
 * @property int $expires_at
 * @property int $mkdate
 * @property int $chdate
 */
class AccessToken extends \SimpleORMap
{
    use RevokedHelper;

    protected static function configure($config = [])
    {
        $config['db_table'] = 'oauth2_access_tokens';

        $config['belongs_to']['client'] = [
            'class_name'  => Client::class,
            'foreign_key' => 'client_id',
        ];

        $config['belongs_to']['user'] = [
            'class_name'  => \User::class,
            'foreign_key' => 'user_id',
        ];

        $config['has_many']['refresh_tokens'] = [
            'class_name'        => RefreshToken::class,
            'assoc_foreign_key' => 'access_token_id',
            'on_delete'         => 'delete',
            'on_store'          => 'store',
        ];

        parent::configure($config);
    }

    public static function findValidTokens(\User $user)
    {
        return static::findBySQL(
            'user_id = ? AND revoked = ? AND expires_at > ?',
            [$user->id, 0, time()]
        );
    }
}