Files
zitadel/internal/api/oidc/introspect.go

197 lines
5.5 KiB
Go
Raw Normal View History

2023-11-02 17:27:30 +02:00
package oidc
import (
"context"
2023-11-05 13:18:17 +02:00
"database/sql"
2023-11-02 17:27:30 +02:00
"errors"
"slices"
"time"
"github.com/zitadel/oidc/v3/pkg/oidc"
"github.com/zitadel/oidc/v3/pkg/op"
2023-11-05 13:58:22 +02:00
2023-11-05 13:18:17 +02:00
"github.com/zitadel/zitadel/internal/crypto"
2023-11-02 17:27:30 +02:00
errz "github.com/zitadel/zitadel/internal/errors"
2023-11-05 13:18:17 +02:00
"github.com/zitadel/zitadel/internal/query"
"github.com/zitadel/zitadel/internal/telemetry/tracing"
2023-11-02 17:27:30 +02:00
)
2023-11-05 13:58:22 +02:00
func (s *Server) Introspect(ctx context.Context, r *op.Request[op.IntrospectionRequest]) (resp *op.Response, err error) {
ctx, span := tracing.NewSpan(ctx)
defer func() { span.EndWithError(err) }()
2023-11-14 14:31:58 +02:00
if s.features.LegacyIntrospection {
return s.LegacyServer.Introspect(ctx, r)
}
2023-11-03 17:17:49 +02:00
ctx, cancel := context.WithCancel(ctx)
defer cancel()
clientChan := make(chan *instrospectionClientResult)
go s.instrospectionClientAuth(ctx, r.Data.ClientCredentials, clientChan)
tokenChan := make(chan *introspectionTokenResult)
go s.introspectionToken(ctx, r.Data.Token, tokenChan)
var (
client *instrospectionClientResult
token *introspectionTokenResult
)
// make sure both channels are always read,
// and cancel the context on first error
for i := 0; i < 2; i++ {
var resErr error
select {
case client = <-clientChan:
resErr = client.err
case token = <-tokenChan:
resErr = token.err
}
if resErr == nil {
continue
}
cancel()
// we only care for the first error that occured,
// as the next error is most probably a context error.
if err == nil {
err = resErr
}
}
// only client auth errors should be returned
var target *oidc.Error
if errors.As(err, &target) && target.ErrorType == oidc.UnauthorizedClient {
2023-11-02 17:27:30 +02:00
return nil, err
}
2023-11-03 17:17:49 +02:00
2023-11-05 13:58:22 +02:00
// remaining errors shoudn't be returned to the client,
// so we catch errors here, log them and return the response
// with active: false
defer func() {
if err != nil {
s.getLogger(ctx).ErrorContext(ctx, "oidc introspection", "err", err)
}
resp, err = op.NewResponse(new(oidc.IntrospectionResponse)), nil
}()
2023-11-02 17:27:30 +02:00
if err != nil {
2023-11-05 13:58:22 +02:00
return nil, err
2023-11-02 17:27:30 +02:00
}
2023-11-15 15:44:14 +02:00
// TODO: can we get rid of this seperate query?
if token.isPAT {
if err = s.assertClientScopesForPAT(ctx, token.accessToken, client.clientID, client.projectID); err != nil {
return nil, err
}
}
2023-11-03 17:17:49 +02:00
if err = validateIntrospectionAudience(token.audience, client.clientID, client.projectID); err != nil {
2023-11-05 13:58:22 +02:00
return nil, err
2023-11-02 17:27:30 +02:00
}
2023-11-15 14:49:20 +02:00
userInfo, err := s.userInfo(ctx, token.userID, client.projectID, token.scope, []string{client.projectID})
2023-11-02 17:27:30 +02:00
if err != nil {
2023-11-05 13:58:22 +02:00
return nil, err
2023-11-02 17:27:30 +02:00
}
2023-11-05 13:58:22 +02:00
introspectionResp := &oidc.IntrospectionResponse{
Active: true,
Scope: token.scope,
ClientID: token.clientID,
TokenType: oidc.BearerToken,
Expiration: oidc.FromTime(token.tokenExpiration),
IssuedAt: oidc.FromTime(token.tokenCreation),
NotBefore: oidc.FromTime(token.tokenCreation),
Audience: token.audience,
Issuer: op.IssuerFromContext(ctx),
JWTID: token.tokenID,
}
introspectionResp.SetUserInfo(userInfo)
2023-11-05 13:58:22 +02:00
return op.NewResponse(introspectionResp), nil
2023-11-02 17:27:30 +02:00
}
2023-11-03 17:17:49 +02:00
type instrospectionClientResult struct {
clientID string
projectID string
err error
}
func (s *Server) instrospectionClientAuth(ctx context.Context, cc *op.ClientCredentials, rc chan<- *instrospectionClientResult) {
2023-11-05 13:18:17 +02:00
ctx, span := tracing.NewSpan(ctx)
2023-11-03 17:17:49 +02:00
2023-11-05 13:18:17 +02:00
clientID, projectID, err := func() (string, string, error) {
client, err := s.clientFromCredentials(ctx, cc)
2023-11-02 17:27:30 +02:00
if err != nil {
2023-11-05 13:18:17 +02:00
return "", "", err
2023-11-03 17:17:49 +02:00
}
2023-11-05 13:18:17 +02:00
if cc.ClientAssertion != "" {
verifier := op.NewJWTProfileVerifierKeySet(keySetMap(client.PublicKeys), op.IssuerFromContext(ctx), time.Hour, time.Second)
if _, err := op.VerifyJWTAssertion(ctx, cc.ClientAssertion, verifier); err != nil {
return "", "", oidc.ErrUnauthorizedClient().WithParent(err)
}
} else {
if err := crypto.CompareHash(client.ClientSecret, []byte(cc.ClientSecret), s.hashAlg); err != nil {
return "", "", oidc.ErrUnauthorizedClient().WithParent(err)
2023-11-03 17:17:49 +02:00
}
2023-11-02 17:27:30 +02:00
}
2023-11-03 17:17:49 +02:00
2023-11-05 13:18:17 +02:00
return client.ClientID, client.ProjectID, nil
}()
2023-11-02 17:27:30 +02:00
2023-11-05 13:18:17 +02:00
span.EndWithError(err)
2023-11-03 17:17:49 +02:00
rc <- &instrospectionClientResult{
clientID: clientID,
projectID: projectID,
2023-11-05 13:18:17 +02:00
err: err,
}
}
// clientFromCredentials parses the client ID early,
// and makes a single query for the client for either auth methods.
func (s *Server) clientFromCredentials(ctx context.Context, cc *op.ClientCredentials) (client *query.IntrospectionClient, err error) {
if cc.ClientAssertion != "" {
claims := new(oidc.JWTTokenRequest)
if _, err := oidc.ParseToken(cc.ClientAssertion, claims); err != nil {
return nil, oidc.ErrUnauthorizedClient().WithParent(err)
}
2023-11-13 19:38:52 +02:00
client, err = s.query.GetIntrospectionClientByID(ctx, claims.Issuer, true)
2023-11-05 13:18:17 +02:00
} else {
2023-11-13 19:38:52 +02:00
client, err = s.query.GetIntrospectionClientByID(ctx, cc.ClientID, false)
2023-11-05 13:18:17 +02:00
}
if errors.Is(err, sql.ErrNoRows) {
return nil, oidc.ErrUnauthorizedClient().WithParent(err)
2023-11-02 17:27:30 +02:00
}
2023-11-05 13:18:17 +02:00
// any other error is regarded internal and should not be reported back to the client.
return client, err
2023-11-02 17:27:30 +02:00
}
2023-11-03 17:17:49 +02:00
type introspectionTokenResult struct {
2023-11-15 15:44:14 +02:00
*accessToken
2023-11-03 17:17:49 +02:00
err error
}
2023-11-15 15:44:14 +02:00
func (s *Server) introspectionToken(ctx context.Context, tkn string, rc chan<- *introspectionTokenResult) {
2023-11-05 13:58:22 +02:00
ctx, span := tracing.NewSpan(ctx)
2023-11-15 15:44:14 +02:00
token, err := s.verifyAccessToken(ctx, tkn)
2023-11-05 13:58:22 +02:00
span.EndWithError(err)
2023-11-02 17:27:30 +02:00
2023-11-15 15:44:14 +02:00
rc <- &introspectionTokenResult{
accessToken: token,
err: err,
2023-11-05 13:58:22 +02:00
}
}
2023-11-03 17:17:49 +02:00
func validateIntrospectionAudience(audience []string, clientID, projectID string) error {
if slices.ContainsFunc(audience, func(entry string) bool {
return entry == clientID || entry == projectID
2023-11-02 17:27:30 +02:00
}) {
2023-11-03 17:17:49 +02:00
return nil
2023-11-02 17:27:30 +02:00
}
2023-11-03 17:17:49 +02:00
return errz.ThrowPermissionDenied(nil, "OIDC-sdg3G", "token is not valid for this client")
2023-11-02 17:27:30 +02:00
}