forked from jitpack/gradle-simple
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAH-2962 | Created a script to run on sms-service startup
* Added token generation script to run on docker up of sms-service * Refactored test cases * Update TokenValidator.java * Update TokenValidator.java * BAH-2962-token | added spring-security filter to validate token. * Update SMSController.java * BAH-2962-token | added spring-security filter to validate token. * BAH-2962-token | added spring-security filter to validate token. --------- Co-authored-by: atish160384 <atish@beehyv.com>
- Loading branch information
1 parent
3f6c17b
commit 2a86a8f
Showing
9 changed files
with
239 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
FROM amazoncorretto:11.0.18 | ||
VOLUME /tmp | ||
RUN yum install openssl -y | ||
COPY package/docker/generate_token.sh /home/ | ||
COPY package/docker/sms-startup.sh / | ||
RUN chmod +x /home/generate_token.sh /sms-startup.sh | ||
COPY build/libs/* app.jar | ||
EXPOSE 8080 | ||
ENTRYPOINT ["java", "-jar", "/app.jar"] | ||
VOLUME /tmp | ||
CMD ["./sms-startup.sh"] |
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,34 @@ | ||
#!/bin/bash | ||
|
||
header='{"alg": "RS256", "typ": "JWT"}' | ||
token_dir="/opt/tokens" | ||
private_key_path="/opt/private_key.pkcs8" | ||
public_key_path="/opt/public_key.crt" | ||
|
||
if [ ! -f "$private_key_path" ]; then | ||
private_key_tempfile=$(mktemp) | ||
openssl genrsa -out $private_key_tempfile 2048 | ||
openssl pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in $private_key_tempfile -out $private_key_path | ||
rm $private_key_tempfile | ||
fi | ||
|
||
if [ ! -f "$public_key_path" ]; then | ||
openssl rsa -pubout -in $private_key_path -out $public_key_path | ||
fi | ||
|
||
mkdir -p $token_dir | ||
|
||
token_file="$token_dir/sms-communications-token.txt" | ||
claims='{"user": "bahmni-emr", "token_id": "'$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1)'"}' | ||
|
||
encoded_header=$(echo -n $header | base64 | tr '+/' '-_' | tr -d '=') | ||
encoded_payload=$(echo -n $claims | base64 | tr '+/' '-_' | tr -d '=') | ||
|
||
data_to_sign="${encoded_header}.${encoded_payload}" | ||
|
||
signature=$(echo -n "$data_to_sign" | openssl dgst -sha256 -sign $private_key_path | base64 | tr '+/' '-_' | tr -d '=') | ||
|
||
jwt_token="${data_to_sign}.${signature}" | ||
jwt_token=$(echo -n "$jwt_token" | tr -d '\n') | ||
|
||
echo "$jwt_token" > $token_file |
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,3 @@ | ||
#!/bin/bash | ||
./home/generate_token.sh & | ||
java -jar app.jar |
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
33 changes: 33 additions & 0 deletions
33
src/main/java/org/bahmni/sms/web/config/SecurityConfig.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,33 @@ | ||
package org.bahmni.sms.web.config; | ||
|
||
import org.bahmni.sms.web.security.TokenValidator; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; | ||
|
||
|
||
@Configuration | ||
@EnableWebSecurity | ||
public class SecurityConfig { | ||
|
||
@Autowired | ||
TokenValidator tokenValidator; | ||
@Bean | ||
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception { | ||
|
||
httpSecurity | ||
.csrf() | ||
.disable() | ||
.authorizeHttpRequests((authorize) -> authorize | ||
.antMatchers("/*") | ||
.permitAll()) | ||
.addFilterBefore(new TokenValidatorFilter(tokenValidator), BasicAuthenticationFilter.class); | ||
|
||
return httpSecurity.build(); | ||
} | ||
} | ||
|
40 changes: 40 additions & 0 deletions
40
src/main/java/org/bahmni/sms/web/config/TokenValidatorFilter.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,40 @@ | ||
package org.bahmni.sms.web.config; | ||
|
||
import org.bahmni.sms.web.security.TokenValidator; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
import java.io.IOException; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
|
||
public class TokenValidatorFilter extends OncePerRequestFilter { | ||
|
||
private TokenValidator tokenValidator; | ||
|
||
|
||
public TokenValidatorFilter(TokenValidator tokenValidator) { | ||
this.tokenValidator = tokenValidator; | ||
} | ||
|
||
|
||
|
||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
boolean isValidToken = false; | ||
|
||
String AuthHeader = request.getHeader("Authorization"); | ||
if (AuthHeader != null && AuthHeader.startsWith("Bearer ")) { | ||
String token = AuthHeader.substring(7); | ||
isValidToken = tokenValidator.validateToken(token); | ||
} | ||
if (!isValidToken) { | ||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid token"); | ||
return; | ||
} | ||
|
||
filterChain.doFilter(request,response); | ||
} | ||
} |
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
110 changes: 110 additions & 0 deletions
110
src/test/java/org/bahmni/sms/web/SMSControllerTest.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,110 @@ | ||
package org.bahmni.sms.web; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.http.HttpVersion; | ||
import org.apache.http.impl.DefaultHttpResponseFactory; | ||
import org.apache.http.message.BasicStatusLine; | ||
import org.bahmni.sms.SMSSender; | ||
import org.bahmni.sms.web.security.OpenMRSAuthenticator; | ||
import org.bahmni.sms.web.security.TokenValidator; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mockito; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import org.springframework.test.web.reactive.server.WebTestClient; | ||
|
||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(SpringExtension.class) | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@AutoConfigureWebTestClient | ||
class SMSControllerTest { | ||
@MockBean | ||
private SMSSender smsSender; | ||
|
||
@Autowired | ||
private WebTestClient webClient; | ||
|
||
@MockBean | ||
private OpenMRSAuthenticator authenticator; | ||
|
||
@MockBean | ||
private TokenValidator tokenValidator; | ||
|
||
|
||
@Test | ||
public void shouldAcceptTheSMSRequest() { | ||
Object requestBody = "{" + | ||
"\"phoneNumber\":\"+919999999999\"," + | ||
"\"message\":\"hello\"" + | ||
"}"; | ||
when(tokenValidator.validateToken("dummy")).thenReturn(Boolean.valueOf("true")); | ||
webClient.post() | ||
.uri("/notification/sms") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.bodyValue(requestBody) | ||
.header("Authorization","Bearer dummy") | ||
.exchange() | ||
.expectStatus() | ||
.is2xxSuccessful(); | ||
} | ||
|
||
@Test | ||
public void shouldThrowBadRequest() { | ||
Object requestBody = "{" + | ||
"'phoneNumber':'+919999999999'," + | ||
"'message':'hello'" + | ||
"}"; | ||
when(tokenValidator.validateToken("dummy")).thenReturn(Boolean.valueOf("true")); | ||
webClient.post() | ||
.uri("/notification/sms") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.bodyValue(requestBody) | ||
.header("Authorization","Bearer dummy") | ||
.exchange() | ||
.expectStatus() | ||
.isBadRequest(); | ||
} | ||
|
||
@Test | ||
public void shouldCallSend() { | ||
Object requestBody = "{" + | ||
"\"message\":\"hello\"," + | ||
"\"phoneNumber\":\"919999999999\"" + | ||
"}"; | ||
when(tokenValidator.validateToken("dummy")).thenReturn(Boolean.valueOf("true")); | ||
webClient.post() | ||
.uri("/notification/sms") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.bodyValue(requestBody) | ||
.header("Authorization","Bearer dummy") | ||
.exchange(); | ||
|
||
Mockito.verify(smsSender, times(1)).send("919999999999", "hello"); | ||
} | ||
|
||
@Test | ||
public void shouldThrowUnAuthorizedWhenAuthenticationFailed() { | ||
Object requestBody = "{" + | ||
"\"message\":\"hello\"," + | ||
"\"phoneNumber\":\"919999999999\"" + | ||
"}"; | ||
when(tokenValidator.validateToken("dummy")).thenReturn(Boolean.valueOf("false")); | ||
webClient.post() | ||
.uri("/notification/sms") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.bodyValue(requestBody) | ||
.header("Authorization","Bearer dummy") | ||
.exchange() | ||
.expectStatus() | ||
.isUnauthorized(); | ||
} | ||
} |