-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#849 | Introduce generateToken user external api
- Loading branch information
Showing
3 changed files
with
38 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
avni-server-api/src/main/java/org/avni/server/web/api/UserLoginApiController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.avni.server.web.api; | ||
|
||
import jakarta.persistence.EntityNotFoundException; | ||
import org.avni.server.dao.UserRepository; | ||
import org.avni.server.domain.User; | ||
import org.avni.server.domain.accessControl.AvniAccessException; | ||
import org.avni.server.framework.security.AuthService; | ||
import org.avni.server.web.request.auth.GenerateTokenRequest; | ||
import org.avni.server.web.request.auth.GenerateTokenResult; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
|
||
@Controller | ||
public class UserLoginApiController { | ||
|
||
private final AuthService authService; | ||
private final UserRepository userRepository; | ||
|
||
public UserLoginApiController(AuthService authService, UserRepository userRepository) { | ||
this.authService = authService; | ||
this.userRepository = userRepository; | ||
} | ||
|
||
@RequestMapping(value = "/api/user/generateToken", method = RequestMethod.POST) | ||
public ResponseEntity<GenerateTokenResult> generateTokenForUser(@RequestBody GenerateTokenRequest request) throws EntityNotFoundException { | ||
User user = userRepository.findByUsername(request.getUsername()); | ||
if (!user.getUserSettings().isAllowedToInvokeTokenGenerationAPI()) { | ||
throw AvniAccessException.createForUserNotAllowedTokenGeneration(user); | ||
} | ||
return ResponseEntity.ok(new GenerateTokenResult(authService.generateTokenForUser(request.getUsername(), request.getPassword()))); | ||
} | ||
} |