2021-02-08 10:30:30 +00:00
|
|
|
package domain
|
|
|
|
|
|
|
|
import (
|
2022-08-09 07:45:59 +00:00
|
|
|
"context"
|
2021-02-18 13:48:27 +00:00
|
|
|
"strings"
|
2021-02-08 10:30:30 +00:00
|
|
|
"time"
|
2021-11-03 07:35:24 +00:00
|
|
|
|
2022-08-09 07:45:59 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2022-04-26 23:01:45 +00:00
|
|
|
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
2021-02-08 10:30:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Token struct {
|
|
|
|
es_models.ObjectRoot
|
|
|
|
|
|
|
|
TokenID string
|
|
|
|
ApplicationID string
|
|
|
|
UserAgentID string
|
2021-11-03 07:35:24 +00:00
|
|
|
RefreshTokenID string
|
2021-02-08 10:30:30 +00:00
|
|
|
Audience []string
|
|
|
|
Expiration time.Time
|
|
|
|
Scopes []string
|
|
|
|
PreferredLanguage string
|
2024-03-20 10:18:46 +00:00
|
|
|
Reason TokenReason
|
|
|
|
Actor *TokenActor
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2022-08-09 07:45:59 +00:00
|
|
|
func AddAudScopeToAudience(ctx context.Context, audience, scopes []string) []string {
|
2021-02-18 13:48:27 +00:00
|
|
|
for _, scope := range scopes {
|
2022-08-09 07:45:59 +00:00
|
|
|
if !(strings.HasPrefix(scope, ProjectIDScope) && strings.HasSuffix(scope, AudSuffix)) {
|
|
|
|
continue
|
2021-02-18 13:48:27 +00:00
|
|
|
}
|
2022-08-09 07:45:59 +00:00
|
|
|
projectID := strings.TrimSuffix(strings.TrimPrefix(scope, ProjectIDScope), AudSuffix)
|
|
|
|
if projectID == ProjectIDScopeZITADEL {
|
|
|
|
projectID = authz.GetInstance(ctx).ProjectID()
|
|
|
|
}
|
2023-02-08 08:06:34 +00:00
|
|
|
audience = addProjectID(audience, projectID)
|
2021-02-18 13:48:27 +00:00
|
|
|
}
|
|
|
|
return audience
|
|
|
|
}
|
2023-02-08 08:06:34 +00:00
|
|
|
|
|
|
|
func addProjectID(audience []string, projectID string) []string {
|
|
|
|
for _, a := range audience {
|
|
|
|
if a == projectID {
|
|
|
|
return audience
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(audience, projectID)
|
|
|
|
}
|
2024-03-20 10:18:46 +00:00
|
|
|
|
|
|
|
//go:generate enumer -type TokenReason -transform snake -trimprefix TokenReason -json
|
|
|
|
type TokenReason int
|
|
|
|
|
|
|
|
const (
|
|
|
|
TokenReasonUnspecified TokenReason = iota
|
|
|
|
TokenReasonAuthRequest
|
|
|
|
TokenReasonRefresh
|
|
|
|
TokenReasonJWTProfile
|
|
|
|
TokenReasonClientCredentials
|
|
|
|
TokenReasonExchange
|
|
|
|
TokenReasonImpersonation
|
|
|
|
)
|
|
|
|
|
|
|
|
type TokenActor struct {
|
|
|
|
Actor *TokenActor `json:"actor,omitempty"`
|
|
|
|
UserID string `json:"user_id,omitempty"`
|
|
|
|
Issuer string `json:"issuer,omitempty"`
|
|
|
|
}
|