feat: Add ent-based sqlite3 storage

Signed-off-by: m.nabokikh <maksim.nabokikh@flant.com>
This commit is contained in:
m.nabokikh
2020-12-31 02:07:32 +04:00
parent 674631c9ab
commit 11859166d0
31 changed files with 1878 additions and 4 deletions

View File

@@ -0,0 +1,89 @@
package schema
import (
"github.com/facebook/ent"
"github.com/facebook/ent/schema/field"
)
/* Original SQL table:
create table auth_code
(
id text not null primary key,
client_id text not null,
scopes blob not null,
nonce text not null,
redirect_uri text not null,
claims_user_id text not null,
claims_username text not null,
claims_email text not null,
claims_email_verified integer not null,
claims_groups blob not null,
connector_id text not null,
connector_data blob,
expiry timestamp not null,
claims_preferred_username text default '' not null,
code_challenge text default '' not null,
code_challenge_method text default '' not null
);
*/
// AuthCode holds the schema definition for the AuthCode entity.
type AuthCode struct {
ent.Schema
}
// Fields of the AuthCode.
func (AuthCode) Fields() []ent.Field {
return []ent.Field{
field.Text("id").
SchemaType(textSchema).
NotEmpty().
Unique(),
field.Text("client_id").
SchemaType(textSchema).
NotEmpty(),
field.JSON("scopes", []string{}).
Optional(),
field.Text("nonce").
SchemaType(textSchema).
NotEmpty(),
field.Text("redirect_uri").
SchemaType(textSchema).
NotEmpty(),
field.Text("claims_user_id").
SchemaType(textSchema).
NotEmpty(),
field.Text("claims_username").
SchemaType(textSchema).
NotEmpty(),
field.Text("claims_email").
SchemaType(textSchema).
NotEmpty(),
field.Bool("claims_email_verified"),
field.JSON("claims_groups", []string{}).
Optional(),
field.Text("claims_preferred_username").
SchemaType(textSchema).
Default(""),
field.Text("connector_id").
SchemaType(textSchema).
NotEmpty(),
field.Bytes("connector_data").
Nillable().
Optional(),
field.Time("expiry"),
field.Text("code_challenge").
SchemaType(textSchema).
Default(""),
field.Text("code_challenge_method").
SchemaType(textSchema).
Default(""),
}
}
// Edges of the AuthCode.
func (AuthCode) Edges() []ent.Edge {
return []ent.Edge{}
}

View File

@@ -0,0 +1,94 @@
package schema
import (
"github.com/facebook/ent"
"github.com/facebook/ent/schema/field"
)
/* Original SQL table:
create table auth_request
(
id text not null primary key,
client_id text not null,
response_types blob not null,
scopes blob not null,
redirect_uri text not null,
nonce text not null,
state text not null,
force_approval_prompt integer not null,
logged_in integer not null,
claims_user_id text not null,
claims_username text not null,
claims_email text not null,
claims_email_verified integer not null,
claims_groups blob not null,
connector_id text not null,
connector_data blob,
expiry timestamp not null,
claims_preferred_username text default '' not null,
code_challenge text default '' not null,
code_challenge_method text default '' not null
);
*/
// AuthRequest holds the schema definition for the AuthRequest entity.
type AuthRequest struct {
ent.Schema
}
// Fields of the AuthRequest.
func (AuthRequest) Fields() []ent.Field {
return []ent.Field{
field.Text("id").
SchemaType(textSchema).
NotEmpty().
Unique(),
field.Text("client_id").
SchemaType(textSchema),
field.JSON("scopes", []string{}).
Optional(),
field.JSON("response_types", []string{}).
Optional(),
field.Text("redirect_uri").
SchemaType(textSchema),
field.Text("nonce").
SchemaType(textSchema),
field.Text("state").
SchemaType(textSchema),
field.Bool("force_approval_prompt"),
field.Bool("logged_in"),
field.Text("claims_user_id").
SchemaType(textSchema),
field.Text("claims_username").
SchemaType(textSchema),
field.Text("claims_email").
SchemaType(textSchema),
field.Bool("claims_email_verified"),
field.JSON("claims_groups", []string{}).
Optional(),
field.Text("claims_preferred_username").
SchemaType(textSchema).
Default(""),
field.Text("connector_id").
SchemaType(textSchema),
field.Bytes("connector_data").
Nillable().
Optional(),
field.Time("expiry"),
field.Text("code_challenge").
SchemaType(textSchema).
Default(""),
field.Text("code_challenge_method").
SchemaType(textSchema).
Default(""),
}
}
// Edges of the AuthRequest.
func (AuthRequest) Edges() []ent.Edge {
return []ent.Edge{}
}

View File

