| 
 | 1 | +package store  | 
 | 2 | + | 
 | 3 | +import (  | 
 | 4 | +	"context"  | 
 | 5 | + | 
 | 6 | +	"github.com/go-oauth2/oauth2/v4"  | 
 | 7 | +	"github.com/go-oauth2/oauth2/v4/errors"  | 
 | 8 | +	"github.com/go-oauth2/oauth2/v4/models"  | 
 | 9 | +	"golang.org/x/crypto/bcrypt"  | 
 | 10 | +)  | 
 | 11 | + | 
 | 12 | +// Hasher is an interface for hashing and verifying client secrets.  | 
 | 13 | +type Hasher interface {  | 
 | 14 | +	// Hash hashes the given secret and returns the hashed value.  | 
 | 15 | +	Hash(secret string) (string, error)  | 
 | 16 | +	// Verify checks if the hashed secret matches the given secret.  | 
 | 17 | +	Verify(hashedPassword, secret string) error  | 
 | 18 | +}  | 
 | 19 | + | 
 | 20 | +// BcryptHasher is a Hasher implementation using bcrypt for hashing and verifying secrets.  | 
 | 21 | +type BcryptHasher struct{}  | 
 | 22 | + | 
 | 23 | +func (b *BcryptHasher) Hash(secret string) (string, error) {  | 
 | 24 | +	hashed, err := bcrypt.GenerateFromPassword([]byte(secret), bcrypt.DefaultCost)  | 
 | 25 | +	if err != nil {  | 
 | 26 | +		return "", err  | 
 | 27 | +	}  | 
 | 28 | +	return string(hashed), nil  | 
 | 29 | +}  | 
 | 30 | + | 
 | 31 | +func (b *BcryptHasher) Verify(hashed, secret string) error {  | 
 | 32 | +	return bcrypt.CompareHashAndPassword([]byte(hashed), []byte(secret))  | 
 | 33 | +}  | 
 | 34 | + | 
 | 35 | +// ClientInfoWithHash wraps an oauth2.ClientInfo and provides secret verification using a Hasher.  | 
 | 36 | +type ClientInfoWithHash struct {  | 
 | 37 | +	wrapped oauth2.ClientInfo  | 
 | 38 | +	hasher  Hasher  | 
 | 39 | +}  | 
 | 40 | + | 
 | 41 | +// NewClientInfoWithHash creates a new instance of client info supporting hashed secret verification.  | 
 | 42 | +func NewClientInfoWithHash(  | 
 | 43 | +	info oauth2.ClientInfo,  | 
 | 44 | +	hasher Hasher,  | 
 | 45 | +) *ClientInfoWithHash {  | 
 | 46 | +	if info == nil {  | 
 | 47 | +		return nil  | 
 | 48 | +	}  | 
 | 49 | +	return &ClientInfoWithHash{  | 
 | 50 | +		wrapped: info,  | 
 | 51 | +		hasher:  hasher,  | 
 | 52 | +	}  | 
 | 53 | +}  | 
 | 54 | + | 
 | 55 | +// VerifyPassword verifies the given plain secret against the hashed secret.  | 
 | 56 | +// It implements the oauth2.ClientPasswordVerifier interface.  | 
 | 57 | +func (v *ClientInfoWithHash) VerifyPassword(secret string) bool {  | 
 | 58 | +	if secret == "" {  | 
 | 59 | +		return false  | 
 | 60 | +	}  | 
 | 61 | +	err := v.hasher.Verify(v.GetSecret(), secret)  | 
 | 62 | +	return err == nil  | 
 | 63 | +}  | 
 | 64 | + | 
 | 65 | +// GetID returns the client ID.  | 
 | 66 | +func (v *ClientInfoWithHash) GetID() string {  | 
 | 67 | +	return v.wrapped.GetID()  | 
 | 68 | +}  | 
 | 69 | + | 
 | 70 | +// GetSecret returns the hashed client secret.  | 
 | 71 | +func (v *ClientInfoWithHash) GetSecret() string {  | 
 | 72 | +	return v.wrapped.GetSecret()  | 
 | 73 | +}  | 
 | 74 | + | 
 | 75 | +// GetDomain returns the client domain.  | 
 | 76 | +func (v *ClientInfoWithHash) GetDomain() string {  | 
 | 77 | +	return v.wrapped.GetDomain()  | 
 | 78 | +}  | 
 | 79 | + | 
 | 80 | +// GetUserID returns the user ID associated with the client.  | 
 | 81 | +func (v *ClientInfoWithHash) GetUserID() string {  | 
 | 82 | +	return v.wrapped.GetUserID()  | 
 | 83 | +}  | 
 | 84 | + | 
 | 85 | +// IsPublic returns true if the client is public.  | 
 | 86 | +func (v *ClientInfoWithHash) IsPublic() bool {  | 
 | 87 | +	return v.wrapped.IsPublic()  | 
 | 88 | +}  | 
 | 89 | + | 
 | 90 | +// ClientStoreWithHash is a wrapper around oauth2.SavingClientStore that hashes client secrets.  | 
 | 91 | +type ClientStoreWithHash struct {  | 
 | 92 | +	underlying oauth2.SavingClientStore  | 
 | 93 | +	hasher     Hasher  | 
 | 94 | +}  | 
 | 95 | + | 
 | 96 | +// NewClientStoreWithBcrypt creates a new ClientStoreWithHash using bcrypt for hashing.  | 
 | 97 | +//  | 
 | 98 | +// It is a convenience function for creating a store with the default bcrypt hasher.  | 
 | 99 | +// The store will hash client secrets using bcrypt before saving them and would  | 
 | 100 | +// return secret information supporting secret verification against the hashed secret.  | 
 | 101 | +func NewClientStoreWithBcrypt(store oauth2.SavingClientStore) *ClientStoreWithHash {  | 
 | 102 | +	return NewClientStoreWithHash(store, &BcryptHasher{})  | 
 | 103 | +}  | 
 | 104 | + | 
 | 105 | +func NewClientStoreWithHash(underlying oauth2.SavingClientStore, hasher Hasher) *ClientStoreWithHash {  | 
 | 106 | +	if hasher == nil {  | 
 | 107 | +		hasher = &BcryptHasher{}  | 
 | 108 | +	}  | 
 | 109 | +	return &ClientStoreWithHash{  | 
 | 110 | +		underlying: underlying,  | 
 | 111 | +		hasher:     hasher,  | 
 | 112 | +	}  | 
 | 113 | +}  | 
 | 114 | + | 
 | 115 | +// GetByID retrieves client information by ID and returns a ClientInfoWithHash instance.  | 
 | 116 | +func (w *ClientStoreWithHash) GetByID(ctx context.Context, id string) (oauth2.ClientInfo, error) {  | 
 | 117 | +	info, err := w.underlying.GetByID(ctx, id)  | 
 | 118 | +	if err != nil {  | 
 | 119 | +		return nil, err  | 
 | 120 | +	}  | 
 | 121 | +	rval := NewClientInfoWithHash(info, w.hasher)  | 
 | 122 | +	if rval == nil {  | 
 | 123 | +		return nil, errors.ErrInvalidClient  | 
 | 124 | +	}  | 
 | 125 | +	return rval, nil  | 
 | 126 | +}  | 
 | 127 | + | 
 | 128 | +// Save hashes the client secret before saving it to the underlying store.  | 
 | 129 | +func (w *ClientStoreWithHash) Save(  | 
 | 130 | +	ctx context.Context,  | 
 | 131 | +	info oauth2.ClientInfo,  | 
 | 132 | +) error {  | 
 | 133 | +	if info == nil {  | 
 | 134 | +		return errors.ErrInvalidClient  | 
 | 135 | +	}  | 
 | 136 | +	if info.GetSecret() == "" {  | 
 | 137 | +		return errors.ErrInvalidClient  | 
 | 138 | +	}  | 
 | 139 | + | 
 | 140 | +	hashed, err := w.hasher.Hash(info.GetSecret())  | 
 | 141 | +	if err != nil {  | 
 | 142 | +		return err  | 
 | 143 | +	}  | 
 | 144 | +	hashedInfo := models.Client{  | 
 | 145 | +		ID:     info.GetID(),  | 
 | 146 | +		Secret: hashed,  | 
 | 147 | +		Domain: info.GetDomain(),  | 
 | 148 | +		UserID: info.GetUserID(),  | 
 | 149 | +		Public: info.IsPublic(),  | 
 | 150 | +	}  | 
 | 151 | +	return w.underlying.Save(ctx, &hashedInfo)  | 
 | 152 | +}  | 
0 commit comments