From f4c0a5facd5e14729fec7c0503b1c137e9cc9ed6 Mon Sep 17 00:00:00 2001 From: jitender Date: Thu, 18 Jul 2019 15:21:14 +0530 Subject: [PATCH 1/2] updated auth and account addresses --- releases/dev/airdrop-service.yaml | 6 +++--- releases/staging/airdrop-service.yaml | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/releases/dev/airdrop-service.yaml b/releases/dev/airdrop-service.yaml index 84cce59..dfa2253 100644 --- a/releases/dev/airdrop-service.yaml +++ b/releases/dev/airdrop-service.yaml @@ -55,11 +55,11 @@ spec: # GRPC PROPERTIES grpc: accountService: - host: account.dev-eu.nynja.net + host: account-service.account.svc.cluster.local port: 6565 authService: - host: auth.dev-eu.nynja.net - port: 6565 + host: auth-service.auth.svc.cluster.local + port: 6566 # REST PROPERTIES rest: pushService: diff --git a/releases/staging/airdrop-service.yaml b/releases/staging/airdrop-service.yaml index 493d90b..3a0c753 100644 --- a/releases/staging/airdrop-service.yaml +++ b/releases/staging/airdrop-service.yaml @@ -55,11 +55,11 @@ spec: # GRPC PROPERTIES grpc: accountService: - host: account.staging.nynja.net + host: account-service.account.svc.cluster.local port: 6565 authService: - host: auth.staging.nynja.net - port: 6565 + host: auth-service.auth.svc.cluster.local + port: 6566 # REST PROPERTIES rest: pushService: -- GitLab From f1fff220d436f141740ee231467a4265fc2f09cd Mon Sep 17 00:00:00 2001 From: Jayendra Date: Mon, 22 Jul 2019 11:32:01 +0530 Subject: [PATCH 2/2] Integrated push message API to send OTP messages --- pom.xml | 2 +- .../airdrop/CassandraStartupScripts.java | 2 +- .../airdrop/constants/QoinProEndpoints.java | 2 +- .../airdrop/controller/AirdropController.java | 123 ++++++++++++++---- .../airdrop/grpc/AccountServiceClient.java | 23 +++- .../nynja/airdrop/kafka/KafkaConsumer.java | 19 ++- .../AuthorizationServerConfiguration.java | 2 + .../airdrop/security/CustomTokenEnhancer.java | 2 + .../nynja/airdrop/util/RestTemplateUtil.java | 88 ++++++++++++- src/main/resources/application.yml | 5 +- 10 files changed, 222 insertions(+), 46 deletions(-) diff --git a/pom.xml b/pom.xml index d6e13ff..450d13c 100644 --- a/pom.xml +++ b/pom.xml @@ -209,6 +209,6 @@ 8 - + \ No newline at end of file diff --git a/src/main/java/biz/nynja/airdrop/CassandraStartupScripts.java b/src/main/java/biz/nynja/airdrop/CassandraStartupScripts.java index 7760aae..88de0dc 100644 --- a/src/main/java/biz/nynja/airdrop/CassandraStartupScripts.java +++ b/src/main/java/biz/nynja/airdrop/CassandraStartupScripts.java @@ -29,7 +29,7 @@ public class CassandraStartupScripts { List cassandraScripts = new ArrayList(6); - String createKeyspace = "CREATE KEYSPACE IF NOT EXISTS " + KEYSPACE + " WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 1};"; + String createKeyspace = "CREATE KEYSPACE IF NOT EXISTS test2 WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 1};"; String createTableAirdropProfile = "CREATE TABLE IF NOT EXISTS " + KEYSPACE + "." + cassandraTemplate.getTableName(AirdropProfiles.class).toString() + " (id uuid, account_id uuid, username text, referrer_code text, created_ts bigint, modified_ts bigint, is_verified tinyint ,verified_ts bigint,PRIMARY KEY(account_id,id));"; String createTableAirdropActions = "CREATE TABLE IF NOT EXISTS " + KEYSPACE + "." + cassandraTemplate.getTableName(AirdropActions.class).toString() + " (id uuid, account_id uuid, action_type text, last_action_ts bigint, count int, PRIMARY KEY(account_id,action_type,id));"; String createTableOtpSessions = "CREATE TABLE IF NOT EXISTS " + KEYSPACE + "." + cassandraTemplate.getTableName(OtpSessions.class).toString() + " (id uuid, created_ts bigint, otp int, is_verified tinyint, verified_ts bigint, account_id uuid, expired_ts bigint, PRIMARY KEY(id));"; diff --git a/src/main/java/biz/nynja/airdrop/constants/QoinProEndpoints.java b/src/main/java/biz/nynja/airdrop/constants/QoinProEndpoints.java index 130b053..687e38a 100644 --- a/src/main/java/biz/nynja/airdrop/constants/QoinProEndpoints.java +++ b/src/main/java/biz/nynja/airdrop/constants/QoinProEndpoints.java @@ -2,5 +2,5 @@ package biz.nynja.airdrop.constants; public interface QoinProEndpoints { - String qoinProEndPoints [] = {"/test11", "/v1/verifyUser", "/v1/verifyOtp", "/v1/resendOtp", "/v1/eventData", "/v1/failedEventData"}; + String qoinProEndPoints [] = {"/test/1", "/v1/verifyUser", "/v1/verifyOtp", "/v1/resendOtp", "/v1/eventData", "/v1/failedEventData"}; } diff --git a/src/main/java/biz/nynja/airdrop/controller/AirdropController.java b/src/main/java/biz/nynja/airdrop/controller/AirdropController.java index ec6fc29..8b1ad15 100644 --- a/src/main/java/biz/nynja/airdrop/controller/AirdropController.java +++ b/src/main/java/biz/nynja/airdrop/controller/AirdropController.java @@ -24,7 +24,17 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.*; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; + +import javax.servlet.http.HttpServletRequest; import static biz.nynja.airdrop.util.HttpConstants.statusCode.*; import static biz.nynja.airdrop.util.HttpConstants.MessageTags.*; @@ -99,34 +109,106 @@ public class AirdropController { @Value("${grpc.authService.port}") private int GRPC_AUTH_SERVICE_PORT; - private static final Logger logger = LoggerFactory.getLogger(AirdropController.class); - @GetMapping("/") - public String index() { + @Autowired + private RestTemplate restTemplate; - return "Airdrop Service"; - } + // AccountId -> 6065b9bf-a69c-4837-99bd-489065f106f1 - @GetMapping("/rest/exchangeRefreshToken") - public String restExchangeRefreshToken(@RequestHeader(value = "Authorization") String token) { + @GetMapping("/test/pushNotification/{username}/{password}") + public String testPushNotification(@PathVariable("username") String username, + @PathVariable("password") String password + ) { System.out.println(""); - System.out.println("-----------"); - System.out.println("Rest Exchange Refresh Token"); - System.out.println("-----------"); + System.out.println("----------------"); + System.out.println(""); + System.out.println("Test Push Notification : "); + System.out.println(""); + System.out.println("username : " + username); + System.out.println("password : " + password); + System.out.println(""); + System.out.println("----------------"); System.out.println(""); - Object response = restTemplateUtil.exchangeRefreshToken(token); + // UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://35.198.110.223:8888/chat/6065b9bf-a69c-4837-99bd-489065f106f1/qoinpro?sender=qoin_pro&message='This is a test Message'"); + // UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://35.198.110.223:8888/chat/3dd830d4-ffa3-4327-9866-9f8d8efbb23c/qoinpro"); + + String authCredentials = username + ":" + password; + byte[] authCredentialsBytes = authCredentials.getBytes(); + byte[] base64Credentials = Base64.getEncoder().encode(authCredentialsBytes); + + MultiValueMap adminTokenParams = new LinkedMultiValueMap<>(1); + adminTokenParams.add("message", "This is a test message."); + + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.add("Authorization", "Basic " + new String(base64Credentials)); - System.out.println("Response : "); + HttpEntity request = new HttpEntity<>( httpHeaders); + + + ResponseEntity response = restTemplate.exchange("http://35.198.110.223:8888/chat/3dd830d4-ffa3-4327-9866-9f8d8efbb23c/qoinpro?sender=qoin_pro&message=This is a test demo message", HttpMethod.GET, request, String.class); + + System.out.println(""); + System.out.println("================"); + System.out.println(""); + System.out.println("Test Response : "); System.out.println(response); + System.out.println(""); + System.out.println(""); + System.out.println("Response Body : "); + System.out.println(response.getBody()); + System.out.println(""); + System.out.println("================"); System.out.println(""); - return response.toString(); + + return response.getBody(); + } + + @GetMapping("/test/basicAuth") + public String testBasicAuth(HttpServletRequest request) { + + System.out.println(""); + System.out.println("---------------"); + System.out.println(""); + System.out.println(""); + System.out.println(""); + System.out.println("PUSH NOTIFICATION TEST"); + System.out.println(""); + System.out.println("Request : "); + System.out.println(""); + System.out.println("Authorization : "); + System.out.println(request.getHeader("authorization")); + System.out.println(""); + System.out.println("---------------"); + System.out.println(""); + + String basic = new String(Base64.getDecoder().decode(request.getHeader("authorization").split(" ")[1])); + + System.out.println(""); + System.out.println("basic : "); + System.out.println(basic); + System.out.println(""); + + return "Push Notification Test done : " + basic; + } + + + @GetMapping("/") + public String index() { + + restTemplateUtil.sendPushNotification("3dd830d4-ffa3-4327-9866-9f8d8efbb23c", 0000); + + return "Airdrop Service"; + } + + + @GetMapping("/test/1") public String testEndpoint1() { @@ -471,17 +553,11 @@ public class AirdropController { responseData.put("otp_session_id", otpSessionId); responseData.put("expiry_duration", OTP_EXPIRE_TIME); - /* - - // HERE, NYNJA PUSH SERVICE WILL BE CALLED TO SEND NOTIFICATION TO USER - - */ - System.out.println(""); System.out.println("SEND PUSH NOTIFICATION API CALLED HERE"); System.out.println(""); - // restTemplateUtil.sendPushNotification(accountId.toString(), otp); + restTemplateUtil.sendPushNotification(accountId.toString(), otp); return HttpResponse.getResponse(SUCCESS_STATUS_CODE, responseData, null); } @@ -886,16 +962,11 @@ public class AirdropController { otpSessionsService.addOtpSessions(otpSessionsObj); - /* - - // HERE, NYNJA PUSH SERVICE WILL BE CALLED TO SEND NOTIFICATION TO USER - - */ System.out.println(""); System.out.println("SEND PUSH NOTIFICATION API CALLED HERE"); System.out.println(""); - // restTemplateUtil.sendPushNotification(otpSessions.getAccount_id().toString(), otp); + restTemplateUtil.sendPushNotification(otpSessions.getAccount_id().toString(), otp); return HttpResponse.getResponse(SUCCESS_STATUS_CODE, responseData, null); } diff --git a/src/main/java/biz/nynja/airdrop/grpc/AccountServiceClient.java b/src/main/java/biz/nynja/airdrop/grpc/AccountServiceClient.java index e1ac039..e460f94 100644 --- a/src/main/java/biz/nynja/airdrop/grpc/AccountServiceClient.java +++ b/src/main/java/biz/nynja/airdrop/grpc/AccountServiceClient.java @@ -126,9 +126,8 @@ public class AccountServiceClient { Metadata.Key key = Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER); header.put(key, "Bearer " + token); - - - accountServiceStub = MetadataUtils.attachHeaders(accountServiceStub, header);*/ + accountServiceStub = MetadataUtils.attachHeaders(accountServiceStub, header); + */ ManagedChannel managedChannel = ManagedChannelBuilder.forAddress(GRPC_ACCOUNT_SERVICE_HOST, GRPC_ACCOUNT_SERVICE_PORT) .usePlaintext().build(); @@ -174,6 +173,24 @@ public class AccountServiceClient { return null; } + if (searchResultDetails.getAccountId() == null) { + + System.out.println(""); + System.out.println("SearchResultDetails getAccountId is Null"); + System.out.println(""); + + return null; + } + + if (searchResultDetails.getAccountId().equals("")) { + + System.out.println(""); + System.out.println("SearchResultDetails getAccountId is Blank"); + System.out.println(""); + + return null; + } + return UUID.fromString(searchResultDetails.getAccountId()); } diff --git a/src/main/java/biz/nynja/airdrop/kafka/KafkaConsumer.java b/src/main/java/biz/nynja/airdrop/kafka/KafkaConsumer.java index fa88cf7..a44e871 100644 --- a/src/main/java/biz/nynja/airdrop/kafka/KafkaConsumer.java +++ b/src/main/java/biz/nynja/airdrop/kafka/KafkaConsumer.java @@ -108,14 +108,20 @@ public class KafkaConsumer { // UNCOMMENT IT LATER - // String response = restTemplateUtil.qoinProChannelEngagementAPI(airdropProfilesList.get(0).getAccount_id().toString(), airdropProfilesList.get(0).getUsername(), kafkaMessageTemplate.getAction_type(), actionsSummary.getAction_description()); - // notifiedQoinPro = (response.equals("Success")) ? true : false; + String response = restTemplateUtil.qoinProChannelEngagementAPI(airdropProfilesList.get(0).getAccount_id().toString(), airdropProfilesList.get(0).getUsername(), kafkaMessageTemplate.getAction_type(), actionsSummary.getAction_description()); System.out.println(""); + System.out.println("QOIN-PRO Channel Engagement API Response"); + System.out.println(response); + System.out.println(""); + + notifiedQoinPro = (response.equals("Success")) ? true : false; + + /*System.out.println(""); System.out.println("QOIN-PRO API Response is : SUCCESS"); System.out.println(""); - notifiedQoinPro = true; + notifiedQoinPro = true;*/ } // If qoinPro response message is "Success" than data will be added in AirdropActions Table @@ -152,9 +158,9 @@ public class KafkaConsumer { airdropActionsService.updateAirdropActions(accountId, airdropActionsList.get(0).getId(), kafkaMessageTemplate.getAction_type(), updateMap); } else { - System.out.println(""); - System.out.println("" + Constants.KafkaActionType.JOIN + " Action is already consumed. So, It won't be consumed again"); - System.out.println(""); + System.out.println(""); + System.out.println("" + Constants.KafkaActionType.JOIN + " Action is already consumed. So, It won't be consumed again"); + System.out.println(""); logger.info("" + Constants.KafkaActionType.JOIN + " Action is already consumed. So, It won't be consumed again"); @@ -187,7 +193,6 @@ public class KafkaConsumer { } - acknowledgment.acknowledge(); } } diff --git a/src/main/java/biz/nynja/airdrop/security/AuthorizationServerConfiguration.java b/src/main/java/biz/nynja/airdrop/security/AuthorizationServerConfiguration.java index f7e7d73..b3fc36a 100644 --- a/src/main/java/biz/nynja/airdrop/security/AuthorizationServerConfiguration.java +++ b/src/main/java/biz/nynja/airdrop/security/AuthorizationServerConfiguration.java @@ -52,6 +52,8 @@ public class AuthorizationServerConfiguration extends AuthorizationServerConfigu .tokenStore(tokenStore).tokenEnhancer(customTokenEnhancer); } + // localhost:8080/airdrop/oauth/token?grant_type=password&password=test&username=qoinPro + @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() diff --git a/src/main/java/biz/nynja/airdrop/security/CustomTokenEnhancer.java b/src/main/java/biz/nynja/airdrop/security/CustomTokenEnhancer.java index ec13ed7..8d6d2b5 100644 --- a/src/main/java/biz/nynja/airdrop/security/CustomTokenEnhancer.java +++ b/src/main/java/biz/nynja/airdrop/security/CustomTokenEnhancer.java @@ -8,6 +8,8 @@ import org.springframework.stereotype.Component; @Component public class CustomTokenEnhancer implements org.springframework.security.oauth2.provider.token.TokenEnhancer { + + // Here, Response of OAuth Token is updated and RefreshToken is removed. @Override public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) { diff --git a/src/main/java/biz/nynja/airdrop/util/RestTemplateUtil.java b/src/main/java/biz/nynja/airdrop/util/RestTemplateUtil.java index 4f0f387..7dec831 100644 --- a/src/main/java/biz/nynja/airdrop/util/RestTemplateUtil.java +++ b/src/main/java/biz/nynja/airdrop/util/RestTemplateUtil.java @@ -8,12 +8,15 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; +import java.util.Base64; + @Component public class RestTemplateUtil { @@ -29,13 +32,21 @@ public class RestTemplateUtil { @Value("${authentication.qoinPro.header.value}") private String quionProAuthenticationHeaderValue; - private static final Logger logger = LoggerFactory.getLogger(RestTemplateUtil.class); @Autowired private RestTemplate restTemplate; + @Value("${authentication.nynjaP2PMessaging.username}") + private String nynjaP2PUserName; + + @Value("${authentication.nynjaP2PMessaging.password}") + private String nynjaP2PPassword; + @Value("${authentication.nynjaP2PMessaging.sender}") + private String nynjaP2PSender; + +/* public Object exchangeRefreshToken(String refreshToken) { UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8085/tokens/refresh") @@ -54,6 +65,7 @@ public class RestTemplateUtil { return response; } +*/ public String qoinProChannelEngagementAPI(String userId, String username, String actionType, String actionDescription) { @@ -89,6 +101,44 @@ public class RestTemplateUtil { public void sendPushNotification(String accountId, int otp) { + System.out.println(""); + System.out.println("Send OTP Message Function"); + System.out.println(""); + System.out.println("nynjaP2PSender : " + nynjaP2PSender); + System.out.println(""); + System.out.println("nynjaP2PUserName : " + nynjaP2PUserName); + System.out.println("nynjaP2PPassword : " + nynjaP2PPassword); + System.out.println(""); + + String authCredentials = nynjaP2PUserName + ":" + nynjaP2PPassword; + byte[] authCredentialsBytes = authCredentials.getBytes(); + byte[] base64Credentials = Base64.getEncoder().encode(authCredentialsBytes); + + + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.add("Authorization", "Basic " + new String(base64Credentials)); + + HttpEntity request = new HttpEntity<>(httpHeaders); + + + ResponseEntity response = restTemplate.exchange("http://35.198.110.223:8888/chat/" + accountId + "/qoinpro?sender=" + nynjaP2PSender + "&message=Your verification OTP is " + otp +"", HttpMethod.GET, request, String.class); + + + System.out.println(""); + System.out.println("Response : "); + System.out.println(response); + System.out.println(""); + System.out.println("-----------"); + System.out.println(""); + System.out.println("Response Body : "); + System.out.println(response.getBody()); + System.out.println(""); + + } + +/* + public void sendPushNotification(String accountId, int otp) { +*//* MultiValueMap pushNotificationParams = new LinkedMultiValueMap(2); pushNotificationParams.add("account_id", accountId); pushNotificationParams.add("otp", otp); @@ -97,13 +147,39 @@ public class RestTemplateUtil { boolean response = restTemplate.postForObject(pushNotification, httpEntity, Boolean.class); - logger.info("Send Push Notification API Response: {}", response); + logger.info("Send Push Notification API Response: {}", response);*//* - /* System.out.println(""); - System.out.println("SendPushNotificationResponse : " + response); - System.out.println("");*/ + String authCredentials = nynjaP2PUserName + ":" + nynjaP2PPassword; + byte[] authCredentialsBytes = authCredentials.getBytes(); + byte[] base64Credentials = Base64.getEncoder().encode(authCredentialsBytes); - } + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.add("Authorization", "Basic " + new String(base64Credentials)); + + HttpEntity request = new HttpEntity<>(httpHeaders); + + + ResponseEntity response = restTemplate.exchange("http://35.198.110.223:8888/chat/" + accountId + + "/qoinpro?sender=qoin_pro&message=This is a test demo message", HttpMethod.GET, request, String.class); + + System.out.println(""); + System.out.println("================"); + System.out.println(""); + System.out.println("Test Response : "); + System.out.println(response); + System.out.println(""); + System.out.println(""); + System.out.println("Response Body : "); + System.out.println(response.getBody()); + System.out.println(""); + System.out.println("================"); + System.out.println(""); + + + // return response.getBody(); + + + }*/ } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index b760753..8a3b4dd 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -77,7 +77,10 @@ authentication: header: name: 'x-api-key' value: 'NEzzAZLl0Ma1Jos1bqqfk2TFHTywQ0Qo9IAFaO0y' - + nynjaP2PMessaging: # This service will be called to send OTP message on P2P chat connection. + username: nynja + password: nynjaTS + sender: qoin_pro # This will be send in request parameters. # QOIN-PRO TOKEN AUTHENTICATION DETAILS users: -- GitLab