2020-07-08 11:56:37 +00:00
|
|
|
package authz
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-08-24 08:06:55 +00:00
|
|
|
"github.com/caos/zitadel/internal/api/grpc"
|
|
|
|
http_util "github.com/caos/zitadel/internal/api/http"
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2020-12-02 07:50:59 +00:00
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
2020-07-08 11:56:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type key int
|
|
|
|
|
|
|
|
const (
|
2020-07-22 12:00:29 +00:00
|
|
|
requestPermissionsKey key = 1
|
|
|
|
dataKey key = 2
|
|
|
|
allPermissionsKey key = 3
|
2020-07-08 11:56:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CtxData struct {
|
|
|
|
UserID string
|
|
|
|
OrgID string
|
|
|
|
ProjectID string
|
|
|
|
AgentID string
|
|
|
|
PreferredLanguage string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctxData CtxData) IsZero() bool {
|
|
|
|
return ctxData.UserID == "" || ctxData.OrgID == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
type Grants []*Grant
|
|
|
|
|
|
|
|
type Grant struct {
|
|
|
|
OrgID string
|
|
|
|
Roles []string
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:34:05 +00:00
|
|
|
func VerifyTokenAndCreateCtxData(ctx context.Context, token, orgID string, t *TokenVerifier, method string) (_ CtxData, err error) {
|
2020-10-21 08:18:34 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2020-07-16 11:51:37 +00:00
|
|
|
if orgID != "" {
|
|
|
|
err = t.ExistsOrg(ctx, orgID)
|
|
|
|
if err != nil {
|
2020-12-14 12:34:05 +00:00
|
|
|
return CtxData{}, errors.ThrowPermissionDenied(nil, "AUTH-Bs7Ds", "Organisation doesn't exist")
|
2020-07-16 11:51:37 +00:00
|
|
|
}
|
2020-07-16 11:27:36 +00:00
|
|
|
}
|
2020-07-16 11:51:37 +00:00
|
|
|
|
2020-08-28 07:44:43 +00:00
|
|
|
userID, clientID, agentID, prefLang, err := verifyAccessToken(ctx, token, t, method)
|
2020-07-08 11:56:37 +00:00
|
|
|
if err != nil {
|
2020-12-14 12:34:05 +00:00
|
|
|
return CtxData{}, err
|
2020-07-08 11:56:37 +00:00
|
|
|
}
|
2020-08-24 08:06:55 +00:00
|
|
|
projectID, origins, err := t.ProjectIDAndOriginsByClientID(ctx, clientID)
|
|
|
|
if err != nil {
|
2020-12-14 12:34:05 +00:00
|
|
|
return CtxData{}, errors.ThrowPermissionDenied(err, "AUTH-GHpw2", "could not read projectid by clientid")
|
2020-08-24 08:06:55 +00:00
|
|
|
}
|
|
|
|
if err := checkOrigin(ctx, origins); err != nil {
|
2020-12-14 12:34:05 +00:00
|
|
|
return CtxData{}, err
|
2020-08-24 08:06:55 +00:00
|
|
|
}
|
2020-12-14 12:34:05 +00:00
|
|
|
return CtxData{
|
|
|
|
UserID: userID,
|
|
|
|
OrgID: orgID,
|
|
|
|
ProjectID: projectID,
|
|
|
|
AgentID: agentID,
|
|
|
|
PreferredLanguage: prefLang,
|
|
|
|
}, nil
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SetCtxData(ctx context.Context, ctxData CtxData) context.Context {
|
|
|
|
return context.WithValue(ctx, dataKey, ctxData)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetCtxData(ctx context.Context) CtxData {
|
|
|
|
ctxData, _ := ctx.Value(dataKey).(CtxData)
|
|
|
|
return ctxData
|
|
|
|
}
|
|
|
|
|
2020-07-22 12:00:29 +00:00
|
|
|
func GetRequestPermissionsFromCtx(ctx context.Context) []string {
|
|
|
|
ctxPermission, _ := ctx.Value(requestPermissionsKey).([]string)
|
|
|
|
return ctxPermission
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAllPermissionsFromCtx(ctx context.Context) []string {
|
|
|
|
ctxPermission, _ := ctx.Value(allPermissionsKey).([]string)
|
2020-07-08 11:56:37 +00:00
|
|
|
return ctxPermission
|
|
|
|
}
|
2020-08-24 08:06:55 +00:00
|
|
|
|
|
|
|
func checkOrigin(ctx context.Context, origins []string) error {
|
|
|
|
origin := grpc.GetGatewayHeader(ctx, http_util.Origin)
|
|
|
|
if origin == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if http_util.IsOriginAllowed(origins, origin) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.ThrowPermissionDenied(nil, "AUTH-DZG21", "Errors.OriginNotAllowed")
|
|
|
|
}
|