generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PI-1493 Add endpoints for HMPPS Auth integration
- Loading branch information
1 parent
3731feb
commit b6d39c2
Showing
19 changed files
with
413 additions
and
61 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
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
8 changes: 4 additions & 4 deletions
8
projects/hmpps-auth-and-delius/src/dev/resources/local-public-key.pub
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,6 +1,6 @@ | ||
-----BEGIN PUBLIC KEY----- | ||
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDo3hw1/oChbttEOxEH4NUDrH+Y | ||
n2x0DavAmDjMbhcSiQ6+/t8Nz/N03BauWzFOGBtftnQrHfnF+O7RAKj8zMjcbIq4 | ||
QrYeXEpnaFCGEwTtOBpxvSEWPrLEpr1gCarBQZDp67ag+SYqrDgkn2Vme/dMvMUQ | ||
xUO3DT6jg9921J6TlwIDAQAB | ||
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQChGeUcaJnFldtX2x3eqCZVXvXC | ||
eXVzoka/DR1D52HvhYdmDc/gx1kyg+S+A1ppdEw1K3qGrUUu2VCgI9aQogZE61NY | ||
CBFblMsHfgQyB076ghz/KdzVF+y5KVu61/alFzDkZ7q/z+k3Mn2xaWrcHP/MPReu | ||
7r9FF41O5uPWza/7aQIDAQAB | ||
-----END PUBLIC KEY----- |
33 changes: 33 additions & 0 deletions
33
projects/hmpps-auth-and-delius/src/dev/resources/schema.ldif
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 @@ | ||
dn: dc=moj,dc=com | ||
dc: moj | ||
objectclass: top | ||
objectclass: domain | ||
|
||
dn: ou=Users,dc=moj,dc=com | ||
objectclass: top | ||
objectclass: organizationalUnit | ||
ou: Users | ||
|
||
dn: cn=test.user,ou=Users,dc=moj,dc=com | ||
cn: test.user | ||
objectclass: NDUser | ||
objectclass: inetOrgPerson | ||
objectclass: top | ||
givenName: Test | ||
sn: User | ||
userHomeArea: N01 | ||
userSector: public | ||
mail: [email protected] | ||
userPassword: secret | ||
|
||
dn: cn=ABC001,cn=test.user,ou=Users,dc=moj,dc=com | ||
cn: ABC001 | ||
description: Test role 1 | ||
objectclass: top | ||
objectClass: NDRoleAssociation | ||
|
||
dn: cn=ABC002,cn=test.user,ou=Users,dc=moj,dc=com | ||
cn: ABC002 | ||
description: Test role 2 | ||
objectclass: top | ||
objectClass: NDRoleAssociation |
2 changes: 1 addition & 1 deletion
2
...ts/hmpps-auth-and-delius/src/dev/resources/simulations/__files/hmpps-auth-token-body.json
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
83 changes: 83 additions & 0 deletions
83
.../src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/AuthenticationIntegrationTest.kt
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,83 @@ | ||
package uk.gov.justice.digital.hmpps | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.equalTo | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT | ||
import org.springframework.http.MediaType | ||
import org.springframework.ldap.core.AttributesMapper | ||
import org.springframework.ldap.core.LdapTemplate | ||
import org.springframework.test.annotation.DirtiesContext | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status | ||
import uk.gov.justice.digital.hmpps.security.withOAuth2Token | ||
|
||
@AutoConfigureMockMvc | ||
@SpringBootTest(webEnvironment = RANDOM_PORT) | ||
internal class AuthenticationIntegrationTest { | ||
@Autowired | ||
lateinit var mockMvc: MockMvc | ||
|
||
@Autowired | ||
lateinit var wireMockServer: WireMockServer | ||
|
||
@Autowired | ||
lateinit var ldapTemplate: LdapTemplate | ||
|
||
@Test | ||
fun `successful authentication`() { | ||
mockMvc.perform(authenticate("""{"username": "test.user", "password": "secret"}""")) | ||
.andExpect(status().isOk) | ||
} | ||
|
||
@Test | ||
fun `failed authentication`() { | ||
mockMvc.perform(authenticate("""{"username": "test.user", "password": "incorrect"}""")) | ||
.andExpect(status().isUnauthorized) | ||
} | ||
|
||
@Test | ||
fun `invalid authentication request`() { | ||
mockMvc.perform(authenticate("""{"username": "", "password": " "}""")) | ||
.andExpect(status().isBadRequest) | ||
} | ||
|
||
@Test | ||
@DirtiesContext | ||
fun `successful password change`() { | ||
assertThat(currentPassword(), equalTo("secret")) | ||
mockMvc.perform(changePassword("test.user", """{"password": "new"}""")) | ||
.andExpect(status().isOk) | ||
assertThat(currentPassword(), equalTo("new")) | ||
} | ||
|
||
@Test | ||
fun `attempt to change password for non-existent user`() { | ||
mockMvc.perform(changePassword("some user", """{"password": "new"}""")) | ||
.andExpect(status().isNotFound) | ||
} | ||
|
||
@Test | ||
fun `invalid password change request`() { | ||
mockMvc.perform(changePassword("some user", """{}""")) | ||
.andExpect(status().isBadRequest) | ||
} | ||
|
||
private fun authenticate(json: String) = post("/authenticate").withOAuth2Token(wireMockServer) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(json) | ||
|
||
private fun changePassword(username: String, json: String) = | ||
post("/user/$username/password").withOAuth2Token(wireMockServer) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(json) | ||
|
||
private fun currentPassword() = | ||
ldapTemplate.search("ou=Users", "cn=test.user", AttributesMapper { String(it["userPassword"].get() as ByteArray) }) | ||
.toList().single() | ||
} |
31 changes: 0 additions & 31 deletions
31
...uth-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
...ius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/UserDetailsIntegrationTest.kt
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,71 @@ | ||
package uk.gov.justice.digital.hmpps | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.github.tomakehurst.wiremock.WireMockServer | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.ResultActions | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status | ||
import uk.gov.justice.digital.hmpps.model.UserDetails | ||
import uk.gov.justice.digital.hmpps.security.withOAuth2Token | ||
|
||
@AutoConfigureMockMvc | ||
@SpringBootTest(webEnvironment = RANDOM_PORT) | ||
internal class UserDetailsIntegrationTest { | ||
@Autowired | ||
lateinit var mockMvc: MockMvc | ||
|
||
@Autowired | ||
lateinit var wireMockServer: WireMockServer | ||
|
||
@Autowired | ||
lateinit var objectMapper: ObjectMapper | ||
|
||
@Test | ||
fun `missing user returns 404`() { | ||
mockMvc.perform(get("/user/does.not.exist").withOAuth2Token(wireMockServer)) | ||
.andExpect(status().isNotFound) | ||
} | ||
|
||
@Test | ||
fun `get user`() { | ||
mockMvc.perform(get("/user/test.user").withOAuth2Token(wireMockServer)) | ||
.andExpect(status().isOk) | ||
.andExpectJson( | ||
UserDetails( | ||
username = "test.user", | ||
firstName = "Test", | ||
surname = "User", | ||
email = "[email protected]", | ||
enabled = true, | ||
roles = listOf("ABC001", "ABC002") | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun `search by email`() { | ||
mockMvc.perform(get("/[email protected]").withOAuth2Token(wireMockServer)) | ||
.andExpect(status().isOk) | ||
.andExpectJson( | ||
listOf( | ||
UserDetails( | ||
username = "test.user", | ||
firstName = "Test", | ||
surname = "User", | ||
email = "[email protected]", | ||
enabled = true, | ||
roles = listOf("ABC001", "ABC002") | ||
) | ||
) | ||
) | ||
} | ||
|
||
fun <T> ResultActions.andExpectJson(obj: T) = this.andExpect(content().json(objectMapper.writeValueAsString(obj))) | ||
} |
3 changes: 2 additions & 1 deletion
3
projects/hmpps-auth-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/App.kt
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
17 changes: 0 additions & 17 deletions
17
...-auth-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/ApiController.kt
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
...elius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/AuthenticationController.kt
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,39 @@ | ||
package uk.gov.justice.digital.hmpps.controller | ||
|
||
import io.swagger.v3.oas.annotations.Operation | ||
import io.swagger.v3.oas.annotations.tags.Tag | ||
import jakarta.validation.Valid | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.ldap.core.LdapTemplate | ||
import org.springframework.ldap.query.LdapQueryBuilder.query | ||
import org.springframework.security.access.prepost.PreAuthorize | ||
import org.springframework.validation.annotation.Validated | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RestController | ||
import uk.gov.justice.digital.hmpps.ldap.byUsername | ||
import uk.gov.justice.digital.hmpps.model.AuthenticationRequest | ||
|
||
@Validated | ||
@RestController | ||
@Tag(name = "Authentication") | ||
class AuthenticationController(private val ldapTemplate: LdapTemplate) { | ||
companion object { | ||
private val log = LoggerFactory.getLogger(this::class.java) | ||
} | ||
|
||
@PostMapping("/authenticate") | ||
@PreAuthorize("hasRole('ROLE_DELIUS_USER_AUTH')") | ||
@Operation(description = "Authenticate a Delius username and password. Requires ROLE_DELIUS_USER_AUTH.") | ||
fun authenticate( | ||
@Valid @RequestBody | ||
request: AuthenticationRequest | ||
) = try { | ||
ldapTemplate.authenticate(query().byUsername(request.username), request.password) | ||
ResponseEntity.ok().build() | ||
} catch (e: Exception) { | ||
log.error("Authentication failed for '${request.username}'", e) | ||
ResponseEntity.status(401).body("Authentication failed for '${request.username}'") | ||
} | ||
} |
Oops, something went wrong.