@@ -0,0 +1,53 @@
package schema
import (
"github.com/facebook/ent"
"github.com/facebook/ent/schema/field"
)
/* Original SQL table:
create table client
(
id text not null primary key,
secret text not null,
redirect_uris blob not null,
trusted_peers blob not null,
public integer not null,
name text not null,
logo_url text not null
);
*/
// OAuth2Client holds the schema definition for the Client entity.
type OAuth2Client struct {
ent.Schema
}
// Fields of the OAuth2Client.
func (OAuth2Client) Fields() []ent.Field {
return []ent.Field{
field.Text("id").
SchemaType(textSchema).
NotEmpty().
Unique(),
field.Text("secret").
SchemaType(textSchema).
NotEmpty(),
field.JSON("redirect_uris", []string{}).
Optional(),
field.JSON("trusted_peers", []string{}).
Optional(),
field.Bool("public"),
field.Text("name").
SchemaType(textSchema).
NotEmpty(),
field.Text("logo_url").
SchemaType(textSchema).
NotEmpty(),
}
}
// Edges of the OAuth2Client.
func (OAuth2Client) Edges() []ent.Edge {
return []ent.Edge{}
}

View File

@@ -0,0 +1,46 @@
package schema
import (
"github.com/facebook/ent"
"github.com/facebook/ent/schema/field"
)
/* Original SQL table:
create table connector
(
id text not null primary key,
type text not null,
name text not null,
resource_version text not null,
config blob
);
*/
// Connector holds the schema definition for the Client entity.
type Connector struct {
ent.Schema
}
// Fields of the Connector.
func (Connector) Fields() []ent.Field {
return []ent.Field{
field.Text("id").
SchemaType(textSchema).
NotEmpty().
Unique(),
field.Text("type").
SchemaType(textSchema).
NotEmpty(),
field.Text("name").
SchemaType(textSchema).
NotEmpty(),
field.Text("resource_version").
SchemaType(textSchema),
field.Bytes("config"),
}
}
// Edges of the Connector.
func (Connector) Edges() []ent.Edge {
return []ent.Edge{}
}

View File

@@ -0,0 +1,50 @@
package schema
import (
"github.com/facebook/ent"
"github.com/facebook/ent/schema/field"
)
/* Original SQL table:
create table device_request
(
user_code text not null primary key,
device_code text not null,
client_id text not null,
client_secret text,
scopes blob not null,
expiry timestamp not null
);
*/
// DeviceRequest holds the schema definition for the DeviceRequest entity.
type DeviceRequest struct {
ent.Schema
}
// Fields of the DeviceRequest.
func (DeviceRequest) Fields() []ent.Field {
return []ent.Field{
field.Text("user_code").
SchemaType(textSchema).
NotEmpty().
Unique(),
field.Text("device_code").
SchemaType(textSchema).
NotEmpty(),
field.Text("client_id").
SchemaType(textSchema).
NotEmpty(),
field.Text("client_secret").
SchemaType(textSchema).
NotEmpty(),
field.JSON("scopes", []string{}).
Optional(),
field.Time("expiry"),
}
}
// Edges of the DeviceRequest.
func (DeviceRequest) Edges() []ent.Edge {
return []ent.Edge{}
}

View File

@@ -0,0 +1,45 @@
package schema
import (
"github.com/facebook/ent"
"github.com/facebook/ent/schema/field"
)
/* Original SQL table:
create table device_token
(
device_code text not null primary key,
status text not null,
token blob,
expiry timestamp not null,
last_request timestamp not null,
poll_interval integer not null
);
*/
// DeviceToken holds the schema definition for the DeviceToken entity.
type DeviceToken struct {
ent.Schema
}
// Fields of the DeviceToken.
func (DeviceToken) Fields() []ent.Field {
return []ent.Field{
field.Text("device_code").
SchemaType(textSchema).
NotEmpty().
Unique(),
field.Text("status").
SchemaType(textSchema).
NotEmpty(),
field.Bytes("token").Nillable().Optional(),
field.Time("expiry"),
field.Time("last_request"),
field.Int("poll_interval"),
}
}
// Edges of the DeviceToken.
func (DeviceToken) Edges() []ent.Edge {
return []ent.Edge{}
}

View File

@@ -0,0 +1,44 @@
package schema
import (
"github.com/facebook/ent"
"github.com/facebook/ent/schema/field"
"gopkg.in/square/go-jose.v2"
"github.com/dexidp/dex/storage"
)
/* Original SQL table:
create table keys
(
id text not null primary key,
verification_keys blob not null,
signing_key blob not null,
signing_key_pub blob not null,
next_rotation timestamp not null
);
*/
// Keys holds the schema definition for the Keys entity.
type Keys struct {
ent.Schema
}
// Fields of the Keys.
func (Keys) Fields() []ent.Field {
return []ent.Field{
field.Text("id").
SchemaType(textSchema).
NotEmpty().
Unique(),
field.JSON("verification_keys", []storage.VerificationKey{}),
field.JSON("signing_key", jose.JSONWebKey{}),
field.JSON("signing_key_pub", jose.JSONWebKey{}),
field.Time("next_rotation"),
}
}
// Edges of the Keys.
func (Keys) Edges() []ent.Edge {
return []ent.Edge{}
}

