Skip to content

Commit

Permalink
Merge pull request #29 from hmcts/EM-1875-openId-connect-updates-2
Browse files Browse the repository at this point in the history
Updated the details for token endpoint
  • Loading branch information
yogesh-hullatti authored Mar 6, 2020
2 parents 0750bf0 + 3b1e71d commit fb9b47a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class IdamScenario {
@Autowired
IdamHelper idamHelper;

@Test(expected = FeignException.Unauthorized.class)
@Test(expected = FeignException.BadRequest.class)
public void testCreationAndDeletion() {
idamHelper.createUser("a@b.com", Stream.of("caseworker").collect(Collectors.toList()));
assertThat(idamHelper.authenticateUser("a@b.com")).isNotEmpty();
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/uk/gov/hmcts/reform/em/test/idam/IdamHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import uk.gov.hmcts.reform.idam.client.models.test.CreateUserRequest;
import uk.gov.hmcts.reform.idam.client.models.test.UserRole;

import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -70,11 +69,17 @@ public String authenticateUser(String username) {
}

private String authenticateOpenIdUser(String username, String password) {
String authorisation = username + ":" + password;
String base64Authorisation = Base64.getEncoder().encodeToString(authorisation.getBytes());
OpenIdAuthUserResponse openIdAuthUserResponse = openIdUserApi.authenticateUser("Basic " + base64Authorisation,
new OpenIdAuthUserRequest(openIdConfiguration.getGrantType(), openIdConfiguration.getClientId(),
openIdConfiguration.getRedirectUri(), openIdConfiguration.getScope()));

OpenIdAuthUserRequest openIdAuthUserRequest = OpenIdAuthUserRequest.builder()
.client_id(openIdConfiguration.getClientId())
.client_secret(openIdConfiguration.getClient_secret())
.grant_type(openIdConfiguration.getGrantType())
.redirect_uri(openIdConfiguration.getRedirectUri())
.scope(openIdConfiguration.getScope())
.username(username)
.password(password)
.build();
OpenIdAuthUserResponse openIdAuthUserResponse = openIdUserApi.authenticateUser(openIdAuthUserRequest);
return "Bearer " + openIdAuthUserResponse.getAccessToken();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
public class OpenIdConfiguration {

private String clientId;
private String client_secret;
private String redirectUri;
private String grant_type;
private String scope;

public OpenIdConfiguration(@Value("${idam.client.id:}") String clientId,
@Value("${idam.client.secret:}") String client_secret,
@Value("${idam.client.redirect_uri:}") String redirectUri,
@Value("${idam.client.scope:}") String scope,
@Value("${idam.client.grant_type:}") String grant_type) {
this.clientId = clientId;
this.client_secret = client_secret;
this.redirectUri = redirectUri;
this.grant_type = grant_type;
this.scope = scope;
Expand All @@ -25,6 +28,10 @@ public String getClientId() {
return clientId;
}

public String getClient_secret() {
return client_secret;
}

public String getRedirectUri() {
return redirectUri;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import uk.gov.hmcts.reform.em.test.idam.client.models.OpenIdAuthUserRequest;
import uk.gov.hmcts.reform.em.test.idam.client.models.OpenIdAuthUserResponse;
import uk.gov.hmcts.reform.idam.client.CoreFeignConfiguration;
Expand All @@ -19,7 +18,6 @@ public interface OpenIdUserApi {
value = {"/o/token"},
consumes = {"application/x-www-form-urlencoded"}
)
OpenIdAuthUserResponse authenticateUser(@RequestHeader("Authorization") String authorisation,
@RequestBody OpenIdAuthUserRequest openIdAuthUserRequest);
OpenIdAuthUserResponse authenticateUser(@RequestBody OpenIdAuthUserRequest openIdAuthUserRequest);

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package uk.gov.hmcts.reform.em.test.idam.client.models;

import lombok.Builder;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode
@Builder
public class OpenIdAuthUserRequest {

private String grant_type;
private String client_id;
private String client_secret;
private String redirect_uri;
private String scope;
private String username;
private String password;

public OpenIdAuthUserRequest(String grant_type, String client_id, String redirect_uri,
String scope) {
public OpenIdAuthUserRequest(String grant_type, String client_id, String client_secret,
String redirect_uri, String scope,
String username, String password) {
this.grant_type = grant_type;
this.client_id = client_id;
this.client_secret = client_secret;
this.redirect_uri = redirect_uri;
this.scope = scope;
this.username = username;
this.password = password;
}

public String getGrantType() {
Expand All @@ -30,4 +42,16 @@ public String getRedirectUri() {
public String getScope() {
return scope;
}

public String getClient_secret() {
return client_secret;
}

public String getUsername() {
return username;
}

public String getPassword() {
return password;
}
}
18 changes: 8 additions & 10 deletions src/test/java/uk/gov/hmcts/reform/em/test/IdamHelperTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package uk.gov.hmcts.reform.em.test;


import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.anyString;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.mockito.junit.MockitoJUnitRunner;
import uk.gov.hmcts.reform.em.test.idam.DeleteUserApi;
import uk.gov.hmcts.reform.em.test.idam.IdamHelper;
Expand Down Expand Up @@ -66,7 +64,7 @@ public void testGetUserId() {
UserDetails userDetailsMock = mock(UserDetails.class);
when(userDetailsMock.getId()).thenReturn("id");
when(openIdAuthUserResponse.getAccessToken()).thenReturn("b");
when(openIdUserApi.authenticateUser(anyString(),any())).thenReturn(openIdAuthUserResponse);
when(openIdUserApi.authenticateUser(any())).thenReturn(openIdAuthUserResponse);
when(idamClient.getUserDetails("Bearer b")).thenReturn(userDetailsMock);
assertThat(idamHelper.getUserId("x")).isEqualTo("id");
}
Expand All @@ -80,11 +78,11 @@ public void testDeleteUser() {
@Test
public void testAuthenticateUser() {
when(openIdAuthUserResponse.getAccessToken()).thenReturn("b");
when(openIdUserApi.authenticateUser(anyString(),any())).thenReturn(openIdAuthUserResponse);
when(openIdUserApi.authenticateUser(any())).thenReturn(openIdAuthUserResponse);
assertThat(idamHelper.authenticateUser("x")).isEqualTo("Bearer b");
assertThat(idamHelper.authenticateUser("x")).isEqualTo("Bearer b");
assertThat(idamHelper.authenticateUser("x")).isEqualTo("Bearer b");
verify(openIdUserApi, times(1)).authenticateUser(anyString(),any());
verify(openIdUserApi, times(1)).authenticateUser(any());
}


Expand Down

0 comments on commit fb9b47a

Please sign in to comment.