2024-05-16 05:07:56 +00:00
|
|
|
package oidc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zitadel/oidc/v3/pkg/oidc"
|
|
|
|
"github.com/zitadel/oidc/v3/pkg/op"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) ClientCredentialsExchange(ctx context.Context, r *op.ClientRequest[oidc.ClientCredentialsRequest]) (_ *op.Response, err error) {
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() {
|
|
|
|
span.EndWithError(err)
|
|
|
|
err = oidcError(err)
|
|
|
|
}()
|
|
|
|
client, ok := r.Client.(*clientCredentialsClient)
|
|
|
|
if !ok {
|
|
|
|
return nil, zerrors.ThrowInternal(nil, "OIDC-ga0EP", "Error.Internal")
|
|
|
|
}
|
|
|
|
scope, err := op.ValidateAuthReqScopes(client, r.Data.Scope)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-11 09:04:09 +00:00
|
|
|
scope, err = s.checkOrgScopes(ctx, client.resourceOwner, scope)
|
2024-05-16 05:07:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
session, err := s.command.CreateOIDCSession(ctx,
|
2024-09-11 09:04:09 +00:00
|
|
|
client.userID,
|
|
|
|
client.resourceOwner,
|
|
|
|
client.clientID,
|
2024-10-31 14:57:17 +00:00
|
|
|
"", // backChannelLogoutURI not needed for service user session
|
2024-05-16 05:07:56 +00:00
|
|
|
scope,
|
|
|
|
domain.AddAudScopeToAudience(ctx, nil, r.Data.Scope),
|
|
|
|
[]domain.UserAuthMethodType{domain.UserAuthMethodTypePassword},
|
|
|
|
time.Now(),
|
|
|
|
"",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
domain.TokenReasonClientCredentials,
|
|
|
|
nil,
|
|
|
|
false,
|
2024-09-03 13:19:00 +00:00
|
|
|
"",
|
2024-11-12 15:20:48 +00:00
|
|
|
domain.OIDCResponseTypeUnspecified,
|
2024-05-16 05:07:56 +00:00
|
|
|
)
|
2024-06-12 06:42:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-16 05:07:56 +00:00
|
|
|
|
2024-09-11 04:45:59 +00:00
|
|
|
return response(s.accessTokenResponseFromSession(ctx, client, session, "", "", false, true, true, false))
|
2024-05-16 05:07:56 +00:00
|
|
|
}
|