puerta/internal/errors/errors.go

81 lines
1.5 KiB
Go
Raw Permalink Normal View History

2022-12-30 06:51:51 +00:00
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2022 Roberto Hidalgo <nidito@un.rob.mx>
package errors
2023-01-04 04:21:49 +00:00
import (
"encoding/base64"
"encoding/json"
"github.com/sirupsen/logrus"
)
2022-12-30 06:51:51 +00:00
type HTTPError interface {
Error() string
Code() int
}
func ToHTTP(err error) (string, int) {
if err := err.(HTTPError); err != nil {
return err.Error(), err.Code()
}
return err.Error(), 500
}
2023-01-04 04:21:49 +00:00
type AuthError interface {
Error() string
Code() int
Log()
}
type InvalidCredentials struct {
Status int
Reason string
}
func (err InvalidCredentials) Error() string {
return "Usuario o contraseña desconocidos"
}
func (err InvalidCredentials) Log() {
logrus.Error(err.Reason)
}
func (err InvalidCredentials) Code() int {
return err.Status
}
type WebAuthFlowChallenge struct {
Flow string
Data any
}
func (c WebAuthFlowChallenge) Error() string {
b, err := json.Marshal(map[string]any{"webauthn": c.Flow, "data": c.Data})
if err != nil {
logrus.Errorf("Could not marshal data: %s", err)
logrus.Errorf("data: %s", c.Data)
return ""
}
return string(b)
}
func (c WebAuthFlowChallenge) Header() string {
b, err := json.Marshal(c.Data)
if err != nil {
logrus.Errorf("Could not marshal data: %s", err)
logrus.Errorf("data: %s", c.Data)
return ""
}
return c.Flow + " " + base64.StdEncoding.EncodeToString([]byte(b))
}
func (c WebAuthFlowChallenge) Log() {
logrus.Error("responding with webauthn challenge")
}
func (c WebAuthFlowChallenge) Code() int {
return 418
}