aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/auth/basic.go
diff options
context:
space:
mode:
authorLeonardo Bishop <me@leonardobishop.com>2025-08-15 19:20:48 +0100
committerLeonardo Bishop <me@leonardobishop.com>2025-08-15 19:20:48 +0100
commit8f7dec8ba6b2f9bde01afd0a110596ebbd43e0ed (patch)
tree7b4f203d92f4b99b1e98fac314415e293984196b /pkg/auth/basic.go
parent4697556cac819c47d068819b9fc9c3b4ea84e279 (diff)
Implement OIDC
Diffstat (limited to 'pkg/auth/basic.go')
-rw-r--r--pkg/auth/basic.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/pkg/auth/basic.go b/pkg/auth/basic.go
new file mode 100644
index 0000000..dafd93f
--- /dev/null
+++ b/pkg/auth/basic.go
@@ -0,0 +1,56 @@
+package auth
+
+import (
+ "errors"
+
+ "github.com/LMBishop/confplanner/pkg/database/sqlc"
+ "github.com/LMBishop/confplanner/pkg/user"
+ "golang.org/x/crypto/bcrypt"
+)
+
+type BasicAuthProvider struct {
+ userService user.Service
+}
+
+func NewBasicAuthProvider(userService user.Service) AuthProvider {
+ return &BasicAuthProvider{
+ userService: userService,
+ }
+}
+
+func (p *BasicAuthProvider) Authenticate(username string, password string) (*sqlc.User, error) {
+ random, err := bcrypt.GenerateFromPassword([]byte("00000000"), bcrypt.DefaultCost)
+ if err != nil {
+ return nil, err
+ }
+
+ u, err := p.userService.GetUserByName(username)
+ if err != nil {
+ if errors.Is(err, user.ErrUserNotFound) {
+ bcrypt.CompareHashAndPassword(random, []byte(password))
+ return nil, nil
+ }
+ return nil, err
+ }
+ if !u.Password.Valid {
+ bcrypt.CompareHashAndPassword(random, []byte(password))
+ return nil, nil
+ }
+
+ if err = bcrypt.CompareHashAndPassword([]byte(u.Password.String), []byte(password)); err != nil {
+ if errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) {
+ return nil, nil
+ }
+ return nil, err
+ }
+
+ return u, nil
+}
+
+func (p *BasicAuthProvider) Name() string {
+ return "Basic"
+}
+
+func (p *BasicAuthProvider) Type() string {
+ return "basic"
+}