2021-05-04 22:06:05 +00:00
|
|
|
package oidc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-20 03:21:45 +00:00
|
|
|
|
|
|
|
"github.com/go-crypt/crypt"
|
2022-12-04 22:37:08 +00:00
|
|
|
"github.com/go-crypt/crypt/algorithm"
|
|
|
|
"github.com/go-crypt/crypt/algorithm/plaintext"
|
2021-05-04 22:06:05 +00:00
|
|
|
)
|
|
|
|
|
2023-03-06 03:58:50 +00:00
|
|
|
// NewHasher returns a new Hasher.
|
|
|
|
func NewHasher() (hasher *Hasher, err error) {
|
|
|
|
hasher = &Hasher{}
|
2022-12-04 22:37:08 +00:00
|
|
|
|
|
|
|
if hasher.decoder, err = crypt.NewDefaultDecoder(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = plaintext.RegisterDecoderPlainText(hasher.decoder); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasher, nil
|
|
|
|
}
|
|
|
|
|
2023-03-06 03:58:50 +00:00
|
|
|
// Hasher implements the fosite.Hasher interface and adaptively compares hashes.
|
|
|
|
type Hasher struct {
|
2022-12-04 22:37:08 +00:00
|
|
|
decoder algorithm.DecoderRegister
|
|
|
|
}
|
|
|
|
|
2021-05-04 22:06:05 +00:00
|
|
|
// Compare compares the hash with the data and returns an error if they don't match.
|
2023-03-06 03:58:50 +00:00
|
|
|
func (h Hasher) Compare(_ context.Context, hash, data []byte) (err error) {
|
2022-12-04 22:37:08 +00:00
|
|
|
var digest algorithm.Digest
|
2022-10-20 03:21:45 +00:00
|
|
|
|
2022-12-04 22:37:08 +00:00
|
|
|
if digest, err = h.decoder.Decode(string(hash)); err != nil {
|
2022-10-20 03:21:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if digest.MatchBytes(data) {
|
|
|
|
return nil
|
2021-05-04 22:06:05 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 03:21:45 +00:00
|
|
|
return errPasswordsDoNotMatch
|
2021-05-04 22:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hash creates a new hash from data.
|
2023-03-06 03:58:50 +00:00
|
|
|
func (h Hasher) Hash(_ context.Context, data []byte) (hash []byte, err error) {
|
2021-05-04 22:06:05 +00:00
|
|
|
return data, nil
|
|
|
|
}
|