| 1 |
package op |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"net/http" |
| 7 |
"net/url" |
| 8 |
"strings" |
| 9 |
|
| 10 |
httphelper "github.com/zitadel/oidc/v3/pkg/http" |
| 11 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 12 |
) |
| 13 |
|
| 14 |
type Revoker interface { |
| 15 |
Decoder() httphelper.Decoder |
| 16 |
Crypto() Crypto |
| 17 |
Storage() Storage |
| 18 |
AccessTokenVerifier(context.Context) *AccessTokenVerifier |
| 19 |
AuthMethodPrivateKeyJWTSupported() bool |
| 20 |
AuthMethodPostSupported() bool |
| 21 |
} |
| 22 |
|
| 23 |
type RevokerJWTProfile interface { |
| 24 |
Revoker |
| 25 |
JWTProfileVerifier(context.Context) *JWTProfileVerifier |
| 26 |
} |
| 27 |
|
| 28 |
func revocationHandler(revoker Revoker) func(http.ResponseWriter, *http.Request) { |
| 29 |
return func(w http.ResponseWriter, r *http.Request) { |
| 30 |
Revoke(w, r, revoker) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func Revoke(w http.ResponseWriter, r *http.Request, revoker Revoker) { |
| 35 |
ctx, span := Tracer.Start(r.Context(), "Revoke") |
| 36 |
r = r.WithContext(ctx) |
| 37 |
defer span.End() |
| 38 |
|
| 39 |
token, tokenTypeHint, clientID, err := ParseTokenRevocationRequest(r, revoker) |
| 40 |
if err != nil { |
| 41 |
RevocationRequestError(w, r, err) |
| 42 |
return |
| 43 |
} |
| 44 |
var subject string |
| 45 |
doDecrypt := true |
| 46 |
if tokenTypeHint != "access_token" { |
| 47 |
userID, tokenID, err := revoker.Storage().GetRefreshTokenInfo(r.Context(), clientID, token) |
| 48 |
if err != nil { |
| 49 |
// An invalid refresh token means that we'll try other things (leaving doDecrypt==true) |
| 50 |
if !errors.Is(err, ErrInvalidRefreshToken) { |
| 51 |
RevocationRequestError(w, r, oidc.ErrServerError().WithParent(err)) |
| 52 |
return |
| 53 |
} |
| 54 |
} else { |
| 55 |
token = tokenID |
| 56 |
subject = userID |
| 57 |
doDecrypt = false |
| 58 |
} |
| 59 |
} |
| 60 |
if doDecrypt { |
| 61 |
tokenID, userID, ok := getTokenIDAndSubjectForRevocation(r.Context(), revoker, token) |
| 62 |
if ok { |
| 63 |
token = tokenID |
| 64 |
subject = userID |
| 65 |
} |
| 66 |
} |
| 67 |
if err := revoker.Storage().RevokeToken(r.Context(), token, subject, clientID); err != nil { |
| 68 |
RevocationRequestError(w, r, err) |
| 69 |
return |
| 70 |
} |
| 71 |
httphelper.MarshalJSON(w, nil) |
| 72 |
} |
| 73 |
|
| 74 |
func ParseTokenRevocationRequest(r *http.Request, revoker Revoker) (token, tokenTypeHint, clientID string, err error) { |
| 75 |
ctx, span := Tracer.Start(r.Context(), "ParseTokenRevocationRequest") |
| 76 |
r = r.WithContext(ctx) |
| 77 |
defer span.End() |
| 78 |
|
| 79 |
err = r.ParseForm() |
| 80 |
if err != nil { |
| 81 |
return "", "", "", oidc.ErrInvalidRequest().WithDescription("unable to parse request").WithParent(err) |
| 82 |
} |
| 83 |
req := new(struct { |
| 84 |
oidc.RevocationRequest |
| 85 |
oidc.ClientAssertionParams // for auth_method private_key_jwt |
| 86 |
ClientID string `schema:"client_id"` // for auth_method none and post |
| 87 |
ClientSecret string `schema:"client_secret"` // for auth_method post |
| 88 |
}) |
| 89 |
err = revoker.Decoder().Decode(req, r.Form) |
| 90 |
if err != nil { |
| 91 |
return "", "", "", oidc.ErrInvalidRequest().WithDescription("error decoding form").WithParent(err) |
| 92 |
} |
| 93 |
if req.ClientAssertionType == oidc.ClientAssertionTypeJWTAssertion { |
| 94 |
revokerJWTProfile, ok := revoker.(RevokerJWTProfile) |
| 95 |
if !ok || !revoker.AuthMethodPrivateKeyJWTSupported() { |
| 96 |
return "", "", "", oidc.ErrInvalidClient().WithDescription("auth_method private_key_jwt not supported") |
| 97 |
} |
| 98 |
profile, err := VerifyJWTAssertion(r.Context(), req.ClientAssertion, revokerJWTProfile.JWTProfileVerifier(r.Context())) |
| 99 |
if err == nil { |
| 100 |
return req.Token, req.TokenTypeHint, profile.Issuer, nil |
| 101 |
} |
| 102 |
return "", "", "", err |
| 103 |
} |
| 104 |
clientID, clientSecret, ok := r.BasicAuth() |
| 105 |
if ok { |
| 106 |
clientID, err = url.QueryUnescape(clientID) |
| 107 |
if err != nil { |
| 108 |
return "", "", "", oidc.ErrInvalidClient().WithDescription("invalid basic auth header").WithParent(err) |
| 109 |
} |
| 110 |
clientSecret, err = url.QueryUnescape(clientSecret) |
| 111 |
if err != nil { |
| 112 |
return "", "", "", oidc.ErrInvalidClient().WithDescription("invalid basic auth header").WithParent(err) |
| 113 |
} |
| 114 |
if err = AuthorizeClientIDSecret(r.Context(), clientID, clientSecret, revoker.Storage()); err != nil { |
| 115 |
return "", "", "", err |
| 116 |
} |
| 117 |
return req.Token, req.TokenTypeHint, clientID, nil |
| 118 |
} |
| 119 |
if req.ClientID == "" { |
| 120 |
return "", "", "", oidc.ErrInvalidClient().WithDescription("invalid authorization") |
| 121 |
} |
| 122 |
client, err := revoker.Storage().GetClientByClientID(r.Context(), req.ClientID) |
| 123 |
if err != nil { |
| 124 |
return "", "", "", oidc.ErrInvalidClient().WithParent(err) |
| 125 |
} |
| 126 |
if req.ClientSecret == "" { |
| 127 |
if client.AuthMethod() != oidc.AuthMethodNone { |
| 128 |
return "", "", "", oidc.ErrInvalidClient().WithDescription("invalid authorization") |
| 129 |
} |
| 130 |
return req.Token, req.TokenTypeHint, req.ClientID, nil |
| 131 |
} |
| 132 |
if client.AuthMethod() == oidc.AuthMethodPost && !revoker.AuthMethodPostSupported() { |
| 133 |
return "", "", "", oidc.ErrInvalidClient().WithDescription("auth_method post not supported") |
| 134 |
} |
| 135 |
if err = AuthorizeClientIDSecret(r.Context(), req.ClientID, req.ClientSecret, revoker.Storage()); err != nil { |
| 136 |
return "", "", "", err |
| 137 |
} |
| 138 |
return req.Token, req.TokenTypeHint, req.ClientID, nil |
| 139 |
} |
| 140 |
|
| 141 |
func RevocationRequestError(w http.ResponseWriter, r *http.Request, err error) { |
| 142 |
statusErr := RevocationError(err) |
| 143 |
httphelper.MarshalJSONWithStatus(w, statusErr.parent, statusErr.statusCode) |
| 144 |
} |
| 145 |
|
| 146 |
func RevocationError(err error) StatusError { |
| 147 |
e := oidc.DefaultToServerError(err, err.Error()) |
| 148 |
status := http.StatusBadRequest |
| 149 |
switch e.ErrorType { |
| 150 |
case oidc.InvalidClient: |
| 151 |
status = 401 |
| 152 |
case oidc.ServerError: |
| 153 |
status = 500 |
| 154 |
} |
| 155 |
return NewStatusError(e, status) |
| 156 |
} |
| 157 |
|
| 158 |
func getTokenIDAndSubjectForRevocation(ctx context.Context, userinfoProvider UserinfoProvider, accessToken string) (string, string, bool) { |
| 159 |
ctx, span := Tracer.Start(ctx, "getTokenIDAndSubjectForRevocation") |
| 160 |
defer span.End() |
| 161 |
|
| 162 |
tokenIDSubject, err := userinfoProvider.Crypto().Decrypt(accessToken) |
| 163 |
if err == nil { |
| 164 |
splitToken := strings.Split(tokenIDSubject, ":") |
| 165 |
if len(splitToken) != 2 { |
| 166 |
return "", "", false |
| 167 |
} |
| 168 |
return splitToken[0], splitToken[1], true |
| 169 |
} |
| 170 |
accessTokenClaims, err := VerifyAccessToken[*oidc.AccessTokenClaims](ctx, accessToken, userinfoProvider.AccessTokenVerifier(ctx)) |
| 171 |
if err != nil { |
| 172 |
return "", "", false |
| 173 |
} |
| 174 |
return accessTokenClaims.JWTID, accessTokenClaims.Subject, true |
| 175 |
} |