Add API for retrieving auth requests by ID (#872)

This commit is contained in:
David Perez 2024-01-30 13:02:31 -06:00 committed by Álison Fernandes
parent 9300ba01f7
commit 4807005428
5 changed files with 94 additions and 54 deletions

View file

@ -26,4 +26,12 @@ interface AuthenticatedAuthRequestsApi {
*/
@GET("/auth-requests")
suspend fun getAuthRequests(): Result<AuthRequestsResponseJson>
/**
* Retrieves an existing authentication request by ID.
*/
@GET("/auth-requests/{requestId}")
suspend fun getAuthRequest(
@Path("requestId") requestId: String,
): Result<AuthRequestsResponseJson.AuthRequest>
}

View file

@ -11,6 +11,13 @@ interface AuthRequestsService {
*/
suspend fun getAuthRequests(): Result<AuthRequestsResponseJson>
/**
* Retrieves an existing auth request to see if a device has approved it.
*/
suspend fun getAuthRequest(
requestId: String,
): Result<AuthRequestsResponseJson.AuthRequest>
/**
* Updates an approval request.
*/

View file

@ -10,6 +10,11 @@ class AuthRequestsServiceImpl(
override suspend fun getAuthRequests(): Result<AuthRequestsResponseJson> =
authenticatedAuthRequestsApi.getAuthRequests()
override suspend fun getAuthRequest(
requestId: String,
): Result<AuthRequestsResponseJson.AuthRequest> =
authenticatedAuthRequestsApi.getAuthRequest(requestId = requestId)
override suspend fun updateAuthRequest(
requestId: String,
key: String,

View file

@ -65,32 +65,7 @@ class AuthRequestsServiceTest : BaseServiceTest() {
@Test
fun `updateAuthRequest when request response is Success should return Success`() = runTest {
val json = """
{
"id": "1",
"publicKey": "2",
"requestDeviceType": "Android",
"requestIpAddress": "1.0.0.1",
"key": "key",
"masterPasswordHash": "verySecureHash",
"creationDate": "2024-09-13T01:00:00.00Z",
"requestApproved": true,
"origin": "www.bitwarden.com"
}
"""
val expected = AuthRequestsResponseJson.AuthRequest(
id = "1",
publicKey = "2",
platform = "Android",
ipAddress = "1.0.0.1",
key = "key",
masterPasswordHash = "verySecureHash",
creationDate = ZonedDateTime.parse("2024-09-13T01:00:00.00Z"),
responseDate = null,
requestApproved = true,
originUrl = "www.bitwarden.com",
)
val response = MockResponse().setBody(json).setResponseCode(200)
val response = MockResponse().setBody(AUTH_REQUEST_RESPONSE_JSON).setResponseCode(200)
server.enqueue(response)
val actual = service.updateAuthRequest(
requestId = "userId",
@ -99,6 +74,49 @@ class AuthRequestsServiceTest : BaseServiceTest() {
masterPasswordHash = "verySecureHash",
isApproved = true,
)
assertEquals(Result.success(expected), actual)
assertEquals(Result.success(AUTH_REQUEST_RESPONSE), actual)
}
@Test
fun `getAuthRequest when request response is Failure should return Failure`() = runTest {
val response = MockResponse().setResponseCode(400)
server.enqueue(response)
val actual = service.getAuthRequest(requestId = "1")
assertTrue(actual.isFailure)
}
@Test
fun `getAuthRequest when request response is Success should return Success`() = runTest {
val response = MockResponse().setBody(AUTH_REQUEST_RESPONSE_JSON).setResponseCode(200)
server.enqueue(response)
val actual = service.getAuthRequest(requestId = "1")
assertEquals(Result.success(AUTH_REQUEST_RESPONSE), actual)
}
}
private const val AUTH_REQUEST_RESPONSE_JSON = """
{
"id": "1",
"publicKey": "2",
"requestDeviceType": "Android",
"requestIpAddress": "1.0.0.1",
"key": "key",
"masterPasswordHash": "verySecureHash",
"creationDate": "2024-09-13T01:00:00.00Z",
"requestApproved": true,
"origin": "www.bitwarden.com"
}
"""
private val AUTH_REQUEST_RESPONSE = AuthRequestsResponseJson.AuthRequest(
id = "1",
publicKey = "2",
platform = "Android",
ipAddress = "1.0.0.1",
key = "key",
masterPasswordHash = "verySecureHash",
creationDate = ZonedDateTime.parse("2024-09-13T01:00:00.00Z"),
responseDate = null,
requestApproved = true,
originUrl = "www.bitwarden.com",
)

View file

@ -34,32 +34,7 @@ class NewAuthRequestServiceTest : BaseServiceTest() {
@Test
fun `createAuthRequest when request response is Success should return Success`() = runTest {
val json = """
{
"id": "1",
"publicKey": "2",
"requestDeviceType": "Android",
"requestIpAddress": "1.0.0.1",
"key": "key",
"masterPasswordHash": "verySecureHash",
"creationDate": "2024-09-13T01:00:00.00Z",
"requestApproved": true,
"origin": "www.bitwarden.com"
}
"""
val expected = AuthRequestsResponseJson.AuthRequest(
id = "1",
publicKey = "2",
platform = "Android",
ipAddress = "1.0.0.1",
key = "key",
masterPasswordHash = "verySecureHash",
creationDate = ZonedDateTime.parse("2024-09-13T01:00:00.00Z"),
responseDate = null,
requestApproved = true,
originUrl = "www.bitwarden.com",
)
val response = MockResponse().setBody(json).setResponseCode(200)
val response = MockResponse().setBody(AUTH_REQUEST_RESPONSE_JSON).setResponseCode(200)
server.enqueue(response)
val actual = service.createAuthRequest(
email = "test@gmail.com",
@ -68,6 +43,33 @@ class NewAuthRequestServiceTest : BaseServiceTest() {
accessCode = "accessCode",
fingerprint = "fingerprint",
)
assertEquals(Result.success(expected), actual)
assertEquals(Result.success(AUTH_REQUEST_RESPONSE), actual)
}
}
private const val AUTH_REQUEST_RESPONSE_JSON = """
{
"id": "1",
"publicKey": "2",
"requestDeviceType": "Android",
"requestIpAddress": "1.0.0.1",
"key": "key",
"masterPasswordHash": "verySecureHash",
"creationDate": "2024-09-13T01:00:00.00Z",
"requestApproved": true,
"origin": "www.bitwarden.com"
}
"""
private val AUTH_REQUEST_RESPONSE = AuthRequestsResponseJson.AuthRequest(
id = "1",
publicKey = "2",
platform = "Android",
ipAddress = "1.0.0.1",
key = "key",
masterPasswordHash = "verySecureHash",
creationDate = ZonedDateTime.parse("2024-09-13T01:00:00.00Z"),
responseDate = null,
requestApproved = true,
originUrl = "www.bitwarden.com",
)