aboutsummaryrefslogtreecommitdiff
path: root/db/migrations/5.2.15_create_oauth2_tables.php
blob: 460d4e0638429e05febdbb992b0d4ee408c9ece0 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php

class CreateOauth2Tables extends Migration
{
    public function description()
    {
        return 'creates all necessary tables for the OAuth2 plugin';
    }

    public function up()
    {
        $db = DBManager::get();

        $query = "CREATE TABLE IF NOT EXISTS `oauth2_access_tokens` (
                    `id`         VARCHAR(100) NOT NULL,
                    `user_id`    CHAR(32) COLLATE `latin1_bin` NULL,
                    `client_id`  BIGINT UNSIGNED NOT NULL,
                    `scopes`     TEXT NULL,
                    `revoked`    TINYINT(1) NOT NULL DEFAULT 0,
                    `expires_at` INT(11) NULL,
                    `mkdate`     INT(11) NOT NULL,
                    `chdate`     INT(11) NOT NULL,

                    PRIMARY KEY (`id`),
                    KEY `user_id` (`user_id`)
                  )";
        $db->exec($query);

        $query = "CREATE TABLE IF NOT EXISTS `oauth2_auth_codes` (
                    `id`           VARCHAR(100) NOT NULL,
                    `user_id`      CHAR(32) COLLATE `latin1_bin` NOT NULL,
                    `client_id`    BIGINT UNSIGNED NOT NULL,
                    `scopes`       TEXT NULL,
                    `revoked`      TINYINT(1) NOT NULL DEFAULT 0,
                    `expires_at`   INT(11) NULL,
                    `mkdate`       INT(11) NOT NULL,
                    `chdate`       INT(11) NOT NULL,

                    PRIMARY KEY (`id`),
                    KEY `user_id` (`user_id`)
                  )";
        $db->exec($query);

        $query = "CREATE TABLE IF NOT EXISTS `oauth2_clients` (
                    `id`            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
                    `name`          VARCHAR(255) NOT NULL,
                    `secret`        VARCHAR(100) NULL,
                    `redirect`      TEXT NOT NULL,
                    `revoked`       TINYINT(1) NOT NULL DEFAULT 0,

                    `description`   TEXT NULL,
                    `owner`         VARCHAR(255) NULL,
                    `homepage`      VARCHAR(255) NULL,
                    `admin_notes`   TEXT NULL,

                    `mkdate`        INT(11) NOT NULL,
                    `chdate`        INT(11) NOT NULL,

                    PRIMARY KEY (`id`)
                  )";
        $db->exec($query);

        $query = "CREATE TABLE IF NOT EXISTS `oauth2_refresh_tokens` (
                    `id`              VARCHAR(100) NOT NULL,
                    `access_token_id` VARCHAR(100) NOT NULL,
                    `revoked`         TINYINT(1) NOT NULL DEFAULT 0,
                    `expires_at`      INT(11) NULL,

                    PRIMARY KEY (`id`),
                    KEY `access_token_id` (`access_token_id`)
                  )";
        $db->exec($query);
    }

    public function down()
    {
        $db = \DBManager::get();
        $db->exec('DROP TABLE IF EXISTS `oauth2_access_tokens`');
        $db->exec('DROP TABLE IF EXISTS `oauth2_auth_codes`');
        $db->exec('DROP TABLE IF EXISTS `oauth2_clients`');
        $db->exec('DROP TABLE IF EXISTS `oauth2_refresh_tokens`');
    }
}