|
|
|
|
@ -4,6 +4,11 @@ package admin
|
|
|
|
|
import ( |
|
|
|
|
"net/http" |
|
|
|
|
|
|
|
|
|
"github.com/coreos/go-oidc/oidc" |
|
|
|
|
"github.com/go-gorp/gorp" |
|
|
|
|
|
|
|
|
|
"github.com/coreos/dex/client" |
|
|
|
|
"github.com/coreos/dex/db" |
|
|
|
|
"github.com/coreos/dex/schema/adminschema" |
|
|
|
|
"github.com/coreos/dex/user" |
|
|
|
|
"github.com/coreos/dex/user/manager" |
|
|
|
|
@ -11,22 +16,25 @@ import (
|
|
|
|
|
|
|
|
|
|
// AdminAPI provides the logic necessary to implement the Admin API.
|
|
|
|
|
type AdminAPI struct { |
|
|
|
|
userManager *manager.UserManager |
|
|
|
|
userRepo user.UserRepo |
|
|
|
|
passwordInfoRepo user.PasswordInfoRepo |
|
|
|
|
localConnectorID string |
|
|
|
|
userManager *manager.UserManager |
|
|
|
|
userRepo user.UserRepo |
|
|
|
|
passwordInfoRepo user.PasswordInfoRepo |
|
|
|
|
clientIdentityRepo client.ClientIdentityRepo |
|
|
|
|
localConnectorID string |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func NewAdminAPI(userManager *manager.UserManager, userRepo user.UserRepo, pwiRepo user.PasswordInfoRepo, localConnectorID string) *AdminAPI { |
|
|
|
|
// TODO(ericchiang): Swap the DbMap for a storage interface. See #278
|
|
|
|
|
|
|
|
|
|
func NewAdminAPI(dbMap *gorp.DbMap, userManager *manager.UserManager, localConnectorID string) *AdminAPI { |
|
|
|
|
if localConnectorID == "" { |
|
|
|
|
panic("must specify non-blank localConnectorID") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return &AdminAPI{ |
|
|
|
|
userManager: userManager, |
|
|
|
|
userRepo: userRepo, |
|
|
|
|
passwordInfoRepo: pwiRepo, |
|
|
|
|
localConnectorID: localConnectorID, |
|
|
|
|
userManager: userManager, |
|
|
|
|
userRepo: db.NewUserRepo(dbMap), |
|
|
|
|
passwordInfoRepo: db.NewPasswordInfoRepo(dbMap), |
|
|
|
|
clientIdentityRepo: db.NewClientIdentityRepo(dbMap), |
|
|
|
|
localConnectorID: localConnectorID, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -108,6 +116,27 @@ func (a *AdminAPI) GetState() (adminschema.State, error) {
|
|
|
|
|
return state, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type ClientRegistrationRequest struct { |
|
|
|
|
IsAdmin bool `json:"isAdmin"` |
|
|
|
|
Client oidc.ClientMetadata `json:"client"` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (a *AdminAPI) CreateClient(req ClientRegistrationRequest) (oidc.ClientRegistrationResponse, error) { |
|
|
|
|
if err := req.Client.Valid(); err != nil { |
|
|
|
|
return oidc.ClientRegistrationResponse{}, mapError(err) |
|
|
|
|
} |
|
|
|
|
// metadata is guarenteed to have at least one redirect_uri by earlier validation.
|
|
|
|
|
id, err := oidc.GenClientID(req.Client.RedirectURIs[0].Host) |
|
|
|
|
if err != nil { |
|
|
|
|
return oidc.ClientRegistrationResponse{}, mapError(err) |
|
|
|
|
} |
|
|
|
|
c, err := a.clientIdentityRepo.New(id, req.Client, req.IsAdmin) |
|
|
|
|
if err != nil { |
|
|
|
|
return oidc.ClientRegistrationResponse{}, mapError(err) |
|
|
|
|
} |
|
|
|
|
return oidc.ClientRegistrationResponse{ClientID: c.ID, ClientSecret: c.Secret, ClientMetadata: req.Client}, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func mapError(e error) error { |
|
|
|
|
if mapped, ok := errorMap[e]; ok { |
|
|
|
|
return mapped(e) |
|
|
|
|
|