2023-02-08 09:06:34 +01:00
|
|
|
package oidc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-10-17 18:19:51 +03:00
|
|
|
"github.com/zitadel/oidc/v3/pkg/oidc"
|
|
|
|
"github.com/zitadel/oidc/v3/pkg/op"
|
2023-02-08 09:06:34 +01:00
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2024-01-18 08:10:49 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-02-08 09:06:34 +01:00
|
|
|
)
|
|
|
|
|
2024-01-18 08:10:49 +02:00
|
|
|
func (o *OPStorage) JWTProfileTokenType(ctx context.Context, request op.TokenRequest) (_ op.AccessTokenType, err error) {
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() {
|
|
|
|
err = oidcError(err)
|
|
|
|
span.EndWithError(err)
|
|
|
|
}()
|
|
|
|
|
2023-02-08 09:06:34 +01:00
|
|
|
mapJWTProfileScopesToAudience(ctx, request)
|
2023-11-21 14:11:38 +02:00
|
|
|
user, err := o.query.GetUserByID(ctx, false, request.GetSubject())
|
2023-02-08 09:06:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
// the user should always be a machine, but let's just be sure
|
|
|
|
if user.Machine == nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return 0, zerrors.ThrowInvalidArgument(nil, "OIDC-jk26S", "invalid client type")
|
2023-02-08 09:06:34 +01:00
|
|
|
}
|
|
|
|
return accessTokenTypeToOIDC(user.Machine.AccessTokenType), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapJWTProfileScopesToAudience(ctx context.Context, request op.TokenRequest) {
|
|
|
|
// the request should always be a JWTTokenRequest, but let's make sure
|
|
|
|
jwt, ok := request.(*oidc.JWTTokenRequest)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
jwt.Audience = domain.AddAudScopeToAudience(ctx, jwt.Audience, jwt.Scopes)
|
|
|
|
}
|