56
pkg/probod/auth_config.go
Normal file
56
pkg/probod/auth_config.go
Normal 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 probod
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type (
|
||||
authConfig struct {
|
||||
// Pepper is a secret key used for password hashing
|
||||
// It should be at least 32 bytes long
|
||||
Pepper string `json:"pepper"`
|
||||
SessionDuration int `json:"session-duration"`
|
||||
CookieName string `json:"cookie-name"`
|
||||
CookieSecure bool `json:"cookie-secure"`
|
||||
CookieHTTPOnly bool `json:"cookie-http-only"`
|
||||
CookieDomain string `json:"cookie-domain"`
|
||||
CookiePath string `json:"cookie-path"`
|
||||
}
|
||||
)
|
||||
|
||||
// GetPepperBytes returns the pepper as a byte array
|
||||
func (c authConfig) GetPepperBytes() ([]byte, error) {
|
||||
if c.Pepper == "" {
|
||||
return nil, fmt.Errorf("pepper cannot be empty")
|
||||
}
|
||||
|
||||
// If the pepper is base64 encoded, decode it
|
||||
if decoded, err := base64.StdEncoding.DecodeString(c.Pepper); err == nil {
|
||||
if len(decoded) < 32 {
|
||||
return nil, fmt.Errorf("decoded pepper must be at least 32 bytes long")
|
||||
}
|
||||
return decoded, nil
|
||||
}
|
||||
|
||||
// Otherwise use the raw string as the pepper
|
||||
if len(c.Pepper) < 32 {
|
||||
return nil, fmt.Errorf("pepper must be at least 32 bytes long")
|
||||
}
|
||||
|
||||
return []byte(c.Pepper), nil
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/getprobo/probo/pkg/api"
|
||||
console_v1 "github.com/getprobo/probo/pkg/api/console/v1"
|
||||
"github.com/getprobo/probo/pkg/probo"
|
||||
"github.com/getprobo/probo/pkg/usrmgr"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
@@ -40,8 +41,9 @@ type (
|
||||
}
|
||||
|
||||
config struct {
|
||||
Pg pgConfig `json:"pg"`
|
||||
Api apiConfig `json:"api"`
|
||||
Pg pgConfig `json:"pg"`
|
||||
Api apiConfig `json:"api"`
|
||||
Auth authConfig `json:"auth"`
|
||||
}
|
||||
)
|
||||
|
||||
@@ -66,6 +68,15 @@ func New() *Implm {
|
||||
Database: "probod",
|
||||
PoolSize: 100,
|
||||
},
|
||||
Auth: authConfig{
|
||||
Pepper: "this-is-a-secure-pepper-for-password-hashing-at-least-32-bytes",
|
||||
SessionDuration: 24,
|
||||
CookieName: "SSID",
|
||||
CookieSecure: false,
|
||||
CookieHTTPOnly: true,
|
||||
CookieDomain: "localhost",
|
||||
CookiePath: "/",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -95,21 +106,36 @@ func (impl *Implm) Run(
|
||||
return fmt.Errorf("cannot create pg client: %w", err)
|
||||
}
|
||||
|
||||
usrmgr, err := usrmgr.NewService(ctx, pgClient)
|
||||
// Get the pepper bytes for password hashing
|
||||
pepper, err := impl.cfg.Auth.GetPepperBytes()
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot get pepper bytes: %w", err)
|
||||
}
|
||||
|
||||
// Initialize the user management service with the pepper
|
||||
usrmgrService, err := usrmgr.NewService(ctx, pgClient, pepper)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create usrmgr service: %w", err)
|
||||
}
|
||||
|
||||
probo, err := probo.NewService(ctx, pgClient)
|
||||
proboService, err := probo.NewService(ctx, pgClient)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create probo service: %w", err)
|
||||
}
|
||||
|
||||
apiServer, err := api.NewServer(
|
||||
api.Config{
|
||||
Probo: probo,
|
||||
Usrmgr: usrmgr,
|
||||
Probo: proboService,
|
||||
Usrmgr: usrmgrService,
|
||||
AllowedOrigins: impl.cfg.Api.Cors.AllowedOrigins,
|
||||
Auth: console_v1.AuthConfig{
|
||||
CookieName: impl.cfg.Auth.CookieName,
|
||||
CookieSecure: impl.cfg.Auth.CookieSecure,
|
||||
CookieHTTPOnly: impl.cfg.Auth.CookieHTTPOnly,
|
||||
CookieDomain: impl.cfg.Auth.CookieDomain,
|
||||
CookiePath: impl.cfg.Auth.CookiePath,
|
||||
SessionDuration: time.Duration(impl.cfg.Auth.SessionDuration) * time.Hour,
|
||||
},
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user