-
Notifications
You must be signed in to change notification settings - Fork 4
/
client_authentication_secret_bcrypt.go
54 lines (40 loc) · 1.3 KB
/
client_authentication_secret_bcrypt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package oauth2
import (
"context"
"fmt"
"golang.org/x/crypto/bcrypt"
"authelia.com/provider/oauth2/x/errorsx"
)
const DefaultBCryptWorkFactor = 12
// NewBCryptClientSecret returns a new BCryptClientSecret given a hash.
func NewBCryptClientSecret(hash string) *BCryptClientSecret {
return &BCryptClientSecret{value: []byte(hash)}
}
// NewBCryptClientSecretPlain returns a new BCryptClientSecret given a plaintext secret.
func NewBCryptClientSecretPlain(rawSecret string, cost int) (secret *BCryptClientSecret, err error) {
hashed, err := bcrypt.GenerateFromPassword([]byte(rawSecret), cost)
if err != nil {
return nil, err
}
return &BCryptClientSecret{value: hashed}, nil
}
type BCryptClientSecret struct {
value []byte
}
func (s *BCryptClientSecret) IsPlainText() (is bool) {
return false
}
func (s *BCryptClientSecret) GetPlainTextValue() (secret []byte, err error) {
return nil, fmt.Errorf("this secret doesn't support plaintext")
}
func (s *BCryptClientSecret) Compare(ctx context.Context, secret []byte) (err error) {
if err = bcrypt.CompareHashAndPassword(s.value, secret); err != nil {
return errorsx.WithStack(err)
}
return nil
}
func (s *BCryptClientSecret) Valid() (valid bool) {
return s != nil && len(s.value) != 0
}