-
Notifications
You must be signed in to change notification settings - Fork 740
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
16 changed files
with
594 additions
and
7 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
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
32 changes: 32 additions & 0 deletions
32
gate-core/src/main/java/com/netflix/spinnaker/gate/config/AuthenticationRequest.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,32 @@ | ||
package com.netflix.spinnaker.gate.config; | ||
|
||
import java.io.Serializable; | ||
|
||
public class AuthenticationRequest implements Serializable { | ||
|
||
private String username; | ||
private String password; | ||
|
||
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 AuthenticationRequest() {} | ||
|
||
public AuthenticationRequest(String username, String password) { | ||
this.setUsername(username); | ||
this.setPassword(password); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
gate-core/src/main/java/com/netflix/spinnaker/gate/config/AuthenticationResponse.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,16 @@ | ||
package com.netflix.spinnaker.gate.config; | ||
|
||
import java.io.Serializable; | ||
|
||
public class AuthenticationResponse implements Serializable { | ||
|
||
private final String jwt; | ||
|
||
public AuthenticationResponse(String jwt) { | ||
this.jwt = jwt; | ||
} | ||
|
||
public String getJwt() { | ||
return jwt; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
gate-core/src/main/java/com/netflix/spinnaker/gate/config/JwtAuthenticationEntryPoint.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.netflix.spinnaker.gate.config; | ||
|
||
import java.io.IOException; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
@Override | ||
public void commence( | ||
HttpServletRequest httpServletRequest, | ||
HttpServletResponse httpServletResponse, | ||
AuthenticationException e) | ||
throws IOException, ServletException { | ||
httpServletResponse.sendError( | ||
HttpServletResponse.SC_UNAUTHORIZED, | ||
"Sorry, You're not authorized to access this resource."); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
gate-core/src/main/java/com/netflix/spinnaker/gate/config/JwtRequestFilter.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,72 @@ | ||
package com.netflix.spinnaker.gate.config; | ||
|
||
import com.netflix.spinnaker.gate.services.UserDataService; | ||
import io.jsonwebtoken.ExpiredJwtException; | ||
import io.jsonwebtoken.MalformedJwtException; | ||
import io.jsonwebtoken.SignatureException; | ||
import io.jsonwebtoken.UnsupportedJwtException; | ||
import java.io.IOException; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
@Component | ||
public class JwtRequestFilter extends OncePerRequestFilter { | ||
|
||
@Autowired private JwtUtil jwtUtil; | ||
@Autowired UserDataService userDetailsService; | ||
|
||
@Override | ||
protected void doFilterInternal( | ||
HttpServletRequest request, HttpServletResponse response, FilterChain chain) | ||
throws ServletException, IOException { | ||
|
||
final String authorizationHeader = request.getHeader("Authorization"); | ||
|
||
String username = null; | ||
String jwt = null; | ||
|
||
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) { | ||
jwt = authorizationHeader.substring(7); | ||
try { | ||
username = jwtUtil.extractUsername(jwt); | ||
} catch (SignatureException ex) { | ||
logger.error("Invalid JWT signature"); | ||
} catch (MalformedJwtException ex) { | ||
logger.error("Invalid JWT token"); | ||
} catch (ExpiredJwtException ex) { | ||
logger.error("Expired JWT token"); | ||
} catch (UnsupportedJwtException ex) { | ||
logger.error("Unsupported JWT token"); | ||
} catch (IllegalArgumentException ex) { | ||
logger.error("JWT claims string is empty."); | ||
} | ||
} | ||
|
||
if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) { | ||
|
||
UserDetails userDetails = this.userDetailsService.loadUserByUsername(username); | ||
|
||
if (jwtUtil.validateToken(jwt, userDetails)) { | ||
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = | ||
new UsernamePasswordAuthenticationToken( | ||
userDetails, null, userDetails.getAuthorities()); | ||
usernamePasswordAuthenticationToken.setDetails( | ||
new WebAuthenticationDetailsSource().buildDetails(request)); | ||
SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken); | ||
} | ||
} | ||
// Pass request down the chain, except for OPTIONS | ||
if (!"OPTIONS".equalsIgnoreCase(request.getMethod())) { | ||
chain.doFilter(request, response); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
gate-core/src/main/java/com/netflix/spinnaker/gate/config/JwtUtil.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,59 @@ | ||
package com.netflix.spinnaker.gate.config; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class JwtUtil { | ||
|
||
private String SECRET_KEY = "secret"; | ||
|
||
public String extractUsername(String token) { | ||
return extractClaim(token, Claims::getSubject); | ||
} | ||
|
||
public Date extractExpiration(String token) { | ||
return extractClaim(token, Claims::getExpiration); | ||
} | ||
|
||
public <T> T extractClaim(String token, Function<Claims, T> claimsResolver) { | ||
final Claims claims = extractAllClaims(token); | ||
return claimsResolver.apply(claims); | ||
} | ||
|
||
private Claims extractAllClaims(String token) { | ||
return Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token).getBody(); | ||
} | ||
|
||
private Boolean isTokenExpired(String token) { | ||
return extractExpiration(token).before(new Date()); | ||
} | ||
|
||
public String generateToken(UserDetails userDetails) { | ||
Map<String, Object> claims = new HashMap<>(); | ||
return createToken(claims, userDetails.getUsername()); | ||
} | ||
|
||
private String createToken(Map<String, Object> claims, String subject) { | ||
|
||
return Jwts.builder() | ||
.setClaims(claims) | ||
.setSubject(subject) | ||
.setIssuedAt(new Date(System.currentTimeMillis())) | ||
.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 10)) | ||
.signWith(SignatureAlgorithm.HS256, SECRET_KEY) | ||
.compact(); | ||
} | ||
|
||
public Boolean validateToken(String token, UserDetails userDetails) { | ||
final String username = extractUsername(token); | ||
return (username.equals(userDetails.getUsername()) && !isTokenExpired(token)); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
gate-core/src/main/java/com/netflix/spinnaker/gate/services/UserDataService.groovy
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 com.netflix.spinnaker.gate.services | ||
|
||
import org.springframework.security.core.GrantedAuthority | ||
import org.springframework.security.core.userdetails.User | ||
import org.springframework.security.core.userdetails.UserDetails | ||
import org.springframework.security.core.userdetails.UserDetailsService | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class UserDataService implements UserDetailsService { | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { | ||
return new User(s, s, new ArrayList<GrantedAuthority>()) | ||
} | ||
} |
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.