2015-08-18 05:57:27 +05:30
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2015-09-01 07:54:45 +05:30
|
|
|
"encoding/base64"
|
2015-08-18 05:57:27 +05:30
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-09-30 08:37:36 +05:30
|
|
|
"reflect"
|
2015-08-18 05:57:27 +05:30
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
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"
|
2016-02-09 05:31:16 +05:30
|
|
|
|
2016-04-15 04:27:53 +05:30
|
|
|
"github.com/coreos/dex/client"
|
2016-02-09 05:31:16 +05:30
|
|
|
"github.com/coreos/dex/pkg/log"
|
|
|
|
"github.com/coreos/dex/refresh"
|
2016-02-17 07:49:23 +05:30
|
|
|
"github.com/coreos/dex/repo"
|
2016-06-10 04:26:37 +05:30
|
|
|
"github.com/coreos/dex/scope"
|
2015-08-18 05:57:27 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
refreshTokenTableName = "refresh_token"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(table{
|
|
|
|
name: refreshTokenTableName,
|
|
|
|
model: refreshTokenModel{},
|
|
|
|
autoinc: true,
|
|
|
|
pkey: []string{"id"},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type refreshTokenRepo struct {
|
2016-02-17 07:49:23 +05:30
|
|
|
*db
|
2015-08-18 05:57:27 +05:30
|
|
|
tokenGenerator refresh.RefreshTokenGenerator
|
|
|
|
}
|
|
|
|
|
|
|
|
type refreshTokenModel struct {
|
|
|
|
ID int64 `db:"id"`
|
2015-09-01 07:54:45 +05:30
|
|
|
PayloadHash []byte `db:"payload_hash"`
|
2016-06-10 04:26:37 +05:30
|
|
|
UserID string `db:"user_id"`
|
|
|
|
ClientID string `db:"client_id"`
|
2016-07-16 04:30:59 +05:30
|
|
|
ConnectorID string `db:"connector_id"`
|
2016-06-10 04:26:37 +05:30
|
|
|
Scopes string `db:"scopes"`
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// buildToken combines the token ID and token payload to create a new token.
|
2015-09-01 07:54:45 +05:30
|
|
|
func buildToken(tokenID int64, tokenPayload []byte) string {
|
|
|
|
return fmt.Sprintf("%d%s%s", tokenID, refresh.TokenDelimer, base64.URLEncoding.EncodeToString(tokenPayload))
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// parseToken parses a token and returns the token ID and token payload.
|
2015-09-01 07:54:45 +05:30
|
|
|
func parseToken(token string) (int64, []byte, error) {
|
2015-08-18 05:57:27 +05:30
|
|
|
parts := strings.SplitN(token, refresh.TokenDelimer, 2)
|
|
|
|
if len(parts) != 2 {
|
2015-09-01 07:54:45 +05:30
|
|
|
return -1, nil, refresh.ErrorInvalidToken
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
2015-12-10 04:17:21 +05:30
|
|
|
id, err := strconv.ParseInt(parts[0], 10, 64)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
2015-09-01 07:54:45 +05:30
|
|
|
return -1, nil, refresh.ErrorInvalidToken
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
2015-09-01 07:54:45 +05:30
|
|
|
tokenPayload, err := base64.URLEncoding.DecodeString(parts[1])
|
|
|
|
if err != nil {
|
|
|
|
return -1, nil, refresh.ErrorInvalidToken
|
|
|
|
}
|
|
|
|
return id, tokenPayload, nil
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2015-09-01 07:54:45 +05:30
|
|
|
func checkTokenPayload(payloadHash, payload []byte) error {
|
|
|
|
if err := bcrypt.CompareHashAndPassword(payloadHash, payload); err != nil {
|
2015-08-18 05:57:27 +05:30
|
|
|
switch err {
|
|
|
|
case bcrypt.ErrMismatchedHashAndPassword:
|
|
|
|
return refresh.ErrorInvalidToken
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRefreshTokenRepo(dbm *gorp.DbMap) refresh.RefreshTokenRepo {
|
2016-02-17 07:49:23 +05:30
|
|
|
return NewRefreshTokenRepoWithGenerator(dbm, refresh.DefaultRefreshTokenGenerator)
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2016-02-10 01:07:32 +05:30
|
|
|
func NewRefreshTokenRepoWithGenerator(dbm *gorp.DbMap, gen refresh.RefreshTokenGenerator) refresh.RefreshTokenRepo {
|
|
|
|
return &refreshTokenRepo{
|
2016-02-17 07:49:23 +05:30
|
|
|
db: &db{dbm},
|
2016-02-10 01:07:32 +05:30
|
|
|
tokenGenerator: gen,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-16 04:30:59 +05:30
|
|
|
func (r *refreshTokenRepo) Create(userID, clientID, connectorID string, scopes []string) (string, error) {
|
2015-08-18 05:57:27 +05:30
|
|
|
if userID == "" {
|
|
|
|
return "", refresh.ErrorInvalidUserID
|
|
|
|
}
|
|
|
|
if clientID == "" {
|
|
|
|
return "", refresh.ErrorInvalidClientID
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(yifan): Check the number of tokens given to the client-user pair.
|
|
|
|
tokenPayload, err := r.tokenGenerator.Generate()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-09-01 07:54:45 +05:30
|
|
|
payloadHash, err := bcrypt.GenerateFromPassword(tokenPayload, bcrypt.DefaultCost)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
record := &refreshTokenModel{
|
2015-09-01 07:54:45 +05:30
|
|
|
PayloadHash: payloadHash,
|
2015-08-18 05:57:27 +05:30
|
|
|
UserID: userID,
|
|
|
|
ClientID: clientID,
|
2016-07-16 04:30:59 +05:30
|
|
|
ConnectorID: connectorID,
|
2016-06-10 04:26:37 +05:30
|
|
|
Scopes: strings.Join(scopes, " "),
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2016-02-17 07:49:23 +05:30
|
|
|
if err := r.executor(nil).Insert(record); err != nil {
|
2015-08-18 05:57:27 +05:30
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildToken(record.ID, tokenPayload), nil
|
|
|
|
}
|
|
|
|
|
2016-07-16 04:30:59 +05:30
|
|
|
func (r *refreshTokenRepo) Verify(clientID, token string) (userID, connectorID string, scope scope.Scopes, err error) {
|
2015-08-18 05:57:27 +05:30
|
|
|
tokenID, tokenPayload, err := parseToken(token)
|
2015-09-01 07:54:45 +05:30
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
2016-07-16 04:30:59 +05:30
|
|
|
return
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
record, err := r.get(nil, tokenID)
|
|
|
|
if err != nil {
|
2016-07-16 04:30:59 +05:30
|
|
|
return
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if record.ClientID != clientID {
|
2016-07-16 04:30:59 +05:30
|
|
|
return "", "", nil, refresh.ErrorInvalidClientID
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2016-07-16 04:30:59 +05:30
|
|
|
if err = checkTokenPayload(record.PayloadHash, tokenPayload); err != nil {
|
|
|
|
return
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2016-06-10 04:26:37 +05:30
|
|
|
var scopes []string
|
|
|
|
if len(record.Scopes) > 0 {
|
|
|
|
scopes = strings.Split(record.Scopes, " ")
|
|
|
|
}
|
2016-06-11 04:17:40 +05:30
|
|
|
|
2016-07-16 04:30:59 +05:30
|
|
|
return record.UserID, record.ConnectorID, scopes, nil
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (r *refreshTokenRepo) Revoke(userID, token string) error {
|
|
|
|
tokenID, tokenPayload, err := parseToken(token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-17 07:49:23 +05:30
|
|
|
tx, err := r.begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
exec := r.executor(tx)
|
|
|
|
record, err := r.get(tx, tokenID)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if record.UserID != userID {
|
|
|
|
return refresh.ErrorInvalidUserID
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := checkTokenPayload(record.PayloadHash, tokenPayload); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-17 07:49:23 +05:30
|
|
|
deleted, err := exec.Delete(record)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if deleted == 0 {
|
|
|
|
return refresh.ErrorInvalidToken
|
|
|
|
}
|
|
|
|
|
2016-02-17 07:49:23 +05:30
|
|
|
return tx.Commit()
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
2016-02-17 07:49:23 +05:30
|
|
|
|
2016-04-06 23:55:50 +05:30
|
|
|
func (r *refreshTokenRepo) RevokeTokensForClient(userID, clientID string) error {
|
|
|
|
q := fmt.Sprintf("DELETE FROM %s WHERE user_id = $1 AND client_id = $2", r.quote(refreshTokenTableName))
|
|
|
|
_, err := r.executor(nil).Exec(q, userID, clientID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:27:53 +05:30
|
|
|
func (r *refreshTokenRepo) ClientsWithRefreshTokens(userID string) ([]client.Client, error) {
|
2016-04-06 23:55:50 +05:30
|
|
|
q := `SELECT c.* FROM %s as c
|
|
|
|
INNER JOIN %s as r ON c.id = r.client_id WHERE r.user_id = $1;`
|
2016-04-15 04:57:57 +05:30
|
|
|
q = fmt.Sprintf(q, r.quote(clientTableName), r.quote(refreshTokenTableName))
|
|
|
|
var clients []clientModel
|
2016-04-06 23:55:50 +05:30
|
|
|
if _, err := r.executor(nil).Select(&clients, q, userID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:27:53 +05:30
|
|
|
c := make([]client.Client, len(clients))
|
2016-04-06 23:55:50 +05:30
|
|
|
for i, client := range clients {
|
2016-04-15 04:27:53 +05:30
|
|
|
ident, err := client.Client()
|
2016-04-06 23:55:50 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c[i] = *ident
|
|
|
|
// Do not share the secret.
|
|
|
|
c[i].Credentials.Secret = ""
|
|
|
|
}
|
2016-06-10 04:26:37 +05:30
|
|
|
|
2016-04-06 23:55:50 +05:30
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2016-02-17 07:49:23 +05:30
|
|
|
func (r *refreshTokenRepo) get(tx repo.Transaction, tokenID int64) (*refreshTokenModel, error) {
|
|
|
|
ex := r.executor(tx)
|
2015-08-18 05:57:27 +05:30
|
|
|
result, err := ex.Get(refreshTokenModel{}, tokenID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if result == nil {
|
|
|
|
return nil, refresh.ErrorInvalidToken
|
|
|
|
}
|
|
|
|
|
|
|
|
record, ok := result.(*refreshTokenModel)
|
|
|
|
if !ok {
|
2015-09-30 08:37:36 +05:30
|
|
|
log.Errorf("expected refreshTokenModel but found %v", reflect.TypeOf(result))
|
2015-08-18 05:57:27 +05:30
|
|
|
return nil, errors.New("unrecognized model")
|
|
|
|
}
|
|
|
|
return record, nil
|
|
|
|
}
|