Skip to content

Commit 93a4efb

Browse files
committed
Add endpoint to update token alias
1 parent d3747bf commit 93a4efb

8 files changed

Lines changed: 62 additions & 9 deletions

File tree

src/main/java/net/explorviz/token/model/LandscapeToken.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ public String getAlias() {
116116
return this.alias;
117117
}
118118

119+
@BsonProperty("alias")
120+
public void setAlias(final String alias) {
121+
this.alias = alias;
122+
}
123+
119124

120125
/**
121126
* The secret of the token that is required to write spans to it.

src/main/java/net/explorviz/token/resources/TokenResource.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import jakarta.ws.rs.ForbiddenException;
99
import jakarta.ws.rs.GET;
1010
import jakarta.ws.rs.NotFoundException;
11+
import jakarta.ws.rs.PATCH;
1112
import jakarta.ws.rs.POST;
1213
import jakarta.ws.rs.Path;
1314
import jakarta.ws.rs.PathParam;
@@ -106,6 +107,30 @@ public Response deleteToken(@PathParam("tid") final String tokenVal) {
106107

107108
}
108109

110+
/**
111+
* Endpoint to update the alias of a token.
112+
*
113+
* @param tokenVal Value of the token which shall be updated.
114+
* @param tokenUpdates Token object containing the new alias.
115+
* @return Response with no content.
116+
*/
117+
@PATCH
118+
@Authenticated
119+
@Produces(MediaType.APPLICATION_JSON)
120+
public Response updateTokenAlias(@PathParam("tid") final String tokenVal,
121+
final LandscapeToken tokenUpdates) {
122+
123+
final LandscapeToken token =
124+
this.tokenService.getByValue(tokenVal).orElseThrow(NotFoundException::new);
125+
final String uid = this.securityIdentity.getPrincipal().getName();
126+
if (this.tokenAccessService.canUpdate(token, uid)) {
127+
this.tokenService.updateAlias(token, tokenUpdates.getAlias());
128+
return Response.noContent().build();
129+
} else {
130+
throw new ForbiddenException();
131+
}
132+
}
133+
109134
/**
110135
* Endpoint to modify an access token, i.e. grant, remove, or clone access to it.
111136
*

src/main/java/net/explorviz/token/service/TokenAccessService.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,14 @@ default boolean canDelete(final LandscapeToken token, final String userId) {
3232
return Arrays.asList(this.getPermissions(token, userId)).contains(TokenPermission.DELETE);
3333
}
3434

35-
35+
/**
36+
* Checks whether a user can update a landscape token.
37+
*
38+
* @param token the token
39+
* @param userId the id of the user
40+
* @return {@code true} iff the user is allowed to update the token
41+
*/
42+
default boolean canUpdate(final LandscapeToken token, final String userId) {
43+
return Arrays.asList(this.getPermissions(token, userId)).contains(TokenPermission.UPDATE);
44+
}
3645
}

src/main/java/net/explorviz/token/service/TokenAccessServiceImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ public class TokenAccessServiceImpl implements TokenAccessService {
2121
public TokenPermission[] getPermissions(final LandscapeToken token, final String userId) {
2222

2323
if (!this.authEnabled.get()) {
24-
return new TokenPermission[] {TokenPermission.DELETE, TokenPermission.READ};
24+
return new TokenPermission[] {TokenPermission.DELETE, TokenPermission.READ,
25+
TokenPermission.UPDATE};
2526
}
2627

2728
if (token.getOwnerId().equals(userId)) {
28-
return new TokenPermission[] {TokenPermission.READ, TokenPermission.DELETE};
29+
return new TokenPermission[] {TokenPermission.READ, TokenPermission.DELETE,
30+
TokenPermission.UPDATE};
2931
}
3032

3133
return new TokenPermission[] {};

src/main/java/net/explorviz/token/service/TokenPermission.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
* Permissions of a user regarding a token.
55
*/
66
public enum TokenPermission {
7-
READ, DELETE
7+
READ, DELETE, UPDATE
88
}

src/main/java/net/explorviz/token/service/TokenService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,12 @@ default LandscapeToken createNewToken(final String ownerId) {
9090
* @param alias the alias of the cloned token
9191
*/
9292
LandscapeToken cloneToken(String token, String ownerId, String alias);
93+
94+
/**
95+
* Update the alias of a token.
96+
*
97+
* @param token the token to update
98+
* @param newAlias the new alias
99+
*/
100+
void updateAlias(LandscapeToken token, String newAlias);
93101
}

src/main/java/net/explorviz/token/service/TokenServiceImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,10 @@ public void revokeAccess(final LandscapeToken token, final String userId) {
153153
this.repository.update("{ $pull: { sharedUsers: ?1 } } }, $set: { ownerId: '$ownerId'}", userId)
154154
.where(DELETE_FLAG_QUERY, token.getValue());
155155
}
156+
157+
@Override
158+
public void updateAlias(final LandscapeToken token, final String newAlias) {
159+
this.repository.update("{ $set: { alias: ?1 } }", newAlias)
160+
.where(DELETE_FLAG_QUERY, token.getValue());
161+
}
156162
}

src/main/resources/application.properties

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@ quarkus.devservices.enabled=false
55
##########
66
## HTTP ##
77
##########
8-
%dev.quarkus.http.port=8080
8+
quarkus.http.port=8080
9+
%dev.quarkus.http.port=8084
910
%dev.quarkus.http.host=0.0.0.0
1011
quarkus.http.cors=true
1112
%dev.quarkus.http.cors.origins=/.*/
1213
quarkus.http.cors.origins=http://localhost:4200,http://localhost:8080,https://samoa.se.informatik.uni-kiel.de,https://explorviz.sustainkieker.kieker-monitoring.net
13-
quarkus.http.cors.methods=GET,PUT,POST,DELETE
14+
quarkus.http.cors.methods=GET,PUT,PATCH,POST,DELETE
1415
quarkus.http.cors.access-control-max-age=24H
1516
quarkus.http.cors.access-control-allow-credentials=true
1617
quarkus.http.test-port=8194
1718

18-
## added for frontend erro 403 ##
19-
%dev.quarkus.http.cors.origins=/.*/
2019
quarkus.tls.trust-all=true
2120
#quarkus.http.cors.origins=http://localhost:4200
22-
quarkus.http.cors.methods=GET,PUT,POST, DELETE
2321
#quarkus.http.cors.headers=Access-Control-Allow-Origin
2422

2523
####################

0 commit comments

Comments
 (0)