2015-08-18 05:57:27 +05:30
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/coreos/dex/client"
|
2016-05-12 22:23:01 +05:30
|
|
|
clientmanager "github.com/coreos/dex/client/manager"
|
2015-08-18 05:57:27 +05:30
|
|
|
"github.com/coreos/dex/pkg/log"
|
2016-04-06 23:59:09 +05:30
|
|
|
"github.com/coreos/dex/refresh"
|
2015-08-18 05:57:27 +05:30
|
|
|
schema "github.com/coreos/dex/schema/workerschema"
|
|
|
|
"github.com/coreos/dex/user"
|
2016-05-12 22:23:01 +05:30
|
|
|
usermanager "github.com/coreos/dex/user/manager"
|
2015-08-18 05:57:27 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errorMap = map[error]Error{
|
|
|
|
user.ErrorNotFound: ErrorResourceNotFound,
|
|
|
|
user.ErrorDuplicateEmail: ErrorDuplicateEmail,
|
|
|
|
user.ErrorInvalidEmail: ErrorInvalidEmail,
|
|
|
|
client.ErrorNotFound: ErrorInvalidClient,
|
|
|
|
}
|
|
|
|
|
2016-02-23 17:10:00 +05:30
|
|
|
ErrorInvalidEmail = newError("invalid_email", "invalid email.", http.StatusBadRequest)
|
|
|
|
ErrorVerifiedEmail = newError("verified_email", "Email already verified.", http.StatusBadRequest)
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
ErrorInvalidClient = newError("invalid_client", "invalid email.", http.StatusBadRequest)
|
|
|
|
|
2016-06-29 02:12:20 +05:30
|
|
|
ErrorDuplicateEmail = newError("duplicate_email", "Email already in use.", http.StatusConflict)
|
2015-08-18 05:57:27 +05:30
|
|
|
ErrorResourceNotFound = newError("resource_not_found", "Resource could not be found.", http.StatusNotFound)
|
|
|
|
|
2016-01-20 03:10:16 +05:30
|
|
|
ErrorUnauthorized = newError("unauthorized", "Necessary credentials not provided.", http.StatusUnauthorized)
|
|
|
|
ErrorForbidden = newError("forbidden", "The given user and client are not authorized to make this request.", http.StatusForbidden)
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
ErrorMaxResultsTooHigh = newError("max_results_too_high", fmt.Sprintf("The max number of results per page is %d", maxUsersPerPage), http.StatusBadRequest)
|
|
|
|
|
|
|
|
ErrorInvalidRedirectURL = newError("invalid_redirect_url", "The provided redirect URL is invalid for the given client", http.StatusBadRequest)
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxUsersPerPage = 100
|
|
|
|
)
|
|
|
|
|
|
|
|
func internalError(internal error) Error {
|
|
|
|
return Error{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
Type: "server_error",
|
|
|
|
Desc: "",
|
|
|
|
Internal: internal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newError(typ string, desc string, code int) Error {
|
|
|
|
return Error{
|
|
|
|
Code: code,
|
|
|
|
Type: typ,
|
|
|
|
Desc: desc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error is the error type returned by AdminAPI methods.
|
|
|
|
type Error struct {
|
|
|
|
Type string
|
|
|
|
|
|
|
|
// The HTTP Code to return for this type of error.
|
|
|
|
Code int
|
|
|
|
|
|
|
|
Desc string
|
|
|
|
|
|
|
|
// The underlying error - not to be consumed by external users.
|
|
|
|
Internal error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Error) Error() string {
|
|
|
|
return fmt.Sprintf("%v: Desc: %v Internal: %v", e.Type, e.Desc, e.Internal)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UsersAPI is the user management API for Dex administrators.
|
|
|
|
|
|
|
|
// All calls take a Creds object with the ClientID of the calling app and the
|
|
|
|
// calling User. It is assumed that the clientID has already validated as an
|
|
|
|
// admin app before calling.
|
|
|
|
type UsersAPI struct {
|
2016-05-12 22:23:01 +05:30
|
|
|
userManager *usermanager.UserManager
|
2016-04-15 04:57:57 +05:30
|
|
|
localConnectorID string
|
2016-05-12 22:23:01 +05:30
|
|
|
clientManager *clientmanager.ClientManager
|
2016-04-15 04:57:57 +05:30
|
|
|
refreshRepo refresh.RefreshTokenRepo
|
|
|
|
emailer Emailer
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type Emailer interface {
|
2015-10-31 03:11:00 +05:30
|
|
|
SendInviteEmail(string, url.URL, string) (*url.URL, error)
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type Creds struct {
|
|
|
|
ClientID string
|
|
|
|
User user.User
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:59:09 +05:30
|
|
|
// TODO(ericchiang): Don't pass a dbMap. See #385.
|
2016-05-12 22:23:01 +05:30
|
|
|
func NewUsersAPI(userManager *usermanager.UserManager, clientManager *clientmanager.ClientManager, refreshRepo refresh.RefreshTokenRepo, emailer Emailer, localConnectorID string) *UsersAPI {
|
2015-08-18 05:57:27 +05:30
|
|
|
return &UsersAPI{
|
2016-05-12 22:23:01 +05:30
|
|
|
userManager: userManager,
|
|
|
|
refreshRepo: refreshRepo,
|
|
|
|
clientManager: clientManager,
|
2016-04-15 04:57:57 +05:30
|
|
|
localConnectorID: localConnectorID,
|
|
|
|
emailer: emailer,
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UsersAPI) GetUser(creds Creds, id string) (schema.User, error) {
|
|
|
|
log.Infof("userAPI: GetUser")
|
|
|
|
|
|
|
|
if !u.Authorize(creds) {
|
|
|
|
return schema.User{}, ErrorUnauthorized
|
|
|
|
}
|
|
|
|
|
2016-05-12 22:23:01 +05:30
|
|
|
usr, err := u.userManager.Get(id)
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return schema.User{}, mapError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return userToSchemaUser(usr), nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 02:21:48 +05:30
|
|
|
func (u *UsersAPI) DisableUser(creds Creds, userID string, disable bool) (schema.UserDisableResponse, error) {
|
|
|
|
log.Infof("userAPI: DisableUser")
|
|
|
|
if !u.Authorize(creds) {
|
|
|
|
return schema.UserDisableResponse{}, ErrorUnauthorized
|
|
|
|
}
|
|
|
|
|
2016-05-12 22:23:01 +05:30
|
|
|
if err := u.userManager.Disable(userID, disable); err != nil {
|
2015-09-29 02:21:48 +05:30
|
|
|
return schema.UserDisableResponse{}, mapError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return schema.UserDisableResponse{
|
|
|
|
Ok: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
func (u *UsersAPI) CreateUser(creds Creds, usr schema.User, redirURL url.URL) (schema.UserCreateResponse, error) {
|
|
|
|
log.Infof("userAPI: CreateUser")
|
|
|
|
if !u.Authorize(creds) {
|
|
|
|
return schema.UserCreateResponse{}, ErrorUnauthorized
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, err := generateTempHash()
|
|
|
|
if err != nil {
|
|
|
|
return schema.UserCreateResponse{}, mapError(err)
|
|
|
|
}
|
|
|
|
|
2016-05-12 22:23:01 +05:30
|
|
|
metadata, err := u.clientManager.Metadata(creds.ClientID)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return schema.UserCreateResponse{}, mapError(err)
|
|
|
|
}
|
|
|
|
|
2016-01-13 06:46:28 +05:30
|
|
|
validRedirURL, err := client.ValidRedirectURL(&redirURL, metadata.RedirectURIs)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
2015-09-26 03:38:45 +05:30
|
|
|
return schema.UserCreateResponse{}, ErrorInvalidRedirectURL
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2016-05-12 22:23:01 +05:30
|
|
|
id, err := u.userManager.CreateUser(schemaUserToUser(usr), user.Password(hash), u.localConnectorID)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return schema.UserCreateResponse{}, mapError(err)
|
|
|
|
}
|
|
|
|
|
2016-05-12 22:23:01 +05:30
|
|
|
userUser, err := u.userManager.Get(id)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
2015-09-26 03:38:45 +05:30
|
|
|
return schema.UserCreateResponse{}, mapError(err)
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2015-09-26 03:38:45 +05:30
|
|
|
usr = userToSchemaUser(userUser)
|
|
|
|
|
2015-10-31 03:11:00 +05:30
|
|
|
url, err := u.emailer.SendInviteEmail(usr.Email, validRedirURL, creds.ClientID)
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
// An email is sent only if we don't get a link and there's no error.
|
|
|
|
emailSent := err == nil && url == nil
|
|
|
|
|
|
|
|
var resetLink string
|
|
|
|
if url != nil {
|
|
|
|
resetLink = url.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return schema.UserCreateResponse{
|
|
|
|
User: &usr,
|
|
|
|
EmailSent: emailSent,
|
|
|
|
ResetPasswordLink: resetLink,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-02-23 17:10:00 +05:30
|
|
|
func (u *UsersAPI) ResendEmailInvitation(creds Creds, userID string, redirURL url.URL) (schema.ResendEmailInvitationResponse, error) {
|
|
|
|
log.Infof("userAPI: ResendEmailInvitation")
|
|
|
|
if !u.Authorize(creds) {
|
|
|
|
return schema.ResendEmailInvitationResponse{}, ErrorUnauthorized
|
|
|
|
}
|
|
|
|
|
2016-05-12 22:23:01 +05:30
|
|
|
metadata, err := u.clientManager.Metadata(creds.ClientID)
|
2016-02-23 17:10:00 +05:30
|
|
|
if err != nil {
|
|
|
|
return schema.ResendEmailInvitationResponse{}, mapError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
validRedirURL, err := client.ValidRedirectURL(&redirURL, metadata.RedirectURIs)
|
|
|
|
if err != nil {
|
|
|
|
return schema.ResendEmailInvitationResponse{}, ErrorInvalidRedirectURL
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve user to check if it's already created
|
2016-05-12 22:23:01 +05:30
|
|
|
userUser, err := u.userManager.Get(userID)
|
2016-02-23 17:10:00 +05:30
|
|
|
if err != nil {
|
|
|
|
return schema.ResendEmailInvitationResponse{}, mapError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if email is verified
|
|
|
|
if userUser.EmailVerified {
|
|
|
|
return schema.ResendEmailInvitationResponse{}, ErrorVerifiedEmail
|
|
|
|
}
|
|
|
|
|
|
|
|
url, err := u.emailer.SendInviteEmail(userUser.Email, validRedirURL, creds.ClientID)
|
|
|
|
|
|
|
|
// An email is sent only if we don't get a link and there's no error.
|
|
|
|
emailSent := err == nil && url == nil
|
|
|
|
|
|
|
|
// If email is not sent a reset link will be generated
|
|
|
|
var resetLink string
|
|
|
|
if url != nil {
|
|
|
|
resetLink = url.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return schema.ResendEmailInvitationResponse{
|
|
|
|
EmailSent: emailSent,
|
|
|
|
ResetPasswordLink: resetLink,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
func (u *UsersAPI) ListUsers(creds Creds, maxResults int, nextPageToken string) ([]*schema.User, string, error) {
|
|
|
|
log.Infof("userAPI: ListUsers")
|
|
|
|
|
|
|
|
if !u.Authorize(creds) {
|
|
|
|
return nil, "", ErrorUnauthorized
|
|
|
|
}
|
|
|
|
|
|
|
|
if maxResults > maxUsersPerPage {
|
|
|
|
return nil, "", ErrorMaxResultsTooHigh
|
|
|
|
}
|
|
|
|
|
2016-05-12 22:23:01 +05:30
|
|
|
users, tok, err := u.userManager.List(user.UserFilter{}, maxResults, nextPageToken)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, "", mapError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
list := []*schema.User{}
|
|
|
|
for _, usr := range users {
|
|
|
|
schemaUsr := userToSchemaUser(usr)
|
|
|
|
list = append(list, &schemaUsr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, tok, nil
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:59:09 +05:30
|
|
|
// ListClientsWithRefreshTokens returns all clients issued refresh tokens
|
|
|
|
// for the authenticated user.
|
|
|
|
func (u *UsersAPI) ListClientsWithRefreshTokens(creds Creds, userID string) ([]*schema.RefreshClient, error) {
|
|
|
|
// Users must either be an admin or be requesting data associated with their own account.
|
|
|
|
if !creds.User.Admin && (creds.User.ID != userID) {
|
|
|
|
return nil, ErrorUnauthorized
|
|
|
|
}
|
|
|
|
clientIdentities, err := u.refreshRepo.ClientsWithRefreshTokens(userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
clients := make([]*schema.RefreshClient, len(clientIdentities))
|
|
|
|
|
|
|
|
urlToString := func(u *url.URL) string {
|
|
|
|
if u == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return u.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, identity := range clientIdentities {
|
|
|
|
clients[i] = &schema.RefreshClient{
|
|
|
|
ClientID: identity.Credentials.ID,
|
|
|
|
ClientName: identity.Metadata.ClientName,
|
|
|
|
ClientURI: urlToString(identity.Metadata.ClientURI),
|
|
|
|
LogoURI: urlToString(identity.Metadata.LogoURI),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return clients, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RevokeClient revokes all refresh tokens issued to this client for the
|
|
|
|
// authenticiated user.
|
|
|
|
func (u *UsersAPI) RevokeRefreshTokensForClient(creds Creds, userID, clientID string) error {
|
|
|
|
// Users must either be an admin or be requesting data associated with their own account.
|
|
|
|
if !creds.User.Admin && (creds.User.ID != userID) {
|
|
|
|
return ErrorUnauthorized
|
|
|
|
}
|
|
|
|
return u.refreshRepo.RevokeTokensForClient(userID, clientID)
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
func (u *UsersAPI) Authorize(creds Creds) bool {
|
2015-09-26 05:59:59 +05:30
|
|
|
return creds.User.Admin && !creds.User.Disabled
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func userToSchemaUser(usr user.User) schema.User {
|
|
|
|
return schema.User{
|
|
|
|
Id: usr.ID,
|
|
|
|
Email: usr.Email,
|
|
|
|
EmailVerified: usr.EmailVerified,
|
|
|
|
DisplayName: usr.DisplayName,
|
|
|
|
Admin: usr.Admin,
|
2015-09-29 02:21:48 +05:30
|
|
|
Disabled: usr.Disabled,
|
2015-08-18 05:57:27 +05:30
|
|
|
CreatedAt: usr.CreatedAt.UTC().Format(time.RFC3339),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func schemaUserToUser(usr schema.User) user.User {
|
|
|
|
return user.User{
|
|
|
|
ID: usr.Id,
|
|
|
|
Email: usr.Email,
|
|
|
|
EmailVerified: usr.EmailVerified,
|
|
|
|
DisplayName: usr.DisplayName,
|
|
|
|
Admin: usr.Admin,
|
2015-09-29 02:21:48 +05:30
|
|
|
Disabled: usr.Disabled,
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapError(e error) error {
|
|
|
|
if mapped, ok := errorMap[e]; ok {
|
|
|
|
return mapped
|
|
|
|
}
|
|
|
|
return internalError(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateTempHash() (string, error) {
|
|
|
|
b := make([]byte, 32)
|
|
|
|
n, err := rand.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2015-09-05 01:15:32 +05:30
|
|
|
}
|
|
|
|
if n != 32 {
|
2015-08-18 05:57:27 +05:30
|
|
|
return "", errors.New("unable to read enough random bytes")
|
|
|
|
}
|
|
|
|
return base64.URLEncoding.EncodeToString(b), nil
|
|
|
|
}
|