Skip to content

Commit

Permalink
added authendicate
Browse files Browse the repository at this point in the history
  • Loading branch information
vidhya03 committed Feb 16, 2018
1 parent 8400804 commit 7836a07
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/main/java/com/labkit/vidhya/web/rest/UserJWTController.java
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;
}
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/labkit/vidhya/web/rest/vm/LoginVM.java
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 src/main/java/com/labkit/vidhya/web/rest/vm/ManagedUserVM.java
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();
}
}

0 comments on commit 7836a07

Please sign in to comment.