Skip to content

Commit

Permalink
fix: interstitial of failed token verification (#103)
Browse files Browse the repository at this point in the history
* fix: Change signed-out & interstitial request state conditions

Set signed-out as default request state and return interstitial ONLY
when the token verification fails with expired or invalid_iat errors.

* chore: Add codeowners
  • Loading branch information
dimkl authored Mar 10, 2023
1 parent b63396c commit 827a001
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @clerkinc/backend-team
24 changes: 20 additions & 4 deletions clerk/middleware_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package clerk

import (
"context"
"errors"
"net"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"

"gopkg.in/square/go-jose.v2/jwt"
)

var urlSchemeRe = regexp.MustCompile(`(^\w+:|^)\/\/`)
Expand Down Expand Up @@ -130,13 +133,26 @@ func WithSessionV2(client Client, verifyTokenOptions ...VerifyTokenOption) func(
}

claims, err := client.VerifyToken(cookieToken.Value, verifyTokenOptions...)
if err == nil && claims.IssuedAt != nil && clientUatTs <= int64(*claims.IssuedAt) {
ctx := context.WithValue(r.Context(), ActiveSessionClaims, claims)
next.ServeHTTP(w, r.WithContext(ctx))

if err == nil {
if claims.IssuedAt != nil && clientUatTs <= int64(*claims.IssuedAt) {
ctx := context.WithValue(r.Context(), ActiveSessionClaims, claims)
next.ServeHTTP(w, r.WithContext(ctx))
return
}

renderInterstitial(client, w)
return
}

renderInterstitial(client, w)
if errors.Is(err, jwt.ErrExpired) || errors.Is(err, jwt.ErrIssuedInTheFuture) {
renderInterstitial(client, w)
return
}

// signed out
next.ServeHTTP(w, r)
return
})
}
}
Expand Down

0 comments on commit 827a001

Please sign in to comment.