change to postgres

This commit is contained in:
2021-05-11 14:31:22 +02:00
parent c7dbaeabbb
commit fd9b673df9

View File

@ -1,111 +1,57 @@
CREATE DATABASE `authservice`; create sequence application_s start with 1 increment by 1;
USE `authservice`; create table application_t (
id integer primary key not null default nextval('application_s'),
name varchar(128) not null unique
);
CREATE TABLE `applications` ( create sequence user_s start with 1 increment by 1;
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, create table user_t (
`name` varchar(128) NOT NULL, id integer primary key not null default nextval('user_s'),
CONSTRAINT PRIMARY KEY (`id`), login varchar(64) not null unique,
CONSTRAINT UNIQUE KEY `uk_applications_name` (`name`) pwhash varchar(64) not null,
) ENGINE=InnoDB; expiry integer not null default 600
);
CREATE TABLE `users` ( create sequence claim_s start with 1 increment by 1;
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, create table claim_t (
`login` varchar(64) NOT NULL, id integer primary key not null default nextval('claim_s'),
`pwhash` varchar(64) NOT NULL, key varchar(64) not null,
`expiry` int(10) unsigned NOT NULL DEFAULT 600, value varchar(64) not null,
CONSTRAINT PRIMARY KEY (`id`), unique (key, value)
CONSTRAINT UNIQUE KEY `uk_users_login` (`login`) );
) ENGINE=InnoDB;
CREATE TABLE `claims` ( create table user_claim_mapping_t (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, "user" integer not null references user_t(id),
`key` varchar(64) NOT NULL, claim integer not null references claim_t(id),
`value` varchar(1024) NOT NULL, unique ("user", claim)
CONSTRAINT PRIMARY KEY (`id`), );
CONSTRAINT UNIQUE KEY `uk_claims_key_value` (`key`, `value`)
) ENGINE=InnoDB;
CREATE TABLE `user_claims_mapping` ( create table user_application_mapping_t (
`user` int(10) unsigned NOT NULL, "user" integer not null references user_t(id),
`claim` int(10) unsigned NOT NULL, application integer not null references application_t(id),
CONSTRAINT UNIQUE KEY `uk_user_claims_mapping` (`user`, `claim` ), unique ("user", application)
CONSTRAINT FOREIGN KEY `fk_user_claims_mapping_user` (`user`) );
REFERENCES `users`(`id`),
CONSTRAINT FOREIGN KEY `fk_user_claims_mapping_claim` (`claim`)
REFERENCES `claims`(`id`)
) ENGINE=InnoDB;
CREATE TABLE `user_applications_mapping` ( create or replace view claims_for_user_v as
`user` int(10) unsigned NOT NULL, select u.id as "user",
`application` int(10) unsigned NOT NULL, c.key as key,
CONSTRAINT UNIQUE KEY `uk_user_applications_mapping` (`user`, `application` ), c.value as value
CONSTRAINT FOREIGN KEY `fk_user_applications_mapping_user` (`user`) from user_t u,
REFERENCES `users`(`id`), claim_t c,
CONSTRAINT FOREIGN KEY `fk_user_applications_mapping_application` (`application`) user_claim_mapping_t m
REFERENCES `applications`(`id`) where m.user = u.id and
) ENGINE=InnoDB;
CREATE OR REPLACE VIEW claims_for_user AS
SELECT u.id AS user,
c.`key` AS `key`,
c.`value` AS `value`
FROM users u,
claims c,
user_claims_mapping m
WHERE m.user = u.id AND
m.claim = c.id; m.claim = c.id;
CREATE OR REPLACE VIEW user_application AS create or replace view user_application_v as
SELECT u.login AS login, select u.login as login,
u.pwhash AS pwhash, u.pwhash as pwhash,
u.id AS id, u.id as id,
u.expiry AS expiry, u.expiry as expiry,
a.name as application a.name as application
FROM users u, from user_t u,
applications a, application_t a,
user_applications_mapping m user_application_mapping_t m
WHERE u.id = m.user AND where u.id = m.user and
a.id = m.application; a.id = m.application;
CREATE USER 'authservice-ui'@'%' IDENTIFIED BY 'test123';
GRANT SELECT ON `user_application` TO 'authservice-ui'@'%';
GRANT SELECT ON `claims_for_user` TO 'authservice-ui'@'%';
CREATE USER 'authservice-cli'@'%' IDENTIFIED BY 'test123';
GRANT INSERT ON `users` TO 'authservice-cli'@'%';
GRANT INSERT ON `user_applications_mapping` TO 'authservice-cli'@'%';
FLUSH PRIVILEGES;
INSERT INTO `applications` (`name`) VALUES ('hv');
INSERT INTO `claims` (`key`, `value`) VALUES ('accesslevel', 'r');
INSERT INTO `claims` (`key`, `value`) VALUES ('accesslevel', 'rw');
-- password is 'test123'
INSERT INTO `users` (`login`, `pwhash`) VALUES ('wn', '$p5k2$186a0$dJXL0AjF$0HualDF92nyilDXPgSbaUn/UpFzSrpPx');
INSERT INTO `user_applications_mapping` (`user`, `application`)
VALUES(
(SELECT `id` FROM `users` WHERE `login` = 'wn'),
(SELECT `id` FROM `applications` WHERE `name` = 'hv')
);
INSERT INTO `user_claims_mapping` (`user`, `claim`)
VALUES(
(SELECT `id` FROM `users` WHERE `login` = 'wn'),
(SELECT `id` FROM `claims` WHERE `key` = 'accesslevel' AND `value` = 'rw')
);
-- password is 'geheim'
INSERT INTO `users` (`login`, `pwhash`) VALUES ('gregor', '$p5k2$186a0$Tcwps8Ar$TsypGB.y1dCB9pWOPz2X2SsxYqrTn3Fv');
INSERT INTO `user_applications_mapping` (`user`, `application`)
VALUES(
(SELECT `id` FROM `users` WHERE `login` = 'gregor'),
(SELECT `id` FROM `applications` WHERE `name` = 'hv')
);
INSERT INTO `user_claims_mapping` (`user`, `claim`)
VALUES(
(SELECT `id` FROM `users` WHERE `login` = 'gregor'),
(SELECT `id` FROM `claims` WHERE `key` = 'accesslevel' AND `value` = 'rw')
);