2015-08-18 05:57:27 +05:30
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-09-30 08:37:36 +05:30
|
|
|
"reflect"
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
"github.com/coreos/go-oidc/oidc"
|
2015-08-25 04:05:44 +05:30
|
|
|
"github.com/go-gorp/gorp"
|
2015-08-18 05:57:27 +05:30
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
|
|
|
|
"github.com/coreos/dex/client"
|
|
|
|
pcrypto "github.com/coreos/dex/pkg/crypto"
|
|
|
|
"github.com/coreos/dex/pkg/log"
|
2016-05-12 03:05:24 +05:30
|
|
|
"github.com/coreos/dex/repo"
|
2015-08-18 05:57:27 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-04-15 04:57:57 +05:30
|
|
|
clientTableName = "client_identity"
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
bcryptHashCost = 10
|
|
|
|
|
|
|
|
// Blowfish, the algorithm underlying bcrypt, has a maximum
|
|
|
|
// password length of 72. We explicitly track and check this
|
|
|
|
// since the bcrypt library will silently ignore portions of
|
|
|
|
// a password past the first 72 characters.
|
|
|
|
maxSecretLength = 72
|
|
|
|
|
|
|
|
// postgres error codes
|
|
|
|
pgErrorCodeUniqueViolation = "23505" // unique_violation
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(table{
|
2016-04-15 04:57:57 +05:30
|
|
|
name: clientTableName,
|
|
|
|
model: clientModel{},
|
2015-08-18 05:57:27 +05:30
|
|
|
autoinc: false,
|
|
|
|
pkey: []string{"id"},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:57:57 +05:30
|
|
|
func newClientModel(cli client.Client) (*clientModel, error) {
|
2016-04-15 04:27:53 +05:30
|
|
|
secretBytes, err := base64.URLEncoding.DecodeString(cli.Credentials.Secret)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-15 04:27:53 +05:30
|
|
|
hashed, err := bcrypt.GenerateFromPassword([]byte(
|
|
|
|
secretBytes),
|
|
|
|
bcryptHashCost)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bmeta, err := json.Marshal(&cli.Metadata)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:57:57 +05:30
|
|
|
cim := clientModel{
|
2016-04-15 04:27:53 +05:30
|
|
|
ID: cli.Credentials.ID,
|
2015-08-18 05:57:27 +05:30
|
|
|
Secret: hashed,
|
|
|
|
Metadata: string(bmeta),
|
2016-04-15 04:27:53 +05:30
|
|
|
DexAdmin: cli.Admin,
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return &cim, nil
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:57:57 +05:30
|
|
|
type clientModel struct {
|
2015-08-18 05:57:27 +05:30
|
|
|
ID string `db:"id"`
|
|
|
|
Secret []byte `db:"secret"`
|
|
|
|
Metadata string `db:"metadata"`
|
2015-08-20 04:10:36 +05:30
|
|
|
DexAdmin bool `db:"dex_admin"`
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2016-04-15 04:57:57 +05:30
|
|
|
func (m *clientModel) Client() (*client.Client, error) {
|
2016-04-15 04:27:53 +05:30
|
|
|
ci := client.Client{
|
2015-08-18 05:57:27 +05:30
|
|
|
Credentials: oidc.ClientCredentials{
|
2016-04-19 05:22:40 +05:30
|
|
|
ID: m.ID,
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
2016-04-15 04:27:53 +05:30
|
|
|
Admin: m.DexAdmin,
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2016-01-13 06:47:50 +05:30
|
|
|
if err := json.Unmarshal([]byte(m.Metadata), &ci.Metadata); err != nil {
|
2015-08-18 05:57:27 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ci, nil
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:57:57 +05:30
|
|
|
func NewClientRepo(dbm *gorp.DbMap) client.ClientRepo {
|
|
|
|
return newClientRepo(dbm)
|
2016-04-20 06:57:45 +05:30
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientRepoWithSecretGenerator(dbm *gorp.DbMap, secGen SecretGenerator) client.ClientRepo {
|
|
|
|
rep := newClientRepo(dbm)
|
|
|
|
rep.secretGenerator = secGen
|
|
|
|
return rep
|
2016-02-17 07:49:23 +05:30
|
|
|
}
|
|
|
|
|
2016-04-15 04:57:57 +05:30
|
|
|
func newClientRepo(dbm *gorp.DbMap) *clientRepo {
|
2016-04-20 06:57:45 +05:30
|
|
|
return &clientRepo{
|
|
|
|
db: &db{dbm},
|
|
|
|
secretGenerator: DefaultSecretGenerator,
|
|
|
|
}
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2016-04-15 04:57:57 +05:30
|
|
|
func NewClientRepoFromClients(dbm *gorp.DbMap, clients []client.Client) (client.ClientRepo, error) {
|
|
|
|
repo := newClientRepo(dbm)
|
2016-02-17 07:49:23 +05:30
|
|
|
tx, err := repo.begin()
|
2016-02-09 05:31:16 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
2016-02-17 07:49:23 +05:30
|
|
|
exec := repo.executor(tx)
|
2015-08-18 05:57:27 +05:30
|
|
|
for _, c := range clients {
|
2016-04-12 05:01:50 +05:30
|
|
|
if c.Credentials.Secret == "" {
|
|
|
|
return nil, fmt.Errorf("client %q has no secret", c.Credentials.ID)
|
|
|
|
}
|
2016-04-15 04:57:57 +05:30
|
|
|
cm, err := newClientModel(c)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-17 07:49:23 +05:30
|
|
|
err = exec.Insert(cm)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2016-02-09 05:31:16 +05:30
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-17 07:49:23 +05:30
|
|
|
return repo, nil
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2016-04-15 04:57:57 +05:30
|
|
|
type clientRepo struct {
|
2016-02-17 07:49:23 +05:30
|
|
|
*db
|
2016-04-20 06:57:45 +05:30
|
|
|
secretGenerator SecretGenerator
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2016-05-12 03:05:24 +05:30
|
|
|
func (r *clientRepo) Get(tx repo.Transaction, clientID string) (client.Client, error) {
|
|
|
|
m, err := r.executor(tx).Get(clientModel{}, clientID)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err == sql.ErrNoRows || m == nil {
|
2016-04-15 04:27:53 +05:30
|
|
|
return client.Client{}, client.ErrorNotFound
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
if err != nil {
|
2016-04-15 04:27:53 +05:30
|
|
|
return client.Client{}, err
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2016-04-15 04:57:57 +05:30
|
|
|
cim, ok := m.(*clientModel)
|
2015-08-18 05:57:27 +05:30
|
|
|
if !ok {
|
2016-04-15 04:27:53 +05:30
|
|
|
log.Errorf("expected clientModel but found %v", reflect.TypeOf(m))
|
|
|
|
return client.Client{}, errors.New("unrecognized model")
|
|
|
|
}
|
|
|
|
|
|
|
|
ci, err := cim.Client()
|
|
|
|
if err != nil {
|
|
|
|
return client.Client{}, err
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2016-04-15 04:27:53 +05:30
|
|
|
return *ci, nil
|
|
|
|
}
|
|
|
|
|
2016-05-12 03:05:24 +05:30
|
|
|
func (r *clientRepo) Metadata(tx repo.Transaction, clientID string) (*oidc.ClientMetadata, error) {
|
|
|
|
c, err := r.Get(tx, clientID)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:27:53 +05:30
|
|
|
return &c.Metadata, nil
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2016-04-15 04:57:57 +05:30
|
|
|
func (r *clientRepo) IsDexAdmin(clientID string) (bool, error) {
|
|
|
|
m, err := r.executor(nil).Get(clientModel{}, clientID)
|
2015-08-18 05:57:27 +05:30
|
|
|
if m == nil || err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:57:57 +05:30
|
|
|
cim, ok := m.(*clientModel)
|
2015-08-18 05:57:27 +05:30
|
|
|
if !ok {
|
2016-04-15 04:57:57 +05:30
|
|
|
log.Errorf("expected clientModel but found %v", reflect.TypeOf(m))
|
2015-08-18 05:57:27 +05:30
|
|
|
return false, errors.New("unrecognized model")
|
|
|
|
}
|
|
|
|
|
|
|
|
return cim.DexAdmin, nil
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:57:57 +05:30
|
|
|
func (r *clientRepo) SetDexAdmin(clientID string, isAdmin bool) error {
|
2016-02-17 07:49:23 +05:30
|
|
|
tx, err := r.begin()
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-09 05:31:16 +05:30
|
|
|
defer tx.Rollback()
|
2016-02-17 07:49:23 +05:30
|
|
|
exec := r.executor(tx)
|
2015-08-18 05:57:27 +05:30
|
|
|
|
2016-04-15 04:57:57 +05:30
|
|
|
m, err := exec.Get(clientModel{}, clientID)
|
2015-08-18 05:57:27 +05:30
|
|
|
if m == nil || err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:57:57 +05:30
|
|
|
cim, ok := m.(*clientModel)
|
2015-08-18 05:57:27 +05:30
|
|
|
if !ok {
|
2016-04-15 04:57:57 +05:30
|
|
|
log.Errorf("expected clientModel but found %v", reflect.TypeOf(m))
|
2015-08-18 05:57:27 +05:30
|
|
|
return errors.New("unrecognized model")
|
|
|
|
}
|
|
|
|
|
|
|
|
cim.DexAdmin = isAdmin
|
2016-02-17 07:49:23 +05:30
|
|
|
_, err = exec.Update(cim)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-09 05:31:16 +05:30
|
|
|
return tx.Commit()
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2016-05-12 03:05:24 +05:30
|
|
|
func (r *clientRepo) Authenticate(tx repo.Transaction, creds oidc.ClientCredentials) (bool, error) {
|
|
|
|
m, err := r.executor(tx).Get(clientModel{}, creds.ID)
|
2015-08-18 05:57:27 +05:30
|
|
|
if m == nil || err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:57:57 +05:30
|
|
|
cim, ok := m.(*clientModel)
|
2015-08-18 05:57:27 +05:30
|
|
|
if !ok {
|
2016-04-15 04:57:57 +05:30
|
|
|
log.Errorf("expected clientModel but found %v", reflect.TypeOf(m))
|
2015-08-18 05:57:27 +05:30
|
|
|
return false, errors.New("unrecognized model")
|
|
|
|
}
|
|
|
|
|
|
|
|
dec, err := base64.URLEncoding.DecodeString(creds.Secret)
|
|
|
|
if err != nil {
|
2016-01-16 05:01:29 +05:30
|
|
|
log.Errorf("error Decoding client creds: %v", err)
|
2015-08-18 05:57:27 +05:30
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dec) > maxSecretLength {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = bcrypt.CompareHashAndPassword(cim.Secret, dec) == nil
|
|
|
|
return ok, nil
|
|
|
|
}
|
|
|
|
|
2016-03-03 01:32:55 +05:30
|
|
|
var alreadyExistsCheckers []func(err error) bool
|
|
|
|
|
|
|
|
func registerAlreadyExistsChecker(f func(err error) bool) {
|
|
|
|
alreadyExistsCheckers = append(alreadyExistsCheckers, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// isAlreadyExistsErr detects database error codes for failing a unique constraint.
|
|
|
|
//
|
|
|
|
// Because database drivers are optionally compiled, use registerAlreadyExistsChecker to
|
|
|
|
// register driver specific implementations.
|
|
|
|
func isAlreadyExistsErr(err error) bool {
|
|
|
|
for _, checker := range alreadyExistsCheckers {
|
|
|
|
if checker(err) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-04-20 06:57:45 +05:30
|
|
|
type SecretGenerator func() ([]byte, error)
|
|
|
|
|
|
|
|
func DefaultSecretGenerator() ([]byte, error) {
|
|
|
|
return pcrypto.RandBytes(maxSecretLength)
|
|
|
|
}
|
|
|
|
|
2016-05-12 03:05:24 +05:30
|
|
|
func (r *clientRepo) New(tx repo.Transaction, cli client.Client) (*oidc.ClientCredentials, error) {
|
2016-04-20 06:57:45 +05:30
|
|
|
secret, err := r.secretGenerator()
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:27:53 +05:30
|
|
|
cli.Credentials.Secret = base64.URLEncoding.EncodeToString(secret)
|
2016-04-15 04:57:57 +05:30
|
|
|
cim, err := newClientModel(cli)
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-12 03:05:24 +05:30
|
|
|
if err := r.executor(tx).Insert(cim); err != nil {
|
2016-03-03 01:32:55 +05:30
|
|
|
if isAlreadyExistsErr(err) {
|
|
|
|
err = errors.New("client ID already exists")
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cc := oidc.ClientCredentials{
|
2016-04-15 04:27:53 +05:30
|
|
|
ID: cli.Credentials.ID,
|
|
|
|
Secret: cli.Credentials.Secret,
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return &cc, nil
|
|
|
|
}
|
|
|
|
|
2016-05-12 03:05:24 +05:30
|
|
|
func (r *clientRepo) All(tx repo.Transaction) ([]client.Client, error) {
|
2016-04-15 04:57:57 +05:30
|
|
|
qt := r.quote(clientTableName)
|
2015-08-18 05:57:27 +05:30
|
|
|
q := fmt.Sprintf("SELECT * FROM %s", qt)
|
2016-05-12 03:05:24 +05:30
|
|
|
objs, err := r.executor(tx).Select(&clientModel{}, q)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:27:53 +05:30
|
|
|
cs := make([]client.Client, len(objs))
|
2015-08-18 05:57:27 +05:30
|
|
|
for i, obj := range objs {
|
2016-04-15 04:57:57 +05:30
|
|
|
m, ok := obj.(*clientModel)
|
2015-08-18 05:57:27 +05:30
|
|
|
if !ok {
|
2016-04-15 04:57:57 +05:30
|
|
|
return nil, errors.New("unable to cast client identity to clientModel")
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2016-04-15 04:27:53 +05:30
|
|
|
ci, err := m.Client()
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cs[i] = *ci
|
|
|
|
}
|
|
|
|
return cs, nil
|
|
|
|
}
|