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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

JWT Improvement Part 2 #721

Closed
wants to merge 1 commit into from
Closed

JWT Improvement Part 2 #721

wants to merge 1 commit into from

Conversation

hardikmashru
Copy link
Collaborator

@hardikmashru hardikmashru commented Apr 5, 2024

  1. Call onTokenRegistrationFailed in more circumstances and rename it
  2. Provide an AuthFailure object to onTokenRegistrationFailed

馃敼 Jira Ticket(s) if any

鉁忥笍 Description

Please provide a brief description of what this pull request does.

1. Call onTokenRegistrationFailed in more circumstances and rename it
2. Provide an AuthFailure object to onTokenRegistrationFailed
Copy link
Member

@Ayyanchira Ayyanchira left a comment

Choose a reason for hiding this comment

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

Looking great!
But lets have it merged in feature branch.
Saw duplicate PR which goes into feature branch. Was that intentional?

public final String userKey;

/** the authToken which casued the failure */
public final String failedAuthToken;
Copy link
Member

Choose a reason for hiding this comment

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

This could be null at times

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes we pass null when creating object in case of null.

this.failedRequestTime = failedRequestTime;
this.failureReason = failureReason;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
}
}
enum class AuthFailureReason {
INVALID_USER,
INVALID_AUTH_TOKEN,
AUTH_TOKEN_EXPIRED,
UNKNOWN
}

Copy link
Member

Choose a reason for hiding this comment

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

Some other enum to consider can be:
//ERROR_CODE_JWT_USER_IDENTIFIERS_MISMATCHED
//"JWT payload requires a value for userId or email" - JWT_PAYLOAD_MISSING_VALID_USER_KEY
//"Email could not be found" - JWT_PAYLOAD_MISSING_VALID_USER_KEY
//ERROR_CODE_INVALID_JWT_PAYLOAD
//"JWT token is expired" - AUTH_TOKEN_EXPIRED
//"exp must be less than 1 year from iat" - AUTH_TOKEN_EXPIRATION_INVALID
//"JWT is invalid" - JWT_SIGNATURE_INVALID
//"JWT format is invalid" - JWT_FORMAT_INVALID
//"JWT token has been invalidated" - JWT_TOKEN_INVALIDATED
//"Invalid payload" - JWT_PAYLOAD_INVALID
//null - JWT_NULL

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes enum sounds good. Thanks for the suggestion.

I hope, all the text error messages for JWT are covered here, because in case of unrelated message we won't be able to return the error message as we are using enum now instead of string.

public final long failedRequestTime;

/** indicates a reason for failure */
public final String failureReason;
Copy link
Member

Choose a reason for hiding this comment

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

It can be an enum as well

@@ -101,7 +101,7 @@ private void handleAuthTokenSuccess(String authToken, IterableHelper.SuccessHand

private void handleAuthTokenFailure(Throwable throwable) {
IterableLogger.e(TAG, "Error while requesting Auth Token", throwable);
authHandler.onTokenRegistrationFailed(throwable);
handleAuthFailure(null, IterableConstants.AUTH_TOKEN_GENERATION_ERROR);
Copy link
Member

Choose a reason for hiding this comment

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

Use of constant highly appreciated. However suggesting above to have set of enum for varied JWT failures. What do you think about it?

@@ -118,7 +118,7 @@ public void queueExpirationRefresh(String encodedJWT) {
}
} catch (Exception e) {
IterableLogger.e(TAG, "Error while parsing JWT for the expiration", e);
authHandler.onTokenRegistrationFailed(new Throwable("Auth token decode failure. Scheduling auth token refresh in 10 seconds..."));
handleAuthFailure(encodedJWT, IterableConstants.AUTH_TOKEN_PAYLOAD_INVALID);
Copy link
Member

Choose a reason for hiding this comment

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

Same as above. Having enum will help developers type cast the reasons into specific enums and take actions easily

@@ -135,6 +135,22 @@ void reSyncAuth() {
}
}

void handleAuthFailure(String authToken, String failureReason) {
authHandler.onAuthFailure(new AuthFailure(getEmailOrUserId(), authToken, IterableUtil.currentTimeMillis(), failureReason));
Copy link
Member

Choose a reason for hiding this comment

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

Great use of currentTime function to tell exactly when the failure occurred.

Comment on lines +260 to +271
public static final String AUTH_TOKEN_NULL = "AUTH_TOKEN_NULL";
public static final String AUTH_TOKEN_GENERATION_ERROR = "AUTH_TOKEN_GENERATION_ERROR";

public static final String AUTH_TOKEN_GENERIC_ERROR = "AUTH_TOKEN_GENERIC_ERROR";
public static final String AUTH_TOKEN_EXPIRED = "AUTH_TOKEN_EXPIRED";
public static final String AUTH_TOKEN_EXPIRATION_INVALID = "AUTH_TOKEN_EXPIRATION_INVALID";
public static final String AUTH_TOKEN_SIGNATURE_INVALID = "AUTH_TOKEN_SIGNATURE_INVALID";
public static final String AUTH_TOKEN_FORMAT_INVALID = "AUTH_TOKEN_FORMAT_INVALID";
public static final String AUTH_TOKEN_INVALIDATED = "AUTH_TOKEN_INVALIDATED";
public static final String AUTH_TOKEN_PAYLOAD_INVALID = "AUTH_TOKEN_PAYLOAD_INVALID";
public static final String AUTH_TOKEN_USER_KEY_INVALID = "AUTH_TOKEN_USER_KEY_INVALID";

Copy link
Member

Choose a reason for hiding this comment

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

Suggested above to have it as Enum for failure reason. Open to suggestions too

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.

None yet

2 participants