Skip to content

Conversation

@pblazej
Copy link

@pblazej pblazej commented Oct 20, 2025

Enables "caching token source" on the client without 3rd party dependencies.

@pblazej
Copy link
Author

pblazej commented Oct 20, 2025

@1egoman I'm thinking of removing JWTKit dependency in Swift, what else do you need for JS - something like:

    pub fn with_unverified_options(
        token: &str, 
        validate_exp: bool,
        validate_nbf: bool,
        leeway: Option<Duration>,
    ) -> Result<Self, AccessTokenError>

@1egoman
Copy link

1egoman commented Oct 20, 2025

@pblazej I think the unverified_token function you have exposed would work for an eventual javascript implementation. In addition, it would be good to pass Claims through the ffi layer as a type that can be used by client implementations as well. I don't think there's a need to control conditional exp / nbf validation in javascript, and IMO a leeway type parameter should be standardized across all sdks for validation and shouldn't be something some of them set in a special way.

As an unrelated FYI, I actually implemented some quite similar logic here but I like the way you did it here better, so I'll just use this interface instead once this one is merged.

Comment on lines +158 to +161
#[cfg(test)]
{
validation.validate_exp = false;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, could it be possible to add make the test token have an expiry time way off in the future (like greater than the year 2100) and get rid of disabling this conditionally?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, can we use https://www.jwt.io/ to generate signed tokens that with long expired date for testing purpose ?

Comment on lines +408 to +411

#[test]
fn test_unverified_token() {
let claims = Claims::with_unverified(TEST_TOKEN).expect("Failed to parse token");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, it would be good to have a 2 other tests:

  • Try parse a token that was valid in the past but no longer is valid, and make sure it fails
  • Try to parse a token which will become valid in the future and make sure it fails

@pblazej
Copy link
Author

pblazej commented Oct 21, 2025

I actually implemented some quite similar logic

Yeah, I realized after a while it must be duplicated 😃

  • Exposing Claims

Yes, I struggled a bit with custom types there, but may be worth doing

  • What I don't like about this impl?

Is that exp/nbf is baked into the initialization, so you cannot inspect an expired token from the cache, not sure if that's minor or not.

I'll update this PR in spare time 👍 If the token source goes merged first, feel free to close it/use it somehow.

Copy link
Contributor

@ladvoc ladvoc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ✅

}

impl Claims {
pub fn with_unverified(token: &str) -> Result<Self, AccessTokenError> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: I think from_unverified is a more descriptive name (it decodes Claims from an unverified token)

/// WARNING: Do not use this for authentication - the signature is not verified!
///
#[uniffi::export]
pub fn unverified_token(token: &str) -> Result<Claims, AccessTokenError> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Not specific to this PR, but in Swift, symbols will be imported in the global namespace, right? If so, we might consider using a standardized naming scheme for exported symbols from each module. For example, in this module we would have token_claims_from_unverified (this method), token_verify, and token_generate—everything is prefixed with "token" so it is clear which module it comes from.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep I wanted to ask that in Notion actually, it's not possible to create something like Claims.myNewMethod at all?

Copy link

@1egoman 1egoman Oct 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ I've only played around with the python bindings, but at least with those I was able to export a class as an "object" with static methods (I think I had to mark them technically as "alternate constructors") and that seemed to work. But maybe the swift bindings are different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants