Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash in CognitoTokenGuard.php:147 #114

Open
SebastienKPA6T opened this issue Jan 9, 2025 · 2 comments
Open

Crash in CognitoTokenGuard.php:147 #114

SebastienKPA6T opened this issue Jan 9, 2025 · 2 comments

Comments

@SebastienKPA6T
Copy link

Hi,

I am trying to integrate the package in Laravel 9 with API guard.
After registering new user, the status of new user in Cognito is "Force change password"
When I am performing the Login with API Guard, a crash occurs in CognitoTokenGuard.php at line 147.
After debugging, following variables have values:

  • $this->challengeName: "NEW_PASSWORD_REQUIRED"
  • $this->challengeData: It contains a simple String with value "NEW_PASSWORD_REQUIRED"
    The error is
  • Cannot access offset of type string on string on this line: $key = $this->challengeData['session_token'];
    There is no 'session_token' in the array.

I think I am doing something wrong but can you help me please?

Best regards
Sebastien

@gioppy
Copy link

gioppy commented Jan 10, 2025

Same here! The problem is on this lines

$key = $this->challengeData['session_token'];

//Save the challenge data
$this->setChallengeData($key);

$returnValue = $this->challengeData;

for some reasons, this->challengeData is not an array but contains the challenge name NEW_PASSWORD_REQUIRED.

@Sebteams69
Copy link

To solve the problem, I override the AwsCognitoServiceProvider and create an overriden class of CognitoTokenGuard
In this guard class I override tje attempt method to handle the challenge response
I use my own processCognitoChallenge

    private function processCognitoChallenge(Collection $result, string $username)
    {
        $result = $result->toArray();
        //Return value
        $returnValue = null;

        switch ($result['ChallengeName']) {
            case 'SOFTWARE_TOKEN_MFA':
                $returnValue = [
                    'status' => $result['ChallengeName'],
                    'session_token' => $result['Session'],
                    'username' => $username
                ];
                break;

            case 'SMS_MFA':
            case 'SELECT_MFA_TYPE':
                $returnValue = [
                    'status' => $result['ChallengeName'],
                    'session_token' => $result['Session'],
                    'challenge_params' => $result['ChallengeParameters'],
                    'username' => $username
                ];
                break;

            default:
                if (in_array($result['ChallengeName'], config('cognito.forced_challenge_names'))) {
                    $returnValue =
                        [
                            'status' => $result['ChallengeName'],
                            'challenge_params' => isset($result['ChallengeParameters']) ? $result['ChallengeParameters'] : null,
                            'username' => $username
                        ];
                } //End if
                break;
        } //End switch

        return $returnValue;
    } //Function ends

Then I handle the response in my Controller

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants