Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
// Copyright © 2023 Ory Corp
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package hash
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/ory/kratos/text"
|
|
"github.com/ory/x/otelx"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
"github.com/ory/kratos/schema"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"github.com/ory/kratos/driver/config"
|
|
)
|
|
|
|
type Bcrypt struct {
|
|
c BcryptConfiguration
|
|
}
|
|
|
|
type BcryptConfiguration interface {
|
|
config.Provider
|
|
}
|
|
|
|
func NewHasherBcrypt(c BcryptConfiguration) *Bcrypt {
|
|
return &Bcrypt{c: c}
|
|
}
|
|
|
|
func (h *Bcrypt) Generate(ctx context.Context, password []byte) (_ []byte, err error) {
|
|
conf := h.c.Config().HasherBcrypt(ctx)
|
|
|
|
_, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "hash.Generate", trace.WithAttributes(
|
|
attribute.String("hash.type", "bcrypt"),
|
|
attribute.String("hash.config", fmt.Sprintf("%#v", conf)),
|
|
))
|
|
defer otelx.End(span, &err)
|
|
|
|
if err := validateBcryptPasswordLength(password); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// ensure that the context is not canceled before doing the heavy lifting
|
|
if ctx.Err() != nil {
|
|
return nil, ctx.Err()
|
|
}
|
|
|
|
hash, err := bcrypt.GenerateFromPassword(password, int(conf.Cost))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return hash, nil
|
|
}
|
|
|
|
func validateBcryptPasswordLength(password []byte) error {
|
|
// Bcrypt truncates the password to the first 72 bytes, following the OpenBSD implementation,
|
|
// so if password is longer than 72 bytes, function returns an error
|
|
// See https://en.wikipedia.org/wiki/Bcrypt#User_input
|
|
if len(password) > 72 {
|
|
return schema.NewPasswordPolicyViolationError(
|
|
"#/password",
|
|
text.NewErrorValidationPasswordMaxLength(72, len(password)),
|
|
)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (h *Bcrypt) Understands(hash []byte) bool {
|
|
return IsBcryptHash(hash)
|
|
}
|