Fix after review

This commit is contained in:
k.struzhkin 2018-11-22 18:39:49 +03:00
parent df35a01fde
commit 9d6b5f1f42
16 changed files with 40 additions and 61 deletions

View File

@ -4,9 +4,6 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
/**
* @author k.struzhkin on 11/21/18
*/
@ServletComponentScan
@SpringBootApplication(scanBasePackages = {"com.rbkmoney.token.keeper"})
public class TokenKeeperApplication {

View File

@ -10,9 +10,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author k.struzhkin on 11/21/18
*/
@Configuration
public class AppConfig {

View File

@ -15,9 +15,6 @@ import org.springframework.core.io.Resource;
import java.io.IOException;
/**
* @author k.struzhkin on 11/21/18
*/
@Configuration
public class MgConfig {

View File

@ -1,8 +1,5 @@
package com.rbkmoney.token.keeper.exception;
/**
* @author k.struzhkin on 11/21/18
*/
public class TokenEncryptionException extends RuntimeException {
public TokenEncryptionException() {

View File

@ -10,9 +10,6 @@ import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.UUID;
/**
* @author k.struzhkin on 11/21/18
*/
@Component
@RequiredArgsConstructor
public class AuthDataFactory {

View File

@ -6,13 +6,12 @@ import com.rbkmoney.machinarium.domain.TMachineEvent;
import com.rbkmoney.machinarium.handler.AbstractProcessorHandler;
import com.rbkmoney.machinegun.stateproc.ComplexAction;
import com.rbkmoney.token.keeper.AuthData;
import lombok.extern.slf4j.Slf4j;
import java.util.Collections;
import java.util.List;
/**
* @author k.struzhkin on 11/21/18
*/
@Slf4j
public class MgProcessorHandler extends AbstractProcessorHandler<AuthData, AuthData> {
public MgProcessorHandler(Class<AuthData> argsType, Class<AuthData> resultType) {
@ -20,18 +19,27 @@ public class MgProcessorHandler extends AbstractProcessorHandler<AuthData, AuthD
}
@Override
protected SignalResultData<AuthData> processSignalInit(String s, String s1, AuthData authData) {
return new SignalResultData<>(Collections.singletonList(authData), new ComplexAction());
protected SignalResultData<AuthData> processSignalInit(String namespace, String machineId, AuthData authData) {
log.info("Request processSignalInit() namespace: {} machineId: {} authData: {}", namespace, machineId, authData);
SignalResultData<AuthData> authDataSignalResultData = new SignalResultData<>(Collections.singletonList(authData), new ComplexAction());
log.info("Response: {}", authDataSignalResultData);
return authDataSignalResultData;
}
@Override
protected SignalResultData<AuthData> processSignalTimeout(String s, String s1, List<TMachineEvent<AuthData>> list) {
return new SignalResultData<>(Collections.emptyList(), new ComplexAction());
protected SignalResultData<AuthData> processSignalTimeout(String namespace, String machineId, List<TMachineEvent<AuthData>> list) {
log.info("Request processSignalTimeout() namespace: {} machineId: {} list: {}", namespace, machineId, list);
SignalResultData<AuthData> authDataSignalResultData = new SignalResultData<>(Collections.emptyList(), new ComplexAction());
log.info("Response: {}", authDataSignalResultData);
return authDataSignalResultData;
}
@Override
protected CallResultData<AuthData> processCall(String s, String s1, AuthData authData, List<TMachineEvent<AuthData>> list) {
return new CallResultData<>(authData, Collections.singletonList(authData), new ComplexAction());
protected CallResultData<AuthData> processCall(String namespace, String machineId, AuthData authData, List<TMachineEvent<AuthData>> list) {
log.info("Request processCall() namespace: {} machineId: {} list: {}", namespace, machineId, list);
CallResultData<AuthData> callResultData = new CallResultData<>(authData, Collections.singletonList(authData), new ComplexAction());
log.info("Response: {}", callResultData);
return callResultData;
}
}

View File

@ -10,9 +10,6 @@ import org.apache.thrift.TException;
import java.util.Map;
/**
* @author k.struzhkin on 11/21/18
*/
@RequiredArgsConstructor
public class TokenKeeperHandler implements TokenKeeperSrv.Iface {

View File

@ -14,9 +14,6 @@ import java.util.Map;
import static com.rbkmoney.token.keeper.util.ParametersChecker.checkBadParameters;
/**
* @author k.struzhkin on 11/21/18
*/
@Slf4j
public class TokenKeeperWithErrorHandler extends TokenKeeperHandler {
@ -27,7 +24,10 @@ public class TokenKeeperWithErrorHandler extends TokenKeeperHandler {
@Override
public AuthData create(Scope scope, Map<String, String> metadata, String subjectId, String realm) throws TException {
try {
return super.create(scope, metadata, subjectId, realm);
log.info("Request create scope: {} metadata: {} subjectId: {} realm: {}", scope, metadata, subjectId, realm);
AuthData authData = super.create(scope, metadata, subjectId, realm);
log.info("Response: {}", authData);
return authData;
} catch (TokenEncryptionException e) {
log.error("Error when create e: ", e);
throw new TException(e.getMessage());
@ -41,8 +41,12 @@ public class TokenKeeperWithErrorHandler extends TokenKeeperHandler {
public AuthData createWithExpiration(Scope scope, Map<String, String> metadata, String subjectId, String realm,
String expirationTime) throws TException {
try {
log.info("Request createWithExpiration scope: {} metadata: {} subjectId: {} realm: {} expirationTime: {}",
scope, metadata, subjectId, realm, expirationTime);
checkBadParameters(expirationTime, "Bad request parameters, expiration required and not empty arg!");
return super.createWithExpiration(scope, metadata, subjectId, realm, expirationTime);
AuthData authData = super.createWithExpiration(scope, metadata, subjectId, realm, expirationTime);
log.info("Response: {}", authData);
return authData;
} catch (TokenEncryptionException e) {
log.error("Error when createWithExpiration e: ", e);
throw new TException(e.getMessage());
@ -55,8 +59,11 @@ public class TokenKeeperWithErrorHandler extends TokenKeeperHandler {
@Override
public AuthData getByToken(String jwe) throws TException {
try {
log.info("Request getByToken jwe: {}", jwe);
checkBadParameters(jwe, "Bad request parameters, jwe required and not empty arg!");
return super.getByToken(jwe);
AuthData authData = super.getByToken(jwe);
log.info("Response: {}", authData);
return authData;
} catch (TokenEncryptionException e) {
log.error("Error when getByToken e: ", e);
throw new TException(e.getMessage());
@ -72,8 +79,11 @@ public class TokenKeeperWithErrorHandler extends TokenKeeperHandler {
@Override
public AuthData get(String tokenId) throws TException {
try {
log.info("Request get tokenId: {}", tokenId);
checkBadParameters(tokenId, "Bad request parameters, tokenId required and not empty arg!");
return super.get(tokenId);
AuthData authData = super.get(tokenId);
log.info("Response: {}", authData);
return authData;
} catch (AuthDataNotFound e) {
log.error("Error when get. Can't find data by this parameters tokenId: {} e: ", tokenId, e);
throw new AuthDataNotFound(e);
@ -86,8 +96,10 @@ public class TokenKeeperWithErrorHandler extends TokenKeeperHandler {
@Override
public void revoke(String tokenId) throws TException {
try {
log.info("Request revoke tokenId: {}", tokenId);
checkBadParameters(tokenId, "Bad request parameters, tokenId required and not empty arg!");
super.revoke(tokenId);
log.info("Revoked tokenId: {}", tokenId);
} catch (AuthDataNotFound e) {
log.error("Error when revoke. Can't find data by this parameters tokenId: {} e: ", tokenId, e);
throw new AuthDataNotFound(e);

View File

@ -4,9 +4,6 @@ import com.rbkmoney.token.keeper.AuthData;
import java.util.Optional;
/**
* @author k.struzhkin on 11/21/18
*/
public interface AuthDataRepository {
void create(AuthData data);

View File

@ -9,9 +9,6 @@ import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
/**
* @author k.struzhkin on 11/21/18
*/
@Service
@RequiredArgsConstructor
public class AuthDataRepositoryImpl implements AuthDataRepository {

View File

@ -8,9 +8,6 @@ import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
/**
* @author k.struzhkin on 11/21/18
*/
@WebServlet("/mg_processor")
@RequiredArgsConstructor
public class MgProcessorServlet extends GenericServlet {

View File

@ -8,9 +8,6 @@ import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
/**
* @author k.struzhkin on 11/21/18
*/
@WebServlet("/token_keeper")
@RequiredArgsConstructor
public class TokenKeeperServlet extends GenericServlet {

View File

@ -1,8 +1,5 @@
package com.rbkmoney.token.keeper.service;
/**
* @author k.struzhkin on 11/21/18
*/
public interface JweTokenGenerator<T> {
String generate(T scope);

View File

@ -22,9 +22,6 @@ import java.io.IOException;
import java.text.ParseException;
import java.util.Base64;
/**
* @author k.struzhkin on 11/21/18
*/
@Slf4j
@Service
public class JweTokenGeneratorImpl implements JweTokenGenerator<AuthData> {
@ -32,10 +29,10 @@ public class JweTokenGeneratorImpl implements JweTokenGenerator<AuthData> {
private static final ObjectMapper om = new ObjectMapper();
private static final String AUTH_DATA = "authData";
private final String secreteKey;
private final String secretKey;
public JweTokenGeneratorImpl(@Value("${jwe.secrete.key}") String secreteKey) {
this.secreteKey = secreteKey;
this.secretKey = secreteKey;
}
@Override
@ -46,7 +43,7 @@ public class JweTokenGeneratorImpl implements JweTokenGenerator<AuthData> {
.build();
JWEHeader header = new JWEHeader(JWEAlgorithm.A256GCMKW, EncryptionMethod.A256GCM);
EncryptedJWT jwt = new EncryptedJWT(header, jwtClaims);
byte[] decodedKey = Base64.getDecoder().decode(secreteKey);
byte[] decodedKey = Base64.getDecoder().decode(secretKey);
SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
AESEncrypter encrypter = new AESEncrypter(originalKey);
jwt.encrypt(encrypter);
@ -60,7 +57,7 @@ public class JweTokenGeneratorImpl implements JweTokenGenerator<AuthData> {
@Override
public AuthData decode(String jwe) {
try {
byte[] decodedKey = Base64.getDecoder().decode(secreteKey);
byte[] decodedKey = Base64.getDecoder().decode(secretKey);
EncryptedJWT parse = EncryptedJWT.parse(jwe);
AESDecrypter aesDecrypter = new AESDecrypter(decodedKey);
parse.decrypt(aesDecrypter);

View File

@ -3,9 +3,6 @@ package com.rbkmoney.token.keeper.util;
import com.google.common.base.Strings;
import org.apache.thrift.TException;
/**
* @author k.struzhkin on 11/21/18
*/
public class ParametersChecker {
public static void checkBadParameters(String tokenId, String s) throws TException {

View File

@ -18,8 +18,6 @@ public class EncryptedJwtTest {
String jwe = jwtTokenGenerator.generate(withoutExpDate);
AuthData decode = jwtTokenGenerator.decode(jwe);
Assert.assertEquals(TEST, decode.id);
log.info(jwe);
}
}