-
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.
Merge pull request #20 from naeemark/develop
Develop to Master
- Loading branch information
Showing
25 changed files
with
636 additions
and
79 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
1 change: 1 addition & 0 deletions
1
src/main/java/com/naeemark/jas/api/health/HealthController.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
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
4 changes: 2 additions & 2 deletions
4
src/main/java/com/naeemark/jas/api/signup/SignupController.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
43 changes: 43 additions & 0 deletions
43
src/main/java/com/naeemark/jas/api/user/UserController.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,43 @@ | ||
package com.naeemark.jas.api.user; | ||
|
||
import com.naeemark.jas.models.response.UserResponse; | ||
import com.naeemark.jas.utils.JwtTokenUtils; | ||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import io.swagger.annotations.ApiResponse; | ||
import io.swagger.annotations.ApiResponses; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* Created by Naeem <naeemark@gmail.com>. | ||
* <p> | ||
* Created on: 2020-08-21 | ||
*/ | ||
@Api(tags = "3 - User", description = "Gets User Data by the header Token") | ||
@RestController | ||
@RequestMapping("/api") | ||
public class UserController { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(UserController.class); | ||
|
||
@ApiOperation(value = "Get User", notes = "Gets User Object", response = UserResponse.class) | ||
@ApiResponses(value = { | ||
@ApiResponse(code = 304, message = "Operation was not successful"), | ||
@ApiResponse(code = 400, message = "Validation Error"), | ||
@ApiResponse(code = 404, message = "Not Found"), | ||
@ApiResponse(code = 417, message = "Expectations failed"), | ||
@ApiResponse(code = 422, message = "Request not processable") | ||
}) | ||
@GetMapping(value = "/user") | ||
public UserResponse getUser(@RequestHeader("Authorization") String authorization) { | ||
|
||
UserResponse userResponse = JwtTokenUtils.validateAuthorization(authorization); | ||
logger.info(userResponse.toString()); | ||
return userResponse; | ||
} | ||
} |
12 changes: 0 additions & 12 deletions
12
src/main/java/com/naeemark/jas/exceptions/AuthException.java
This file was deleted.
Oops, something went wrong.
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
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
3 changes: 2 additions & 1 deletion
3
...com/naeemark/jas/models/AuthResponse.java → ...ark/jas/models/response/AuthResponse.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
2 changes: 1 addition & 1 deletion
2
...eemark/jas/api/health/HealthResponse.java → ...k/jas/models/response/HealthResponse.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
23 changes: 23 additions & 0 deletions
23
src/main/java/com/naeemark/jas/models/response/UserResponse.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,23 @@ | ||
package com.naeemark.jas.models.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
|
||
/** | ||
* Created by Naeem <naeemark@gmail.com>. | ||
* <p> | ||
* Created on: 2020-08-20 | ||
*/ | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@ToString | ||
public class UserResponse { | ||
|
||
private Integer id; | ||
private String name; | ||
private String userName; | ||
private String email; | ||
} |
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
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
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,71 @@ | ||
package com.naeemark.jas.utils; | ||
|
||
import com.naeemark.jas.models.User; | ||
import com.naeemark.jas.models.response.UserResponse; | ||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import java.util.Date; | ||
|
||
import static com.naeemark.jas.utils.Constants.*; | ||
|
||
/** | ||
* Created by Naeem <naeemark@gmail.com>. | ||
* <p> | ||
* Created on: 2020-08-21 | ||
*/ | ||
public class JwtTokenUtils { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(JwtTokenUtils.class); | ||
|
||
/** | ||
* Generates an Access Token based on provided payload | ||
* | ||
* @param user User | ||
* @return String | ||
*/ | ||
public static String generateToken(User user) { | ||
|
||
long timestamp = System.currentTimeMillis(); | ||
String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, JWT_SECRET_KEY) | ||
.setIssuedAt(new Date(timestamp)) | ||
.setExpiration(new Date(timestamp + JWT_ACCESS_TOKEN_EXPIRY_MILLIS)) | ||
.claim("id", user.getId()) | ||
.claim("name", user.getName()) | ||
.claim("email", user.getEmail()) | ||
.claim("userName", user.getUserName()) | ||
.compact(); | ||
|
||
logger.info(token); | ||
return token; | ||
} | ||
|
||
/** | ||
* Validates and returns claim values | ||
* | ||
* @param authorization | ||
* @return | ||
*/ | ||
public static UserResponse validateAuthorization(String authorization) { | ||
if (authorization == null || authorization.isEmpty()) { | ||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, ERROR_AUTHORIZATION_REQUIRED); | ||
} | ||
try { | ||
Claims claims = Jwts.parser().setSigningKey(JWT_SECRET_KEY) | ||
.parseClaimsJws(authorization).getBody(); | ||
return new UserResponse( | ||
Integer.parseInt(claims.get("id").toString()), | ||
claims.get("name").toString(), | ||
claims.get("userName").toString(), | ||
claims.get("email").toString() | ||
); | ||
} catch (Exception e) { | ||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, ERROR_INVALID_AUTHORIZATION); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.