Skip to content

Commit

Permalink
Delegate validateChallenge logic to api (#17)
Browse files Browse the repository at this point in the history
* Delegate isValid logic to API

* Bump version
  • Loading branch information
stevenclouston committed May 2, 2024
1 parent e20531c commit 7241f32
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 44 deletions.
44 changes: 9 additions & 35 deletions lib/Authsignal/Authsignal.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

abstract class Authsignal
{
const VERSION = '1.0.3';
const VERSION = '2.0.0';

public static $apiKey;

Expand Down Expand Up @@ -142,41 +142,15 @@ public static function enrollVerifiedAuthenticator(string $userId, Array $authen
*/
public static function validateChallenge(string $token, ?string $userId = null)
{
$key = self::getApiKey();
$decoded = (array)JWT::decode($token, new Key($key, 'HS256'));
$otherClaim = (array)$decoded['other'];

$decodedUserId = $otherClaim["userId"];
$decodedActionCode = $otherClaim["actionCode"];
$decodedIdempotencyKey= $otherClaim["idempotencyKey"];

if ($userId && ($userId != $decodedUserId))
{
return [
"userId" => $decodedUserId,
"success" => false,
"state" => null
];
}

if($decodedActionCode && $decodedIdempotencyKey){
$action = self::getAction($decodedUserId, $decodedActionCode, $decodedIdempotencyKey);

if($action){
$success = $action["state"] === "CHALLENGE_SUCCEEDED";
return [
"userId" => $decodedUserId,
"success" => $success,
"state" => $action["state"]
];
}
}
$request = new AuthsignalClient();

return [
"userId" => $decodedUserId,
"success" => false,
"state" => null
$payload = [
'userId' => $userId,
'token' => $token
];
}

list($response, $request) = $request->send("/validate", $payload, 'post');

return $response;
}
}
21 changes: 12 additions & 9 deletions test/AuthsignalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,12 @@ public function testValidateChallenge() {
$mockedResponse = array("state" => "CHALLENGE_SUCCEEDED",
"idempotencyKey" => "5924a649-b5d3-4baf-a4ab-4b812dde97a0",
"stateUpdatedAt" => "2022-07-25T03:19:00.316Z",
"createdAt" => "2022-07-25T03:19:00.316Z",
"ruleIds" => []);
"userId" => "123:test",
"isValid" => "true",
"actionCode" => "signIn",
"verificationMethod" => "AUTHENTICATOR_APP");

self::$server->setResponseOfPath("/v1/users/123%3Atest/actions/signIn/5924a649-b5d3-4baf-a4ab-4b812dde97a0", new Response(json_encode($mockedResponse)));
self::$server->setResponseOfPath("/v1/validate", new Response(json_encode($mockedResponse)));

$key = "secret";
$testTokenPayload = [
Expand All @@ -133,17 +135,19 @@ public function testValidateChallenge() {

$response = Authsignal::validateChallenge(userId: "123:test", token: $token);

$this->assertEquals($response["success"], true);
$this->assertEquals($response['isValid'], "true");
}

public function testValidateChallengeOptionalUserId() {
$mockedResponse = array("state" => "CHALLENGE_SUCCEEDED",
"idempotencyKey" => "5924a649-b5d3-4baf-a4ab-4b812dde97a0",
"stateUpdatedAt" => "2022-07-25T03:19:00.316Z",
"createdAt" => "2022-07-25T03:19:00.316Z",
"ruleIds" => []);
"userId" => null,
"isValid" => "true",
"actionCode" => "signIn",
"verificationMethod" => "AUTHENTICATOR_APP");

self::$server->setResponseOfPath("/v1/users/123%3Atest/actions/signIn/5924a649-b5d3-4baf-a4ab-4b812dde97a0", new Response(json_encode($mockedResponse)));
self::$server->setResponseOfPath("/v1/validate", new Response(json_encode($mockedResponse)));

$key = "secret";
$testTokenPayload = [
Expand All @@ -152,7 +156,6 @@ public function testValidateChallengeOptionalUserId() {
'iat' => 1356999524,
'nbf' => 1357000000,
'other' => [
'userId' => "123:test",
'state' => "CHALLENGE_SUCCEEDED",
'actionCode' => 'signIn',
'idempotencyKey' => "5924a649-b5d3-4baf-a4ab-4b812dde97a0",
Expand All @@ -162,6 +165,6 @@ public function testValidateChallengeOptionalUserId() {

$response = Authsignal::validateChallenge(token: $token);

$this->assertEquals($response["success"], true);
$this->assertEquals($response["isValid"], "true");
}
}

0 comments on commit 7241f32

Please sign in to comment.