-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
src/main/java/com/labkit/vidhya/web/rest/UserJWTController.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,73 @@ | ||
package com.labkit.vidhya.web.rest; | ||
|
||
import com.labkit.vidhya.security.jwt.JWTConfigurer; | ||
import com.labkit.vidhya.security.jwt.TokenProvider; | ||
import com.labkit.vidhya.web.rest.vm.LoginVM; | ||
|
||
import com.codahale.metrics.annotation.Timed; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.validation.Valid; | ||
|
||
/** | ||
* Controller to authenticate users. | ||
*/ | ||
@RestController | ||
@RequestMapping("/api") | ||
public class UserJWTController { | ||
|
||
private final TokenProvider tokenProvider; | ||
|
||
private final AuthenticationManager authenticationManager; | ||
|
||
public UserJWTController(TokenProvider tokenProvider, AuthenticationManager authenticationManager) { | ||
this.tokenProvider = tokenProvider; | ||
this.authenticationManager = authenticationManager; | ||
} | ||
|
||
@PostMapping("/authenticate") | ||
@Timed | ||
public ResponseEntity<JWTToken> authorize(@Valid @RequestBody LoginVM loginVM) { | ||
|
||
UsernamePasswordAuthenticationToken authenticationToken = | ||
new UsernamePasswordAuthenticationToken(loginVM.getUsername(), loginVM.getPassword()); | ||
|
||
Authentication authentication = this.authenticationManager.authenticate(authenticationToken); | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
boolean rememberMe = (loginVM.isRememberMe() == null) ? false : loginVM.isRememberMe(); | ||
String jwt = tokenProvider.createToken(authentication, rememberMe); | ||
HttpHeaders httpHeaders = new HttpHeaders(); | ||
httpHeaders.add(JWTConfigurer.AUTHORIZATION_HEADER, "Bearer " + jwt); | ||
return new ResponseEntity<>(new JWTToken(jwt), httpHeaders, HttpStatus.OK); | ||
} | ||
|
||
/** | ||
* Object to return as body in JWT Authentication. | ||
*/ | ||
static class JWTToken { | ||
|
||
private String idToken; | ||
|
||
JWTToken(String idToken) { | ||
this.idToken = idToken; | ||
} | ||
|
||
@JsonProperty("id_token") | ||
String getIdToken() { | ||
return idToken; | ||
} | ||
|
||
void setIdToken(String idToken) { | ||
this.idToken = idToken; | ||
} | ||
} | ||
} |
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,52 @@ | ||
package com.labkit.vidhya.web.rest.vm; | ||
|
||
import javax.validation.constraints.NotNull; | ||
import javax.validation.constraints.Size; | ||
|
||
/** | ||
* View Model object for storing a user's credentials. | ||
*/ | ||
public class LoginVM { | ||
|
||
@NotNull | ||
@Size(min = 1, max = 50) | ||
private String username; | ||
|
||
@NotNull | ||
@Size(min = ManagedUserVM.PASSWORD_MIN_LENGTH, max = ManagedUserVM.PASSWORD_MAX_LENGTH) | ||
private String password; | ||
|
||
private Boolean rememberMe; | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
|
||
public Boolean isRememberMe() { | ||
return rememberMe; | ||
} | ||
|
||
public void setRememberMe(Boolean rememberMe) { | ||
this.rememberMe = rememberMe; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "LoginVM{" + | ||
"username='" + username + '\'' + | ||
", rememberMe=" + rememberMe + | ||
'}'; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/labkit/vidhya/web/rest/vm/ManagedUserVM.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 com.labkit.vidhya.web.rest.vm; | ||
|
||
|
||
import javax.validation.constraints.Size; | ||
|
||
/** | ||
* View Model extending the UserDTO, which is meant to be used in the user management UI. | ||
*/ | ||
public class ManagedUserVM { | ||
|
||
public static final int PASSWORD_MIN_LENGTH = 4; | ||
|
||
public static final int PASSWORD_MAX_LENGTH = 100; | ||
|
||
@Size(min = PASSWORD_MIN_LENGTH, max = PASSWORD_MAX_LENGTH) | ||
private String password; | ||
|
||
public ManagedUserVM() { | ||
// Empty constructor needed for Jackson. | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ManagedUserVM{" + | ||
"} " + super.toString(); | ||
} | ||
} |