Skip to content

Commit

Permalink
feat(lint): fix golang practice
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkness4 committed Feb 20, 2024
1 parent fcf823d commit 4307a60
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 85 deletions.
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ linters:
enable:
# Drop-in replacement of `golint`.
- revive
- ireturn

issues:
include:
Expand Down
38 changes: 38 additions & 0 deletions auth/webauthn/session/memory/memory_session_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Package memory implements a session store in-memory.
package memory

import (
"context"

"github.com/Darkness4/auth-htmx/auth/webauthn/session"
"github.com/go-webauthn/webauthn/webauthn"
)

// Store stores the login/registration session in-memory.
//
// In production, you should use a Redis or ETCD, or any distributed Key-Value database.
// Because of this, you cannot create replicas.
type Store struct {
store map[string]*webauthn.SessionData
}

// NewStore instanciates a session store in memory.
func NewStore() *Store {
return &Store{
store: make(map[string]*webauthn.SessionData),
}
}

// Get the login or registration session.
func (s *Store) Get(_ context.Context, userID []byte) (*webauthn.SessionData, error) {
if v, ok := s.store[string(userID)]; ok {
return v, nil
}
return nil, session.ErrNotFound
}

// Save the login or registration session.
func (s *Store) Save(_ context.Context, session *webauthn.SessionData) error {
s.store[string(session.UserID)] = session
return nil
}
18 changes: 18 additions & 0 deletions auth/webauthn/session/session_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Package session handles the login/register sessions of webauthn.
package session

import (
"context"
"errors"

"github.com/go-webauthn/webauthn/webauthn"
)

// Store stores the login/registration session.
type Store interface {
Save(ctx context.Context, session *webauthn.SessionData) error
Get(ctx context.Context, userID []byte) (*webauthn.SessionData, error)
}

// ErrNotFound happens when the session is not found in the store.
var ErrNotFound = errors.New("not found in session store")
47 changes: 0 additions & 47 deletions auth/webauthn/session/webauthn_session_store.go

This file was deleted.

20 changes: 20 additions & 0 deletions auth/webauthn/user/user_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Package user describes the methods to handle users needed for webauthn.
package user

import (
"context"

"github.com/Darkness4/auth-htmx/database/user"
"github.com/go-webauthn/webauthn/webauthn"
)

// Repository defines the user methods needed for webauthn.
type Repository interface {
GetOrCreateByName(ctx context.Context, name string) (*user.User, error)
GetByName(ctx context.Context, name string) (*user.User, error)
Get(ctx context.Context, id []byte) (*user.User, error)
Create(ctx context.Context, name string, displayName string) (*user.User, error)
AddCredential(ctx context.Context, id []byte, credential *webauthn.Credential) error
UpdateCredential(ctx context.Context, credential *webauthn.Credential) error
RemoveCredential(ctx context.Context, id []byte, credentialID []byte) error
}
2 changes: 1 addition & 1 deletion auth/webauthn/webauthn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/Darkness4/auth-htmx/auth/webauthn/session"
"github.com/Darkness4/auth-htmx/database/user"
"github.com/Darkness4/auth-htmx/auth/webauthn/user"
"github.com/Darkness4/auth-htmx/jwt"
"github.com/go-webauthn/webauthn/protocol"
"github.com/go-webauthn/webauthn/webauthn"
Expand Down
17 changes: 6 additions & 11 deletions database/counter/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,20 @@ import (
"github.com/Darkness4/auth-htmx/database"
)

// Repository defines the counter methods.
type Repository interface {
Inc(ctx context.Context, userID string) (new int64, err error)
Get(ctx context.Context, userID string) (int64, error)
}

