-
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.
add stateless token authentication. The user must be logged in by '/login' by sending a JSON form with user and pass, and in case the authentication is successful, a token is returned in the Header 'X-AUTH-TOKEN', otherwise an authenticated error is sent. Also, when trying to access a resource which is not '/login' or '/' and no token is provided, a 401 unauthorized error will be returned. - implement login authentication filter - implement authentication filter for logged users - implement handler to return unauthorized message - add codecov config
- Loading branch information
Showing
12 changed files
with
434 additions
and
174 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
codecov: | ||
notify: | ||
require_ci_to_pass: true | ||
comment: | ||
behavior: default | ||
layout: header, diff | ||
require_changes: false | ||
coverage: | ||
precision: 2 | ||
range: | ||
- 70.0 | ||
- 100.0 | ||
round: down | ||
status: | ||
changes: false | ||
patch: false | ||
project: false | ||
parsers: | ||
gcov: | ||
branch_detection: | ||
conditional: true | ||
loop: true | ||
macro: false | ||
method: false | ||
javascript: | ||
enable_partials: false | ||
|
||
|
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
60 changes: 60 additions & 0 deletions
60
webapp/src/main/java/ar/edu/itba/paw/webapp/auth/JWTTokenHandler.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,60 @@ | ||
package ar.edu.itba.paw.webapp.auth; | ||
|
||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import io.jsonwebtoken.SignatureException; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
|
||
public class JWTTokenHandler implements TokenHandler { | ||
private static final String KEY = "B/1+wzD9jv8UwBZke9EZHfAclHDYre9msPR54UfhXmhx8OBQXrQb0URl9mBwGPBK\n" + | ||
"ZExc4gxayeTxEPT3DIwMCZfCdMBVh4yjhzJ46AzummsrpyHeo2TgRtWRW+KCTtwo\n" + | ||
"19kyrxbMpc8dEY9YO2ghNT0ZOcwtvEu13kzngevUsPV9pWsKdRXGgt5HV3rpuvJ7\n" + | ||
"M0rL3BeV4L2HxkGcs6lfFgsHPxeQvtXLInwdTlrba0TjhN7AdC9vu3000CPmhRGL\n" + | ||
"4opte33ukexjtkzRXoMdYsDv+pbbufi+vlcl439dwMl9toEhFpKt74ZmPfd989m2\n" + | ||
"cvSzTN3iOdjfYlf2jMecDV/s05Cs1k4WgCuXYeBY7KVm0hDH3qSzUwPy2I8lH+Pv\n" + | ||
"ECeypIH0fXNiJurARZ+TndcHicM0ENyEdtM8z8x77oneYioidGC6InotO3FiPiiP\n" + | ||
"g5gkCMmZKgKdwrHqyT7+/TEUd6dggmHyUgLcSdIb6kznwof/vnbGaoIpyGJqGBc/\n" + | ||
"rOk4WY61BOQAwnqxg/81vXTv+xFRSjhHWEoSNToshpZwXKq1OvM4s6xIENBXdz6p\n" + | ||
"V3t4aUYxW7+wBxKxOkl4lq/2MFcTG0szMyiq4E9qrz4kSL6lKEzM/9arXX6+2uOy\n" + | ||
"ccSr02tkCPLSl05R+/unRZJbOTAgFrvLQvl0dFawj7HQaS0sPf53NenXzU9gmVgo\n" + | ||
"ZlINm2XKpmf3xOTCS81TS7ReKj4NMGAw0y0WhXasn18ziTEEK9im99SECsZC/hUU\n" + | ||
"JXBgahepbHqkv8HzJkhV0ExhVn88OOv+Ma2X95ZARmkpbPVBhERRfWVSns12NQzX\n" + | ||
"FAmS+LCbTTaE2xsz0PDSGZwKIFa5lh2QxKaNlSaaYLbOwVtYxf0CdNQHpvBYJSjj\n" + | ||
"v5BNMyleYKCZAuLTqTRDuEPn08sPpii2bOafiKPWJ/dgqRjB2sJKpEj0E9IcO5Mk\n" + | ||
"/SbjyQB7KD0ZwbCQyRC7ffaxjiHkOJV8GwC1x5wB0H5uSfPRIvOmm9FDiZeexbUc\n" + | ||
"EiOYfXiqetKJe9dTcLKFKRYZDHIj3fZThasbA7nC2qthNHy/0T/UFksmMO+VnOUe\n" + | ||
"Nm8Zq0DFZqQx6Q/FMUFWZbZvJDT8YtBATX6XkxHGpkWapnkidowDdsWeWT44HCfk\n" + | ||
"uxCXTkhNRmoQAgOr5Eev2Zda7+ydFK6jxweC5hvR+diobD6ItHllAdBaoOj9hVVs\n" + | ||
"tJ53S9JXi/az7+UlzxUtPgk4kO1jrulQ5XiN2tn6R2pv1ncnIHdqhaQHjSBkUZik\n" + | ||
"vUf8FRscFkvo2EbtKPPLQiY6A1yuvrpobCgWj6TOKeujALF6jzdhepsk7sL2Qg1g\n" + | ||
"6Nah/Tu9I0VOloJlciQenA==\n"; | ||
|
||
private UserDetailsService userDetailsServices = new UserDetailsServiceImpl(); | ||
|
||
@Override | ||
public String createToken(final UserDetails user) { | ||
final String token = Jwts.builder() | ||
.setSubject(user.getUsername()) | ||
.signWith(SignatureAlgorithm.HS512, KEY) | ||
.compact(); | ||
|
||
return token; | ||
} | ||
|
||
public String getUsername(final String token) { | ||
String username = null; | ||
|
||
try { | ||
username = Jwts.parser() | ||
.setSigningKey(KEY) | ||
.parseClaimsJws(token) | ||
.getBody() | ||
.getSubject(); | ||
} catch (SignatureException e) { | ||
return null; | ||
} | ||
return username; | ||
|
||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
webapp/src/main/java/ar/edu/itba/paw/webapp/auth/PlainTextBasicAuthenticationEntryPoint.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,17 @@ | ||
package ar.edu.itba.paw.webapp.auth; | ||
|
||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
public class PlainTextBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint { | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authenticationException) throws IOException, ServletException { | ||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authenticationException.getMessage()); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
webapp/src/main/java/ar/edu/itba/paw/webapp/auth/StatelessAuthenticationFilter.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,29 @@ | ||
package ar.edu.itba.paw.webapp.auth; | ||
|
||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.filter.GenericFilterBean; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import java.io.IOException; | ||
|
||
public class StatelessAuthenticationFilter extends GenericFilterBean { | ||
|
||
private final TokenAuthenticationService tokenAuthenticationService; | ||
|
||
public StatelessAuthenticationFilter(TokenAuthenticationService tokenAuthenticationService) { | ||
this.tokenAuthenticationService = tokenAuthenticationService; | ||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
final Authentication authentication = tokenAuthenticationService.getAuthentication((HttpServletRequest) request); | ||
|
||
SecurityContextHolder.getContext().setAuthentication(authentication); //Set the context to be accessed by the controllers | ||
chain.doFilter(request, response); //Continue the chain of filters | ||
} | ||
} |
Oops, something went wrong.