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

Set tokenTimeToLive in seconds from now #18

Closed
wants to merge 2 commits into from

Conversation

arnfred
Copy link

@arnfred arnfred commented May 28, 2024

getTokenTimeToLive uses a default value 15 days from current date in milliseconds:

System.currentTimeMillis() + 3600 * 24 * 15 * 1000

However, the value is then used to configure the Max-Age cookie property: https://github.com/kinde-oss/kinde-java-sdk/blob/main/src/main/java/org/openapitools/sdk/storage/Storage.java#L65

According to MDN web docs, the max-age of a cookie "Indicates the number of seconds until the cookie expires", so it should be specified in seconds from now.

For this reason, I'm changing the default value used here to reflect 15 days in seconds

Explain your changes

Suppose there is a related issue with enough detail for a reviewer to understand your changes fully. In that case, you can omit an explanation and instead include either “Fixes #XX” or “Updates #XX” where “XX” is the issue number.

Checklist

🛟 If you need help, consider asking for advice over in the Kinde community.

`getTokenTimeToLive` uses a default value 15 days from current date in milliseconds:

```
System.currentTimeMillis() + 3600 * 24 * 15 * 1000
```

However, the value is then used to configure the [Max-Age cookie property](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#max-agenumber]): https://github.com/kinde-oss/kinde-java-sdk/blob/main/src/main/java/org/openapitools/sdk/storage/Storage.java#L65

According to MDN web docs, the max-age of a cookie "_Indicates the number of seconds until the cookie expires_", so it should be specified in seconds from now.

For this reason, I'm changing the default value used here to reflect 15 days in seconds
Copy link
Contributor

coderabbitai bot commented May 28, 2024

Walkthrough

The Storage.java file has been updated to simplify the management of token expiration. The getTokenTimeToLive() method now returns a constant value representing the time-to-live (TTL) in seconds, enhancing code clarity. Additionally, the setState() and setCodeVerifier() methods have been modified to use fixed expiration durations, eliminating unnecessary calculations and improving maintainability.

Changes

Files/Paths Change Summary
src/main/java/org/openapitools/sdk/storage/Storage.java Updated getTokenTimeToLive(), setState(), and setCodeVerifier() methods to use fixed TTL values instead of dynamic time calculations.

Sequence Diagram(s)

The changes are straightforward and do not introduce new features or significantly alter the control flow, hence sequence diagrams are not necessary.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
src/main/java/org/openapitools/sdk/storage/Storage.java (1)

90-90: The change correctly sets the token TTL to 15 days in seconds, aligning with the PR objectives.

Consider adding a comment explaining why the value is in seconds, to aid future maintainability.

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between bebb88b and f87edcb.
Files selected for processing (1)
  • src/main/java/org/openapitools/sdk/storage/Storage.java (2 hunks)

@arnfred
Copy link
Author

arnfred commented Jul 2, 2024

This has been open now for more than a month. Would someone be able to have a look?

@muzzybee
Copy link

muzzybee commented Aug 1, 2024

Hi - you missed two other methods in that same class that need a similar fix.

setState
setCodeVerifier

Regards,
Muz

See previous commit for more detail
@arnfred
Copy link
Author

arnfred commented Aug 1, 2024

Fixed

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f87edcb and b279a35.

Files selected for processing (1)
  • src/main/java/org/openapitools/sdk/storage/Storage.java (4 hunks)
Additional comments not posted (3)
src/main/java/org/openapitools/sdk/storage/Storage.java (3)

90-90: LGTM!

The method correctly returns a fixed value representing 15 days in seconds.


102-102: LGTM!

The method correctly sets the expiration time for the state to 2 hours in seconds.


111-111: LGTM!

The method correctly sets the expiration time for the code verifier to 2 hours in seconds.

@@ -87,7 +87,7 @@ public static Long getExpiredAt(HttpServletRequest request) {
}

public static Long getTokenTimeToLive() {
return tokenTimeToLive != null ? tokenTimeToLive : System.currentTimeMillis() + 3600 * 24 * 15 * 1000; // Live in 15 days
return tokenTimeToLive != null ? tokenTimeToLive : 3600 * 24 * 15; // Live in 15 days
Copy link

Choose a reason for hiding this comment

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

Can define as a static final int FIFTEEN_DAYS = 3600 * 24 * 15 and use in getTokenTImeToLeave. And the function can also simplify:

public static Long getTokenTimeToLive() {
        return tokenTimeToLive == null ? FIFTEEN_DAYS : tokenTimeToLive;
}

Copy link

@7171mayanktyagi 7171mayanktyagi Aug 1, 2024

Choose a reason for hiding this comment

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

I believe correct implementation would be:

public static Long getTokenTimeToLive() {
return tokenTimeToLive != null ? tokenTimeToLive : 3600 * 24 * 15 * 1000; // Live in 15 days
}

Copy link

@hhovhann hhovhann Aug 1, 2024

Choose a reason for hiding this comment

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

@7171mayanktyagi that was removed in f87edcb My comments was related to current PR, if calculation will be changed, could properly change the above my comments as well, where I already specify the constants. Which was my only notes + simplify nullify check, and not negate null checking.

Choose a reason for hiding this comment

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

As, value of getTokenTimeToLive() is being passed in setMaxAge() of Cookie. And it accept value in ms.

Choose a reason for hiding this comment

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

@hhovhann I just verified in java documentation. @arnfred implementation is absolutely correct. Cookie 's maxage set in seconds.

Choose a reason for hiding this comment

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

@hhovhann, I also came across this #PR on Upwork. The question is, who is the author of this post?

Copy link

Choose a reason for hiding this comment

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

#18 (comment) - @7171mayanktyagi my comment wasn't realated to cookies maxage, my comments was suggestion to use constants define in class level the value of the constant what would be used for the calculatation.
Based on Java Documentation suggesting to use the Constants in code instead of numbers, which i suggested, as well as the better practise. Upto you will you use it or no, this is just the suggestion and again I am not the code owner to restrict your commit. I hope I am clear in my explanations

#18 (comment) - @7171mayanktyagi the upwork job post not visible the company name, because that is job post which you can apply and if the Job poster will respond you then you can see the company/person. I just see on comments the people when adding the feedback the name Andre, not sure if Andre is part of the Kinde.

Hope my clarifications is enough for you.

@@ -99,7 +99,7 @@ public static String getState(HttpServletRequest request) {
}

public static void setState( HttpServletResponse response,String newState) {
setItem(response,StorageEnums.STATE.getValue(), newState,(int) ((long) (System.currentTimeMillis() + 3600 *2 )));
setItem(response,StorageEnums.STATE.getValue(), newState, 3600 * 2); // 2 hours
Copy link

Choose a reason for hiding this comment

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

Same is here
int static final TWO_DAYS = 3600 * 2;

    public static void setState( HttpServletResponse response,String newState) {
        setItem(response,StorageEnums.STATE.getValue(), newState, TWO_HOURS); // 2 hours
        // set expiration time for state
    }

@@ -108,7 +108,7 @@ public static String getCodeVerifier(HttpServletRequest request) {
}

public static void setCodeVerifier(HttpServletResponse response,String newCodeVerifier) {
setItem(response,StorageEnums.CODE_VERIFIER.getValue(), newCodeVerifier,(int) ((long) (System.currentTimeMillis() + 3600 *2 )));
setItem(response,StorageEnums.CODE_VERIFIER.getValue(), newCodeVerifier, 3600 * 2); // 2 hours
Copy link

Choose a reason for hiding this comment

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

Same is here
int static final TWO_DAYS = 3600 * 2;

public static void setCodeVerifier(HttpServletResponse response,String newCodeVerifier) {
        setItem(response,StorageEnums.CODE_VERIFIER.getValue(), newCodeVerifier, TWO_DAYS); // 2 hours
        // set expiration time for code verifier
}

@hhovhann
Copy link

hhovhann commented Aug 1, 2024

Added couple of comments please take alook, otherwise LGTM

@arnfred
Copy link
Author

arnfred commented Aug 1, 2024

Listen, there's clearly a bug in your code. I've put up a PR with a fix.

It's been ignored for two months and I have had to escalate to your security team to even had it looked at. And now you're asking me to tidy up your commercial codebase while I'm at it.

I suggest you apply any cosmetic changes you're interested in in your own paid time and merge the PR when you think it's ready.

@hhovhann
Copy link

hhovhann commented Aug 1, 2024

Dear @arnfred if this message #18 (comment) to me, unfortunately I am not the owner of the code and I just added my comments as a free contribution based on the open upwork job post. If that wasn't to me, then you can ignore this comment.

Thanks

@arnfred
Copy link
Author

arnfred commented Aug 1, 2024

My comment was to you. I assumed you were working for Kinde.

To any Kinde employee seeing this PR:

Browser cookies with the wrong expiry date in your official SDK is BAD. You want this fixed. All of your customers who use this SDK will be affected. Their users will not see updated permissions because their tokens aren't updating. They will stay logged in after their user has been revoked and disabled for practically forever.

The current value of System.currentTimeMillis() is roughly 1722514700000. This value interpreted as seconds is equal to 54620 years.

@7171mayanktyagi
Copy link

7171mayanktyagi commented Aug 1, 2024

My comment was to you. I assumed you were working for Kinde.

To any Kinde employee seeing this PR:

Browser cookies with the wrong expiry date in your official SDK is BAD. You want this fixed. All of your customers who use this SDK will be affected. Their users will not see updated permissions because their tokens aren't updating. They will stay logged in after their user has been revoked and disabled for practically forever.

The current value of System.currentTimeMillis() is roughly 1722514700000. This value interpreted as seconds is equal to 54620 years.

Well said! It's Immortal cookie .

@ben-mathu
Copy link

Have you seen this issue #12 ?

@ben-mathu
Copy link

Have you seen this issue #12 ?

I also don't work at kinde, but I just wanted to direct you to a commit suggested in the issue

@wwadge
Copy link

wwadge commented Aug 2, 2024

Why use magic numbers and constants? There's already a built in JDK method for this:
TimeUnit.MILLISECONDS.convert(2, TimeUnit.DAYS);

@brettchaldecott
Copy link
Contributor

This is no longer required with the reworked SDK. Please close

@brettchaldecott
Copy link
Contributor

Since this PR was originally created, the SDK has undergone a significant refactor. The changes made during that process have rendered this PR redundant. Therefore, we are closing it. Thank you for your contribution!

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.

7 participants