// NewRepository wraps around a SQL database to execute the counter methods.
func NewRepository(db *sql.DB) Repository {
return &repository{
func NewRepository(db *sql.DB) *Repository {
return &Repository{
Queries: database.New(db),
}
}

type repository struct {
// Repository wraps around a SQL database to execute the counter methods.
type Repository struct {
*database.Queries
}

// Inc increments the counter of a user in the database by one.
func (r *repository) Inc(ctx context.Context, userID string) (newValue int64, err error) {
func (r *Repository) Inc(ctx context.Context, userID string) (newValue int64, err error) {
newValue, err = r.Queries.IncrementCounter(ctx, userID)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return newValue, err
Expand All @@ -39,7 +34,7 @@ func (r *repository) Inc(ctx context.Context, userID string) (newValue int64, er
}

// Get the value of the counter of a user from the database.
func (r *repository) Get(ctx context.Context, userID string) (int64, error) {
func (r *Repository) Get(ctx context.Context, userID string) (int64, error) {
counter, err := r.Queries.GetCounter(ctx, userID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
Expand Down
36 changes: 13 additions & 23 deletions database/user/user_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,15 @@ import (
"github.com/go-webauthn/webauthn/webauthn"
)

// Repository defines the user methods.
type Repository interface {
GetOrCreateByName(ctx context.Context, name string) (*User, error)
GetByName(ctx context.Context, name string) (*User, error)
Get(ctx context.Context, id []byte) (*User, error)
Create(ctx context.Context, name string, displayName string) (*User, error)
AddCredential(ctx context.Context, id []byte, credential *webauthn.Credential) error
UpdateCredential(ctx context.Context, credential *webauthn.Credential) error
RemoveCredential(ctx context.Context, id []byte, credentialID []byte) error
}

// NewRepository wraps around a SQL database to execute the counter methods.
func NewRepository(db *sql.DB) Repository {
return &repository{
// NewRepository instanciates a new user repository.
func NewRepository(db *sql.DB) *Repository {
return &Repository{
Queries: database.New(db),
}
}

type repository struct {
// Repository wraps around a SQL database to execute the webauthn methods.
type Repository struct {
*database.Queries
}

Expand All @@ -42,7 +32,7 @@ var (
)

// AddCredential to a user from the database.
func (r *repository) AddCredential(
func (r *Repository) AddCredential(
ctx context.Context,
id []byte,
credential *webauthn.Credential,
Expand Down Expand Up @@ -75,7 +65,7 @@ func (r *repository) AddCredential(
}

// UpdateCredential of a user from the database.
func (r *repository) UpdateCredential(ctx context.Context, credential *webauthn.Credential) error {
func (r *Repository) UpdateCredential(ctx context.Context, credential *webauthn.Credential) error {
if credential.Transport == nil {
credential.Transport = []protocol.AuthenticatorTransport{}
}
Expand Down Expand Up @@ -106,7 +96,7 @@ func (r *repository) UpdateCredential(ctx context.Context, credential *webauthn.
// Create a user in the database.
//
// The user ID is completely randomized.
func (r *repository) Create(ctx context.Context, name string, displayName string) (*User, error) {
func (r *Repository) Create(ctx context.Context, name string, displayName string) (*User, error) {
id := make([]byte, 64)
if _, err := rand.Read(id); err != nil {
return nil, err
Expand All @@ -125,7 +115,7 @@ func (r *repository) Create(ctx context.Context, name string, displayName string
}

// GetOrCreateByName a user from the databse.
func (r *repository) GetOrCreateByName(ctx context.Context, name string) (*User, error) {
func (r *Repository) GetOrCreateByName(ctx context.Context, name string) (*User, error) {
u, err := r.GetByName(ctx, name)
if errors.Is(err, ErrUserNotFound) {
u, err = r.Create(ctx, name, name)
Expand All @@ -140,7 +130,7 @@ func (r *repository) GetOrCreateByName(ctx context.Context, name string) (*User,
}

// Gea user from the database.
func (r *repository) Get(ctx context.Context, id []byte) (*User, error) {
func (r *Repository) Get(ctx context.Context, id []byte) (*User, error) {
u, err := r.Queries.GetUser(ctx, id)
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrUserNotFound
Expand All @@ -157,7 +147,7 @@ func (r *repository) Get(ctx context.Context, id []byte) (*User, error) {
}

// GetByName a user from the database.
func (r *repository) GetByName(ctx context.Context, name string) (*User, error) {
func (r *Repository) GetByName(ctx context.Context, name string) (*User, error) {
u, err := r.Queries.GetUserByName(ctx, name)
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrUserNotFound
Expand All @@ -173,7 +163,7 @@ func (r *repository) GetByName(ctx context.Context, name string) (*User, error)
return fromModel(&u, credentials), nil
}

func (r *repository) getCredentialsByUser(
func (r *Repository) getCredentialsByUser(
ctx context.Context,
id []byte,
) ([]webauthn.Credential, error) {
Expand All @@ -192,7 +182,7 @@ func (r *repository) getCredentialsByUser(
}

// RemoveCredential of a user from the database.
func (r *repository) RemoveCredential(
func (r *Repository) RemoveCredential(
ctx context.Context,
id []byte,
credentialID []byte,
Expand Down
2 changes: 1 addition & 1 deletion handler/handler_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// Count increments the counter and returns the new value.
func Count(counter counter.Repository) http.HandlerFunc {
func Count(counter *counter.Repository) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
claims, ok := jwt.GetClaimsFromRequest(r)
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/Darkness4/auth-htmx/auth"
"github.com/Darkness4/auth-htmx/auth/oauth"
internalwebauthn "github.com/Darkness4/auth-htmx/auth/webauthn"
"github.com/Darkness4/auth-htmx/auth/webauthn/session"
"github.com/Darkness4/auth-htmx/auth/webauthn/session/memory"
"github.com/Darkness4/auth-htmx/database"
"github.com/Darkness4/auth-htmx/database/counter"
"github.com/Darkness4/auth-htmx/database/user"
Expand Down Expand Up @@ -168,7 +168,7 @@ var app = &cli.App{
webauthnS := internalwebauthn.New(
webAuthn,
user.NewRepository(d),
session.NewInMemory(),
memory.NewStore(),
jwt.Secret(jwtSecret),
)

Expand Down

0 comments on commit 4307a60

Please sign in to comment.