diff --git a/src/main/java/fr/univnantes/webandcloud/api/controllers/v1/Tokens.java b/src/main/java/fr/univnantes/webandcloud/api/controllers/v1/Tokens.java new file mode 100644 index 0000000000000000000000000000000000000000..7204ba45e000250c4cc75e1f90116d8a107f5578 --- /dev/null +++ b/src/main/java/fr/univnantes/webandcloud/api/controllers/v1/Tokens.java @@ -0,0 +1,42 @@ +package fr.univnantes.webandcloud.api.controllers.v1; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.lang.NonNull; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import fr.univnantes.webandcloud.api.core.Token; +import fr.univnantes.webandcloud.api.responses.BadRequest; +import fr.univnantes.webandcloud.api.responses.ResponseError; +import fr.univnantes.webandcloud.api.services.DBService; +import fr.univnantes.webandcloud.api.services.TokenService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.responses.ApiResponse; + +@RestController() +public class Tokens extends BaseController { + + @Autowired + TokenService tokenService; + @Autowired + DBService<Token> tokenDB; + + @CrossOrigin + @Operation(summary = "Check validity of token") + @ApiResponse(responseCode = "200", description = "Token Valid") + @RequestMapping(value = "/tokens/check", method = RequestMethod.GET) + public void checkToken(@Parameter(description = "Payload of User") @RequestHeader @NonNull String token) + throws ResponseError { + try { + if (tokenService.getTokenRelatedWithoutLinks(token) == null) + throw new BadRequest("Token Unknown"); + } catch (RuntimeException ignored) { + tokenDB.remove(token); + throw new BadRequest("Token Unknown"); + } + } +}