Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions internal/server/auth/apikey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package auth

import (
"crypto/rand"
"encoding/base64"
"fmt"
)

var Prefix = "potok_"

const keyBytes = 32

func GenerateAPIKey() (key string, err error) {
raw := make([]byte, keyBytes)
if _, err := rand.Read(raw); err != nil {
return "", fmt.Errorf("auth: generate key: %w", err)
}
key = Prefix + base64.RawURLEncoding.EncodeToString(raw)
return key, nil
}
17 changes: 17 additions & 0 deletions internal/server/auth/apikey_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package auth

import (
"strings"
"testing"
)

func TestGenerateAPIKey(t *testing.T) {
valid, err := GenerateAPIKey()
if err != nil {
t.Fatalf("GenerateAPIKey() error: %v", err)
}

if !strings.HasPrefix(valid, Prefix) {
t.Errorf("GenerateAPIKey() = %v, want %v", valid, Prefix+valid)
}
}
2 changes: 0 additions & 2 deletions internal/server/auth/doc.go

This file was deleted.

14 changes: 11 additions & 3 deletions internal/server/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/google/uuid"
"github.com/michaeltukdev/Potok/internal/server/auth"
"golang.org/x/crypto/bcrypt"
)

Expand All @@ -30,6 +31,7 @@ type User struct {
Email string `json:"email"`
PasswordHash string `json:"password_hash"`
IsAdmin bool `json:"is_admin"`
APIKey string `json:"api_key"`
CreatedAt time.Time `json:"created_at"`
}

Expand Down Expand Up @@ -87,18 +89,24 @@ func (s *Store) CreateUser(ctx context.Context, email, password string) (User, e
return User{}, fmt.Errorf("store: create user: %w", err)
}

key, err := auth.GenerateAPIKey()
if err != nil {
return User{}, fmt.Errorf("store: create user: %w", err)
}

user := User{
ID: uuid.NewString(),
Email: email,
PasswordHash: string(hash),
APIKey: key,
}

err = s.db.QueryRowContext(ctx, `
INSERT INTO users (id, email, password_hash)
VALUES (?, ?, ?)
INSERT INTO users (id, email, password_hash, api_key)
VALUES (?, ?, ?, ?)
ON CONFLICT (email) DO NOTHING
RETURNING id, email, is_admin, created_at`,
user.ID, user.Email, user.PasswordHash,
user.ID, user.Email, user.PasswordHash, key,
).Scan(&user.ID, &user.Email, &user.IsAdmin, &user.CreatedAt)

if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions migrations/0001_init.down.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
DROP INDEX IF EXISTS vaults_user_id_idx;
DROP TABLE IF EXISTS vaults;
DROP INDEX IF EXISTS api_keys_user_id_idx;
DROP TABLE IF EXISTS api_keys;
DROP TABLE IF EXISTS users;
12 changes: 1 addition & 11 deletions migrations/0001_init.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,10 @@ CREATE TABLE users (
email TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
is_admin INTEGER NOT NULL DEFAULT 0,
api_key TEXT UNIQUE,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE api_keys (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE,
label TEXT NOT NULL,
key_hash BLOB NOT NULL UNIQUE,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
last_used_at TIMESTAMP
);

CREATE INDEX api_keys_user_id_idx ON api_keys (user_id);

CREATE TABLE vaults (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE,
Expand Down
Loading