2022-07-29 06:29:21 +02:00
|
|
|
// Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
|
|
|
// All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2022-07-07 14:08:37 +02:00
|
|
|
package auth
|
2022-07-07 12:11:49 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2023-06-15 12:00:45 +02:00
|
|
|
"github.com/ClusterCockpit/cc-backend/pkg/log"
|
2023-08-17 10:29:00 +02:00
|
|
|
"github.com/ClusterCockpit/cc-backend/pkg/schema"
|
2022-07-07 12:11:49 +02:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LocalAuthenticator struct {
|
|
|
|
auth *Authentication
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Authenticator = (*LocalAuthenticator)(nil)
|
|
|
|
|
2022-09-07 12:24:45 +02:00
|
|
|
func (la *LocalAuthenticator) Init(
|
|
|
|
_ interface{}) error {
|
|
|
|
|
2022-07-07 12:11:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-07 12:24:45 +02:00
|
|
|
func (la *LocalAuthenticator) CanLogin(
|
2023-08-17 10:29:00 +02:00
|
|
|
user *schema.User,
|
2023-08-14 12:40:21 +02:00
|
|
|
username string,
|
2022-09-07 12:24:45 +02:00
|
|
|
rw http.ResponseWriter,
|
2023-08-17 14:02:04 +02:00
|
|
|
r *http.Request) (*schema.User, bool) {
|
2022-09-07 12:24:45 +02:00
|
|
|
|
2023-08-17 14:02:04 +02:00
|
|
|
return user, user != nil && user.AuthSource == schema.AuthViaLocalPassword
|
2022-07-07 12:11:49 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 12:24:45 +02:00
|
|
|
func (la *LocalAuthenticator) Login(
|
2023-08-17 10:29:00 +02:00
|
|
|
user *schema.User,
|
2022-09-07 12:24:45 +02:00
|
|
|
rw http.ResponseWriter,
|
2023-08-17 10:29:00 +02:00
|
|
|
r *http.Request) (*schema.User, error) {
|
2022-09-07 12:24:45 +02:00
|
|
|
|
2023-08-12 09:02:41 +02:00
|
|
|
if e := bcrypt.CompareHashAndPassword([]byte(user.Password),
|
|
|
|
[]byte(r.FormValue("password"))); e != nil {
|
2023-06-15 12:00:45 +02:00
|
|
|
log.Errorf("AUTH/LOCAL > Authentication for user %s failed!", user.Username)
|
|
|
|
return nil, fmt.Errorf("AUTH/LOCAL > Authentication failed")
|
2022-07-07 12:11:49 +02:00
|
|
|
}
|
|
|
|
|
2022-07-07 12:48:04 +02:00
|
|
|
return user, nil
|
2022-07-07 12:11:49 +02:00
|
|
|
}
|