Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
package rest
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/cortezaproject/corteza/server/pkg/auth"
|
|
"github.com/cortezaproject/corteza/server/pkg/payload"
|
|
"github.com/cortezaproject/corteza/server/system/rest/request"
|
|
"github.com/cortezaproject/corteza/server/system/service"
|
|
"github.com/cortezaproject/corteza/server/system/types"
|
|
)
|
|
|
|
type (
|
|
Auth struct {
|
|
settings *types.AppSettings
|
|
authSvc authUserService
|
|
}
|
|
|
|
authUserResponse struct {
|
|
JWT string `json:"jwt"`
|
|
User *authUserPayload `json:"user"`
|
|
}
|
|
|
|
authUserPayload struct {
|
|
ID uint64 `json:"userID,string"`
|
|
Name string `json:"name"`
|
|
Email string `json:"email"`
|
|
Username string `json:"username"`
|
|
Handle string `json:"handle"`
|
|
|
|
Roles []string `json:"roles"`
|
|
}
|
|
|
|
authUserService interface {
|
|
Impersonate(ctx context.Context, userID uint64) (*types.User, error)
|
|
LoadRoleMemberships(ctx context.Context, user *types.User) error
|
|
}
|
|
)
|
|
|
|
func (Auth) New() *Auth {
|
|
return &Auth{
|
|
settings: service.CurrentSettings,
|
|
authSvc: service.DefaultAuth,
|
|
}
|
|
}
|
|
|
|
// Impersonate implements impersonation functionality
|
|
//
|
|
// This is experimental and internals will most likely change in the future:
|
|
func (ctrl *Auth) Impersonate(ctx context.Context, r *request.AuthImpersonate) (interface{}, error) {
|
|
u, err := ctrl.authSvc.Impersonate(ctx, r.UserID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return ctrl.makePayload(ctx, u)
|
|
}
|
|
|
|
func (ctrl *Auth) makePayload(ctx context.Context, user *types.User) (*authUserResponse, error) {
|
|
if err := ctrl.authSvc.LoadRoleMemberships(ctx, user); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Generate and save the token
|
|
t, err := auth.TokenIssuer.Issue(ctx, auth.WithIdentity(user))
|
|
if err != nil {
|
|
return nil, nil
|
|
}
|
|
|
|
return &authUserResponse{
|
|
JWT: string(t),
|
|
User: &authUserPayload{
|
|
ID: user.ID,
|
|
Name: user.Name,
|
|
Handle: user.Handle,
|
|
Username: user.Username,
|
|
Email: user.Email,
|
|
Roles: payload.Uint64stoa(user.Roles()),
|
|
},
|
|
}, nil
|
|
}
|