-- oauth2 support
drop table if exists oauth_client;
create table oauth_client(
    id                        integer primary key autoincrement,
    client_id                 varchar(64)   not null,
    client_name               varchar(64)   not null,
    description               varchar(128)  not null,
    client_secret             varchar(128)  not null,
    client_domain             varchar(128)  not null,
    logout_url                varchar(128)  not null,
    trusted                   int(10)       not null,
    create_time               datetime      not null,
    update_time               datetime      not null
);

drop table if exists oauth_authorization_code;
create table oauth_authorization_code(
    id                        integer primary key autoincrement,
    client_id                 varchar(64)   not null,
    user_id                   bigint(20)    not null,
    session_id                varchar(64)   not null,
    authorization_code        varchar(64)   not null,
    create_time               datetime      not null,
    update_time               datetime      not null,
    unique (authorization_code)
);

drop table if exists oauth_access_token;
create table oauth_access_token(
    id                        integer primary key autoincrement,
    client_id                 varchar(64)   not null,
    user_id                   bigint(20)    not null,
    session_id                varchar(64)   not null,
    access_token              varchar(64)   not null,
    refresh_token             varchar(64)   not null,
    token_type                varchar(32)   not null,
    expires                   bigint(20)    not null,
    scope                     varchar(64)   not null,
    create_time               datetime      not null,
    update_time               datetime      not null
);

-- 用户已登录的系统
drop table if exists oauth_login_client;
create table oauth_login_client(
    id                        integer primary key autoincrement,
    user_id                   bigint(20)    not null,
    session_id                varchar(64)   not null,
    client_id                 varchar(64)   not null,
    create_time               datetime      not null,
    update_time               datetime      not null,
    unique (user_id, session_id, client_id)
);

drop table if exists oauth_user_session;
create table oauth_user_session(
    id                   varchar(64) primary key not null,
    app_id               bigint(20)          not null,
    user_id              bigint(20)          not null,
    user_name            varchar(32)         not null,
    nick_name            varchar(32)         not null,
    client_id            varchar(64)         not null,
    remote_ip            varchar(64)         not null,
    last_access_time     datetime            not null,
    expires              datetime            not null,
    create_time          datetime            not null,
    update_time          datetime            not null
);
