Documentation
¶
Overview ¶
Package password hashes and verifies passwords.
New hashes use argon2id in the PHC string format:
$argon2id$v=19$m=19456,t=2,p=1$<salt>$<hash>
bcrypt hashes ($2a$, $2b$, $2y$) are accepted for verification only, so existing users can be migrated: after a successful login, NeedsRehash reports true and the caller stores a fresh argon2id hash.
Index ¶
- Constants
- Variables
- func NormalizeLogin(login string) string
- type Argon2id
- type CredentialStore
- type Hasher
- type Params
- type Policy
- type Provider
- func (p *Provider) Authenticate(ctx context.Context, params map[string]string) (*provider.Identity, error)
- func (p *Provider) Name() string
- func (p *Provider) Register(ctx context.Context, params map[string]string) (*provider.Identity, error)
- func (p *Provider) Unregister(ctx context.Context, providerID string) error
Examples ¶
Constants ¶
const ProviderName = "password"
ProviderName is the provider name used in iam.AuthRequest.Provider.
Variables ¶
var ( // ErrTooShort is returned by Policy.Check. ErrTooShort = errors.New("password: too short") // ErrTooLong is returned by Policy.Check. ErrTooLong = errors.New("password: too long") )
var DefaultParams = Params{Memory: 19 * 1024, Iterations: 2, Parallelism: 1, SaltLength: 16, KeyLength: 32}
DefaultParams is the OWASP baseline for argon2id (19 MiB, t=2, p=1).
var DefaultPolicy = Policy{MinLength: 12, MaxLength: 1024}
DefaultPolicy follows NIST SP 800-63B guidance: length over composition rules.
var ( // ErrMalformedHash is returned for encoded hashes that cannot be parsed. ErrMalformedHash = errors.New("password: malformed hash") )
Functions ¶
func NormalizeLogin ¶
NormalizeLogin trims spaces and lower-cases a login name.
Types ¶
type Argon2id ¶
type Argon2id struct {
// contains filtered or unexported fields
}
Argon2id is the default Hasher.
func NewArgon2id ¶
NewArgon2id returns an argon2id hasher. maxConcurrent bounds how many hash computations run at once (each uses params.Memory KiB); zero means GOMAXPROCS.
Example ¶
package main
import (
"context"
"fmt"
"strings"
"github.com/kararnab/iam/v2/password"
)
func main() {
ctx := context.Background()
h, _ := password.NewArgon2id(password.DefaultParams, 0)
encoded, _ := h.Hash(ctx, "correct horse battery staple")
fmt.Println(strings.HasPrefix(encoded, "$argon2id$v=19$m=19456,t=2,p=1$"))
ok, _ := h.Verify(ctx, "correct horse battery staple", encoded)
bad, _ := h.Verify(ctx, "wrong", encoded)
fmt.Println(ok, bad, h.NeedsRehash(encoded))
}
Output: true true false false
func (*Argon2id) NeedsRehash ¶
NeedsRehash implements Hasher.
type CredentialStore ¶
type CredentialStore interface {
// GetCredential returns the encoded hash for login, or iam.ErrNotFound.
GetCredential(ctx context.Context, login string) (encodedHash string, err error)
// CreateCredential stores a new credential, or returns iam.ErrConflict.
CreateCredential(ctx context.Context, login, encodedHash string) error
// UpdateCredential replaces an existing hash (rehash, password change).
UpdateCredential(ctx context.Context, login, encodedHash string) error
// DeleteCredential removes a credential. Missing is not an error.
DeleteCredential(ctx context.Context, login string) error
}
CredentialStore persists password hashes by login name. It is implemented by the application (see memstore for a reference implementation).
Logins are normalized by the provider (trimmed, lower-cased) before they reach the store.
type Hasher ¶
type Hasher interface {
// Hash returns an encoded hash of password.
Hash(ctx context.Context, password string) (string, error)
// Verify reports whether password matches encoded. A malformed encoded
// hash is an error; a wrong password is (false, nil).
Verify(ctx context.Context, password, encoded string) (bool, error)
// NeedsRehash reports whether encoded should be replaced by a new Hash,
// because it uses another algorithm or other parameters.
NeedsRehash(encoded string) bool
}
Hasher hashes and verifies passwords.
type Params ¶
type Params struct {
Memory uint32 // KiB
Iterations uint32
Parallelism uint8
SaltLength uint32 // bytes
KeyLength uint32 // bytes
}
Params are argon2id parameters.
type Policy ¶
type Policy struct {
MinLength int // characters; DefaultPolicy uses 12
MaxLength int // bytes; DefaultPolicy uses 1024
}
Policy holds password length rules. Lengths are counted in Unicode characters for the minimum and bytes for the maximum (which only exists to bound hashing cost).
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider authenticates logins with passwords.
Params for Authenticate and Register: "username" and "password". The identity's ProviderID is the normalized login, which the application links to a subject through iam.IdentityStore.
func NewProvider ¶
func NewProvider(creds CredentialStore, hasher Hasher, policy Policy) (*Provider, error)
NewProvider returns a password provider. A zero Policy means DefaultPolicy.
func (*Provider) Authenticate ¶
func (p *Provider) Authenticate(ctx context.Context, params map[string]string) (*provider.Identity, error)
Authenticate implements provider.AuthProvider.