| /** |
| * 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 io.streamnative.pulsar.manager.service.impl; |
| |
| import io.streamnative.pulsar.manager.service.JwtService; |
| |
| import io.jsonwebtoken.SignatureAlgorithm; |
| import io.jsonwebtoken.Claims; |
| import io.jsonwebtoken.Jws; |
| import io.jsonwebtoken.Jwts; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.beans.factory.annotation.Value; |
| import org.springframework.stereotype.Service; |
| |
| import java.util.Date; |
| import java.util.Map; |
| import java.util.Optional; |
| import java.util.concurrent.ConcurrentHashMap; |
| |
| @Service |
| public class JwtServiceImpl implements JwtService { |
| |
| private String secret; |
| private int sessionTime; |
| |
| private final Map<String, String> tokens = new ConcurrentHashMap<>(); |
| |
| @Autowired |
| public JwtServiceImpl(@Value("${jwt.secret}") String secret, |
| @Value("${jwt.sessionTime}") int sessionTime) { |
| this.secret = secret; |
| this.sessionTime = sessionTime; |
| } |
| |
| @Override |
| public String toToken(String id) { |
| return Jwts.builder() |
| .setSubject(id) |
| .setExpiration(expireTimeFromNow()) |
| .signWith(SignatureAlgorithm.HS512, secret) |
| .compact(); |
| } |
| |
| @Override |
| public Optional<String> getSubFromToken(String token) { |
| try { |
| Jws<Claims> claimsJws = Jwts.parser().setSigningKey(secret).parseClaimsJws(token); |
| return Optional.ofNullable(claimsJws.getBody().getSubject()); |
| } catch (Exception e) { |
| return Optional.empty(); |
| } |
| } |
| |
| private Date expireTimeFromNow() { |
| return new Date(System.currentTimeMillis() + sessionTime * 1000); |
| } |
| |
| @Override |
| public void setToken(String key, String value) { |
| synchronized (this.tokens) { |
| this.tokens.put(key, value); |
| } |
| } |
| |
| @Override |
| public String getToken(String key) { |
| return this.tokens.get(key); |
| } |
| |
| @Override |
| public void removeToken(String key) { |
| synchronized (this.tokens) { |
| this.tokens.remove(key); |
| } |
| } |
| } |