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

Update token level #942

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

byungjunn
Copy link
Contributor

motivation:

modification:

  • Change admin from true to false or vice versa.

result:

image image

* <p>Updates a permission of a token of the specified ID.
*/
@Patch("/tokens/{appId}/level")
@Consumes("application/json-patch+json")
Copy link
Contributor

Choose a reason for hiding this comment

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

json-patch+json seems not used for this API

Copy link
Contributor Author

@byungjunn byungjunn Apr 18, 2024

Choose a reason for hiding this comment

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

Okay, I'll remove the annotation.

Author author, User loginUser) {
return getTokenOrRespondForbidden(ctx, appId, loginUser).thenCompose(
token -> {
if (token.isAdmin()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we make this API idempotent instead of changing the behavior depending on the token's status?
Some people may want to retry an update request if the previous request is timed out somehow.

How about taking the desired level in the body?

PATCH /tokens/{appId}/permission 

{ "level": "USER" }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ikhoon
Oh, i got it.
In this project, in which package should the request body class be placed?

public class TokenLevel {
    private String level;

    public TokenLevel() {
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

We may place this class in the same package of TokenService.

  • Could remove public
  • Rename TokenLevel to TokenLevelRequest for clarity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, Thanks for your comment. I'll add it.

token -> {
if (token.isAdmin()) {
return mds.updateTokenToUser(author, appId).thenApply(
unused -> mds.findTokenByAppId(appId).join().withoutSecret()
Copy link
Contributor

Choose a reason for hiding this comment

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

join() should not be called for asynchronous code. thenCompose() could be used instead.

Copy link
Contributor Author

@byungjunn byungjunn Apr 18, 2024

Choose a reason for hiding this comment

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

I'll remove the join() .

@minwoox
Copy link
Member

minwoox commented May 10, 2024

Could you add an e2e test to verify that the request is converted correctly?

Author author, User loginUser) {

return getTokenOrRespondForbidden(ctx, appId, loginUser).thenCompose(
token -> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we return 304 Not Modified if the token is at the desired level already?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I'll add

 throw HttpStatusException.of(HttpStatus.NOT_MODIFIED);

when the current status and desired one is same.

@byungjunn byungjunn requested a review from ikhoon May 19, 2024 08:56
Copy link
Member

@minwoox minwoox left a comment

Choose a reason for hiding this comment

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

Looks great so far. 😉

@@ -203,4 +244,52 @@ public void updateToken() {
.isInstanceOf(CompletionException.class)
.hasCauseInstanceOf(IllegalArgumentException.class);
}

@Test
public void updateTokenLevel() {
Copy link
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
public void updateTokenLevel() {
void updateTokenLevel() {

}

@Test
public void createTokenAndUpdateLevel() throws JsonParseException {
Copy link
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
public void createTokenAndUpdateLevel() throws JsonParseException {
void createTokenAndUpdateLevel() throws JsonParseException {

Comment on lines 19 to 36
class TokenLevelRequest {
private String level;

TokenLevelRequest() {
}

TokenLevelRequest(String level) {
this.level = level;
}

String getLevel() {
return level;
}

void setLevel(String level) {
this.level = level;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Let's use Jackson annotations to make it clear. 😉

Suggested change
class TokenLevelRequest {
private String level;
TokenLevelRequest() {
}
TokenLevelRequest(String level) {
this.level = level;
}
String getLevel() {
return level;
}
void setLevel(String level) {
this.level = level;
}
}
class TokenLevelRequest {
private final String level;
@JsonCreator
TokenLevelRequest(@JsonProperty("level") String level) {
this.level = level;
}
@JsonProperty
String level() {
return level;
}
}

@@ -192,6 +193,41 @@ public CompletableFuture<Token> updateToken(ServiceRequestContext ctx,
);
}

/**
* PATCH /tokens/{appId}/permission
Copy link
Member

Choose a reason for hiding this comment

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

Should we use permission or level?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will change "permission" to "level" in the comment.

return mds.updateTokenToAdmin(author, appId).thenCompose(
unused -> mds.findTokenByAppId(appId).thenApply(Token::withoutSecret));
default:
throw new IllegalArgumentException("Unexpected token level: " + tokenLevelRequest);
Copy link
Member

Choose a reason for hiding this comment

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

I think we can do this before calling getTokenOrRespondForbidden

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before calling getTokenOrRespondForbidden, i will check the argument.

checkArgument(Arrays.asList("user", "admin").contains(tokenLevelRequest.level().toLowerCase()),
                      "Unsupported token level: " + tokenLevelRequest.level());


return getTokenOrRespondForbidden(ctx, appId, loginUser).thenCompose(
token -> {
switch (tokenLevelRequest.getLevel()) {
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about supporting case-insensitive requests?

Suggested change
switch (tokenLevelRequest.getLevel()) {
switch (tokenLevelRequest.getLevel().toLowerCase()) {

/**
* Update the {@link Token} of the specified {@code appId} to admin.
*/
public CompletableFuture<Revision> updateTokenToAdmin(Author author, String appId) {
Copy link
Member

Choose a reason for hiding this comment

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

I think we can merge these two methods with a boolean parameter. Could you merge them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, it could be more simple.

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

Successfully merging this pull request may close these issues.

Provide a way to update the permission of a token
3 participants