function processAuthZCodeCallback()

in Modules/callback-path.js [24:91]


function processAuthZCodeCallback(event, callback) {
    console.log("An Authorization Code has been sent back as Callback");
    
    //Search the DynamoDb table for Authorization Request with provided State
    var DynamoDBParams = {
        ExpressionAttributeValues: {
            ":authz_state": {
                S: event.queryStringParameters.state
            }
        },
        KeyConditionExpression: "AuthZ_State = :authz_state", 
        IndexName: process.env.DYNAMODB_AUTHZ_STATE_INDEX,
        TableName: process.env.DYNAMODB_TABLE
    };
    common.dynamodb.query(DynamoDBParams, function(err, data) {
        if (err) { 
            //There was an error retrieving the Authorization request
            console.log("Authorization State can't be retrieved: " +  event.queryStringParameters.state);
            console.log(err, err.stack);
            common.returnHTMLError(400, "<H1>Error, can't update status</H1>", callback);
        } else {
            console.log("Successful response");
            //If there is no result set
            if (data.Items.length == 0) {
                 console.log("No AuthZ State was returned");
                 common.returnHTMLError(400, "<H1>Error, can't update status</H1>", callback);
            //If Result Set is more than 1 entry
            } else if (data.Items.length > 1) {
                console.log("Too much AuthZ State were returned");
                common.returnHTMLError(400, "<H1>Error, can't update status</H1>", callback);
            } else {
                console.log("AuthZ State was returned");
                // Updating the Authorization request with the Code returned through the Authorization Code grant flow with PKCE callback
                DynamoDBParams = {
                    ExpressionAttributeNames: {
                        "#AuthZ_code": "AuthZ_code"
                    },
                    ExpressionAttributeValues: {
                        ":value": {
                            S: event.queryStringParameters.code
                        }
                    }, 
                    Key: {
                        "Device_code": {
                            S: data.Items[0].Device_code.S
                        }
                    },
                    ReturnValues: "ALL_NEW", 
                    TableName: process.env.DYNAMODB_TABLE,
                    UpdateExpression: "SET #AuthZ_code = :value"
                };
                common.dynamodb.updateItem(DynamoDBParams, function(err, data) {
                    if (err) {
                        //Update was not successful
                        console.log("Unable to set state to Authorization Code for Device Code");
                        console.log(err, err.stack);
                        common.returnHTMLError(400, "<H1>Error, can't update status</H1>", callback);
                    }
                    else {
                        //Update was successful
                        console.log("AuthZ Code updated");
                        common.returnHTMLSuccess("<H1>Thanks, Device has been Authorized. You can return to your device.</H1>", callback);
                    }
                });
            }
        }
    });
}