| 1 |
package op |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"net/http" |
| 7 |
|
| 8 |
httphelper "github.com/zitadel/oidc/v3/pkg/http" |
| 9 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 10 |
) |
| 11 |
|
| 12 |
type Introspector interface { |
| 13 |
Decoder() httphelper.Decoder |
| 14 |
Crypto() Crypto |
| 15 |
Storage() Storage |
| 16 |
AccessTokenVerifier(context.Context) *AccessTokenVerifier |
| 17 |
} |
| 18 |
|
| 19 |
type IntrospectorJWTProfile interface { |
| 20 |
Introspector |
| 21 |
JWTProfileVerifier(context.Context) JWTProfileVerifier |
| 22 |
} |
| 23 |
|
| 24 |
func introspectionHandler(introspector Introspector) func(http.ResponseWriter, *http.Request) { |
| 25 |
return func(w http.ResponseWriter, r *http.Request) { |
| 26 |
Introspect(w, r, introspector) |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func Introspect(w http.ResponseWriter, r *http.Request, introspector Introspector) { |
| 31 |
ctx, span := Tracer.Start(r.Context(), "Introspect") |
| 32 |
defer span.End() |
| 33 |
r = r.WithContext(ctx) |
| 34 |
|
| 35 |
response := new(oidc.IntrospectionResponse) |
| 36 |
token, clientID, err := ParseTokenIntrospectionRequest(r, introspector) |
| 37 |
if err != nil { |
| 38 |
http.Error(w, err.Error(), http.StatusUnauthorized) |
| 39 |
return |
| 40 |
} |
| 41 |
tokenID, subject, ok := getTokenIDAndSubject(r.Context(), introspector, token) |
| 42 |
if !ok { |
| 43 |
httphelper.MarshalJSON(w, response) |
| 44 |
return |
| 45 |
} |
| 46 |
err = introspector.Storage().SetIntrospectionFromToken(r.Context(), response, tokenID, subject, clientID) |
| 47 |
if err != nil { |
| 48 |
httphelper.MarshalJSON(w, response) |
| 49 |
return |
| 50 |
} |
| 51 |
response.Active = true |
| 52 |
httphelper.MarshalJSON(w, response) |
| 53 |
} |
| 54 |
|
| 55 |
func ParseTokenIntrospectionRequest(r *http.Request, introspector Introspector) (token, clientID string, err error) { |
| 56 |
clientID, authenticated, err := ClientIDFromRequest(r, introspector) |
| 57 |
if err != nil { |
| 58 |
return "", "", err |
| 59 |
} |
| 60 |
if !authenticated { |
| 61 |
return "", "", oidc.ErrInvalidClient().WithParent(ErrNoClientCredentials) |
| 62 |
} |
| 63 |
|
| 64 |
req := new(oidc.IntrospectionRequest) |
| 65 |
err = introspector.Decoder().Decode(req, r.Form) |
| 66 |
if err != nil { |
| 67 |
return "", "", errors.New("unable to parse request") |
| 68 |
} |
| 69 |
|
| 70 |
return req.Token, clientID, nil |
| 71 |
} |
| 72 |
|
| 73 |
type IntrospectionRequest struct { |
| 74 |
*ClientCredentials |
| 75 |
*oidc.IntrospectionRequest |
| 76 |
} |