Bootstrap user manager

Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
gearnode
2025-02-02 11:37:35 -08:00
parent 7dfa3363c0
commit 0e9746bbee
7 changed files with 262 additions and 0 deletions

View File

@@ -20,6 +20,7 @@ import (
console_v1 "github.com/getprobo/probo/pkg/api/console/v1"
"github.com/getprobo/probo/pkg/probo"
"github.com/getprobo/probo/pkg/usrmgr"
"github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
"go.gearno.de/kit/httpserver"
@@ -29,6 +30,7 @@ type (
Config struct {
AllowedOrigins []string
Probo *probo.Service
Usrmgr *usrmgr.Service
}
Server struct {

View File

@@ -25,6 +25,7 @@ import (
"github.com/getprobo/probo/pkg/api"
"github.com/getprobo/probo/pkg/probo"
"github.com/getprobo/probo/pkg/usrmgr"
"github.com/prometheus/client_golang/prometheus"
"go.gearno.de/kit/httpserver"
"go.gearno.de/kit/log"
@@ -94,6 +95,11 @@ func (impl *Implm) Run(
return fmt.Errorf("cannot create pg client: %w", err)
}
usrmgr, err := usrmgr.NewService(ctx, pgClient)
if err != nil {
return fmt.Errorf("cannot create usrmgr service: %w", err)
}
probo, err := probo.NewService(ctx, pgClient)
if err != nil {
return fmt.Errorf("cannot create probo service: %w", err)
@@ -102,6 +108,7 @@ func (impl *Implm) Run(
apiServer, err := api.NewServer(
api.Config{
Probo: probo,
Usrmgr: usrmgr,
AllowedOrigins: impl.cfg.Api.Cors.AllowedOrigins,
},
)

View File

@@ -0,0 +1,24 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
import (
"embed"
)
var (
//go:embed migrations/*.sql
Migrations embed.FS
)

View File

@@ -0,0 +1,12 @@
CREATE TABLE usrmgr_users (
id TEXT PRIMARY KEY,
created_at TIMESTAMP NOT NULL DEFAULT now(),
updated_at TIMESTAMP NOT NULL DEFAULT now()
);
CREATE TABLE usrmgr_sessions (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES usrmgr_users(id),
created_at TIMESTAMP NOT NULL DEFAULT now(),
updated_at TIMESTAMP NOT NULL DEFAULT now()
);

View File

@@ -0,0 +1,81 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
import (
"context"
"fmt"
"time"
"github.com/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5"
"go.gearno.de/crypto/uuid"
"go.gearno.de/kit/pg"
)
type (
Session struct {
ID gid.GID
UserID gid.GID
CreatedAt time.Time
UpdatedAt time.Time
}
)
func (s Session) CursorKey() page.CursorKey {
return page.NewCursorKey(uuid.UUID(s.ID), s.CreatedAt)
}
func (s *Session) scan(r pgx.Row) error {
return r.Scan(
&s.ID,
&s.CreatedAt,
&s.UpdatedAt,
)
}
func (s *Session) LoadByID(
ctx context.Context,
conn pg.Conn,
sessionID gid.GID,
) error {
q := `
SELECT
id,
created_at,
updated_at
FROM
sessions
WHERE
id = @session_id
LIMIT 1;
`
q = fmt.Sprintf(q)
args := pgx.NamedArgs{"session_id": sessionID}
r := conn.QueryRow(ctx, q, args)
s2 := Session{}
if err := s2.scan(r); err != nil {
return err
}
*s = s2
return nil
}

View File

@@ -0,0 +1,80 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
import (
"context"
"fmt"
"time"
"github.com/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5"
"go.gearno.de/crypto/uuid"
"go.gearno.de/kit/pg"
)
type (
User struct {
ID gid.GID
CreatedAt time.Time
UpdatedAt time.Time
}
)
func (u User) CursorKey() page.CursorKey {
return page.NewCursorKey(uuid.UUID(u.ID), u.CreatedAt)
}
func (u *User) scan(r pgx.Row) error {
return r.Scan(
&u.ID,
&u.CreatedAt,
&u.UpdatedAt,
)
}
func (u *User) LoadByID(
ctx context.Context,
conn pg.Conn,
userID gid.GID,
) error {
q := `
SELECT
id,
created_at,
updated_at
FROM
users
WHERE
id = @user_id
LIMIT 1;
`
q = fmt.Sprintf(q)
args := pgx.NamedArgs{"user_id": userID}
r := conn.QueryRow(ctx, q, args)
u2 := User{}
if err := u2.scan(r); err != nil {
return err
}
*u = u2
return nil
}

56
pkg/usrmgr/usrmgr.go Normal file
View File

@@ -0,0 +1,56 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package usrmgr
import (
"context"
"fmt"
"github.com/getprobo/probo/pkg/usrmgr/coredata"
"go.gearno.de/kit/migrator"
"go.gearno.de/kit/pg"
)
type (
Service struct {
pg *pg.Client
}
)
func NewService(
ctx context.Context,
pgClient *pg.Client,
) (*Service, error) {
err := migrator.NewMigrator(pgClient, coredata.Migrations).Run(ctx, "migrations")
if err != nil {
return nil, fmt.Errorf("cannot migrate database schema: %w", err)
}
return &Service{
pg: pgClient,
}, nil
}
func (s Service) Login(email, password string) (*coredata.Session, error) {
return nil, nil
}
func (s Service) Logout(sessionID string) error {
return nil
}
func (s Service) GetSession(sessionID string) (*coredata.Session, error) {
return nil, nil
}