com.rabobank.argos.service.security.TokenProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of argos-service Show documentation
Show all versions of argos-service Show documentation
Argos service for Argos Supply Chain Notary
/*
* Copyright (C) 2019 - 2020 Rabobank Nederland
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rabobank.argos.service.security;
import com.rabobank.argos.service.security.oauth2.ArgosOAuth2User;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.UnsupportedJwtException;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.security.SignatureException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.crypto.SecretKey;
import java.sql.Timestamp;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Base64;
import java.util.Date;
@Service
@RequiredArgsConstructor
@Slf4j
public class TokenProvider {
@Value("${jwt.token.secret}")
private String secret;
@Value("#{T(java.time.Duration).parse('${jwt.token.expiration}')}")
private Duration timeout;
private SecretKey secretKey;
/**
* create secret for application.yml
* jwt:
* token:
* secret: generated secret
*
* @param args not used
*/
public static void main(String[] args) {
log.info(Base64.getEncoder().encodeToString(Keys.secretKeyFor(SignatureAlgorithm.HS512).getEncoded()));
}
@PostConstruct
public void init() {
secretKey = Keys.hmacShaKeyFor(Decoders.BASE64.decode(this.secret));
}
public String createToken(Authentication authentication) {
ArgosOAuth2User oAuth2User = (ArgosOAuth2User) authentication.getPrincipal();
return Jwts.builder()
.setSubject(oAuth2User.getAccountId())
.setIssuedAt(new Date())
.setExpiration(Timestamp.valueOf(LocalDateTime.now().plus(timeout)))
.signWith(secretKey)
.compact();
}
public String getUserIdFromToken(String token) {
Claims claims = Jwts.parser()
.setSigningKey(secretKey)
.parseClaimsJws(token)
.getBody();
return claims.getSubject();
}
public boolean validateToken(String authToken) {
try {
Jwts.parser().setSigningKey(secretKey).parseClaimsJws(authToken);
return true;
} catch (SignatureException ex) {
log.error("Invalid JWT signature");
} catch (MalformedJwtException ex) {
log.error("Invalid JWT token");
} catch (ExpiredJwtException ex) {
log.error("Expired JWT token");
} catch (UnsupportedJwtException ex) {
log.error("Unsupported JWT token");
} catch (IllegalArgumentException ex) {
log.error("JWT claims string is empty.");
}
return false;
}
}