2020-03-23 07:01:59 +01:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/caos/logging"
|
2020-04-07 13:23:04 +02:00
|
|
|
"github.com/caos/zitadel/internal/api"
|
|
|
|
grpc_util "github.com/caos/zitadel/internal/api/grpc"
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
"strconv"
|
2020-03-23 07:01:59 +01:00
|
|
|
)
|
|
|
|
|
2020-03-30 11:52:08 +02:00
|
|
|
type key int
|
|
|
|
|
|
|
|
var (
|
|
|
|
permissionsKey key
|
|
|
|
dataKey key
|
|
|
|
)
|
2020-03-23 07:01:59 +01:00
|
|
|
|
|
|
|
type CtxData struct {
|
|
|
|
UserID string
|
|
|
|
OrgID string
|
|
|
|
ProjectID string
|
|
|
|
AgentID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctxData CtxData) IsZero() bool {
|
|
|
|
return ctxData.UserID == "" || ctxData.OrgID == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
type Grants []*Grant
|
|
|
|
|
|
|
|
type Grant struct {
|
|
|
|
OrgID string
|
|
|
|
Roles []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type TokenVerifier interface {
|
|
|
|
VerifyAccessToken(ctx context.Context, token string) (string, string, string, error)
|
|
|
|
ResolveGrants(ctx context.Context, sub, orgID string) ([]*Grant, error)
|
|
|
|
GetProjectIDByClientID(ctx context.Context, clientID string) (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func VerifyTokenAndWriteCtxData(ctx context.Context, token, orgID string, t TokenVerifier) (_ context.Context, err error) {
|
2020-04-07 13:23:04 +02:00
|
|
|
var userID, projectID, clientID, agentID string
|
|
|
|
//TODO: Remove as soon an authentification is implemented
|
|
|
|
if CheckInternal(ctx) {
|
|
|
|
userID = grpc_util.GetHeader(ctx, api.ZitadelUserID)
|
|
|
|
projectID = grpc_util.GetHeader(ctx, api.ZitadelClientID)
|
|
|
|
agentID = grpc_util.GetHeader(ctx, api.ZitadelAgentID)
|
|
|
|
} else {
|
|
|
|
userID, clientID, agentID, err = verifyAccessToken(ctx, token, t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-23 07:01:59 +01:00
|
|
|
|
2020-04-07 13:23:04 +02:00
|
|
|
projectID, err = t.GetProjectIDByClientID(ctx, clientID)
|
|
|
|
logging.LogWithFields("AUTH-GfAoV", "clientID", clientID).OnError(err).Warn("could not read projectid by clientid")
|
|
|
|
}
|
2020-03-30 11:52:08 +02:00
|
|
|
return context.WithValue(ctx, dataKey, CtxData{UserID: userID, OrgID: orgID, ProjectID: projectID, AgentID: agentID}), nil
|
2020-03-23 07:01:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetCtxData(ctx context.Context) CtxData {
|
2020-03-30 11:52:08 +02:00
|
|
|
ctxData, _ := ctx.Value(dataKey).(CtxData)
|
2020-03-30 10:06:48 +02:00
|
|
|
return ctxData
|
2020-03-23 07:01:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetPermissionsFromCtx(ctx context.Context) []string {
|
2020-03-30 11:52:08 +02:00
|
|
|
ctxPermission, _ := ctx.Value(permissionsKey).([]string)
|
2020-03-30 10:06:48 +02:00
|
|
|
return ctxPermission
|
2020-03-23 07:01:59 +01:00
|
|
|
}
|
2020-04-07 13:23:04 +02:00
|
|
|
|
|
|
|
//TODO: Remove as soon an authentification is implemented
|
|
|
|
func CheckInternal(ctx context.Context) bool {
|
|
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
v, ok := md[api.LoginKey]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ok, _ = strconv.ParseBool(v[0])
|
|
|
|
return ok
|
|
|
|
}
|