Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preferred way to check if a Signed GlobalID is expired? #141

Open
aergonaut opened this issue Apr 8, 2022 · 4 comments
Open

Preferred way to check if a Signed GlobalID is expired? #141

aergonaut opened this issue Apr 8, 2022 · 4 comments

Comments

@aergonaut
Copy link

The documentation for Signed Global IDs shows that they can have an expiry date. I know that if the SGID is expired, then trying to use it to locate a record returns nil. But returning nil could also mean that the record indicated by the SGID no longer exists.

Is there a way to specifically tell that the SGID is expired? I see in the code, an ExpiredMessage exception is actually raised, but it is immediately caught and turned into nil.

def verify(sgid, options)
metadata = pick_verifier(options).verify(sgid)
raise_if_expired(metadata['expires_at'])
metadata['gid'] if pick_purpose(options) == metadata['purpose']
rescue ActiveSupport::MessageVerifier::InvalidSignature, ExpiredMessage
nil
end

I could use the SignedGlobalID#verifier to decode the message and then get the expired_at attribute out of the Hash. That seems hacky to me, so I was wondering if there was another way to check for expiration?

Would a PR to add an expired? method to SignedGlobalID be welcome? I'd be happy to work on that.

Thanks!

@fschwahn
Copy link

I'd be interested as well - we want to know if & when a globalid expired to show users an info like "this link expired on XXX". Currently we do this manually using SignedGlobalID.verifier. It would be great if an expires_at method could be added in addition to expired?.

@dhh
Copy link
Member

dhh commented Dec 16, 2022

I'd take #expired? 👍

@rafaelfranca
Copy link
Member

If we want to implement this it need to be in the ActiveSupport::MessageVerifier that is used to globalid to check message

https://github.com/rails/rails/blob/main/activesupport/lib/active_support/message_verifier.rb

The current code here is going to be removed to verify the message is legacy and will be removed. See #107

@ghiculescu
Copy link
Member

PR to add to Rails: rails/rails#48820

Once that's merged I can make a PR here to add expired?.

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

No branches or pull requests

5 participants