View File

@@ -0,0 +1,46 @@
package schema
import (
"github.com/facebook/ent"
"github.com/facebook/ent/schema/field"
)
/* Original SQL table:
create table offline_session
(
user_id text not null,
conn_id text not null,
refresh blob not null,
connector_data blob,
primary key (user_id, conn_id)
);
*/
// OfflineSession holds the schema definition for the OfflineSession entity.
type OfflineSession struct {
ent.Schema
}
// Fields of the OfflineSession.
func (OfflineSession) Fields() []ent.Field {
return []ent.Field{
// Using id field here because it's impossible to create multi-key primary yet
field.Text("id").
SchemaType(textSchema).
NotEmpty().
Unique(),
field.Text("user_id").
SchemaType(textSchema).
NotEmpty(),
field.Text("conn_id").
SchemaType(textSchema).
NotEmpty(),
field.Bytes("refresh"),
field.Bytes("connector_data").Nillable().Optional(),
}
}
// Edges of the OfflineSession.
func (OfflineSession) Edges() []ent.Edge {
return []ent.Edge{}
}

View File

@@ -0,0 +1,44 @@
package schema
import (
"github.com/facebook/ent"
"github.com/facebook/ent/schema/field"
)
/* Original SQL table:
create table password
(
email text not null primary key,
hash blob not null,
username text not null,
user_id text not null
);
*/
// Password holds the schema definition for the Password entity.
type Password struct {
ent.Schema
}
// Fields of the Password.
func (Password) Fields() []ent.Field {
return []ent.Field{
field.Text("email").
SchemaType(textSchema).
StorageKey("email"). // use email as ID field to make querying easier
NotEmpty().
Unique(),
field.Bytes("hash"),
field.Text("username").
SchemaType(textSchema).
NotEmpty(),
field.Text("user_id").
SchemaType(textSchema).
NotEmpty(),
}
}
// Edges of the Password.
func (Password) Edges() []ent.Edge {
return []ent.Edge{}
}

View File

@@ -0,0 +1,89 @@
package schema
import (
"time"
"github.com/facebook/ent"
"github.com/facebook/ent/schema/field"
)
/* Original SQL table:
create table refresh_token
(
id text not null primary key,
client_id text not null,
scopes blob not null,
nonce text not null,
claims_user_id text not null,
claims_username text not null,
claims_email text not null,
claims_email_verified integer not null,
claims_groups blob not null,
connector_id text not null,
connector_data blob,
token text default '' not null,
created_at timestamp default '0001-01-01 00:00:00 UTC' not null,
last_used timestamp default '0001-01-01 00:00:00 UTC' not null,
claims_preferred_username text default '' not null
);
*/
// RefreshToken holds the schema definition for the RefreshToken entity.
type RefreshToken struct {
ent.Schema
}
// Fields of the RefreshToken.
func (RefreshToken) Fields() []ent.Field {
return []ent.Field{
field.Text("id").
SchemaType(textSchema).
NotEmpty().
Unique(),
field.Text("client_id").
SchemaType(textSchema).
NotEmpty(),
field.JSON("scopes", []string{}).
Optional(),
field.Text("nonce").
SchemaType(textSchema).
NotEmpty(),
field.Text("claims_user_id").
SchemaType(textSchema).
NotEmpty(),
field.Text("claims_username").
SchemaType(textSchema).
NotEmpty(),
field.Text("claims_email").
SchemaType(textSchema).
NotEmpty(),
field.Bool("claims_email_verified"),
field.JSON("claims_groups", []string{}).
Optional(),
field.Text("claims_preferred_username").
SchemaType(textSchema).
Default(""),
field.Text("connector_id").
SchemaType(textSchema).
NotEmpty(),
field.Bytes("connector_data").
Nillable().
Optional(),
field.Text("token").
SchemaType(textSchema).
Default(""),
field.Time("created_at").
Default(time.Now),
field.Time("last_used").
Default(time.Now),
}
}
// Edges of the RefreshToken.
func (RefreshToken) Edges() []ent.Edge {
return []ent.Edge{}
}

View File

@@ -0,0 +1,9 @@
package schema
import (
"github.com/facebook/ent/dialect"
)
var textSchema = map[string]string{
dialect.SQLite: "text",
}