Files
nats-server/auth/plain.go
2015-11-30 17:38:08 -08:00

39 lines
732 B
Go

// Copyright 2014-2015 Apcera Inc. All rights reserved.
package auth
import (
"strings"
"github.com/nats-io/gnatsd/server"
"golang.org/x/crypto/bcrypt"
)
const BcryptPrefix = "$2a$"
func isBcrypt(password string) bool {
return strings.HasPrefix(password, BcryptPrefix)
}
type Plain struct {
Username string
Password string
}
func (p *Plain) Check(c server.ClientAuth) bool {
opts := c.GetOpts()
if p.Username != opts.Username {
return false
}
// Check to see if the password is a bcrypt hash
if isBcrypt(p.Password) {
if err := bcrypt.CompareHashAndPassword([]byte(p.Password), []byte(opts.Password)); err != nil {
return false
}
} else if p.Password != opts.Password {
return false
}
return true
}