AWS API网关身份验证而无需使用Cognito



i当前在我们拥有自定义身份验证/授权系统的系统上工作。

我们正在迁移到AWS,Web API和Lambda ...我想重用我们目前的auth/授权。

我正在考虑具有登录终点的身份验证/授权lambda,一个用于特定用户的角色。

Web API将这两种路由路由进行身份验证/授权lambda,其余的lambda将其路由到其他lambdas。

我还需要使用JWT进行跟随到网关的呼叫。令牌生成/使用将在我的上述方案中发生在哪里?

是否有更好的方法来利用我们的自定义用户/角色方案在此新的AWS网关/lambda Universe中?

Reza Nasiri的评论中提到的,自定义/lambda授权器似乎是您的最佳选择。您可以在Lambda授权器功能中编写自定义验证逻辑,Lambda授权器可以根据您的要求验证JWT令牌。当您使用Lambda授权器时,您可以拥有完整的代码自由,并且可以实现任何形式的逻辑,因此可以利用旧身份验证引擎的令牌/字符串。

根据官方文档的示例代码段,用于官方文档的lambda授权程序如下:

// A simple token-based authorizer example to demonstrate how to use an authorization token 
// to allow or deny a request. In this example, the caller named 'user' is allowed to invoke 
// a request if the client-supplied token value is 'allow'. The caller is not allowed to invoke 
// the request if the token value is 'deny'. If the token value is 'unauthorized' or an empty
// string, the authorizer function returns an HTTP 401 status code. For any other token value, 
// the authorizer returns an HTTP 500 status code. 
// Note that token values are case-sensitive.
exports.handler =  function(event, context, callback) {
    var token = event.authorizationToken;
    switch (token) {
        case 'allow':
            callback(null, generatePolicy('user', 'Allow', event.methodArn));
            break;
        case 'deny':
            callback(null, generatePolicy('user', 'Deny', event.methodArn));
            break;
        case 'unauthorized':
            callback("Unauthorized");   // Return a 401 Unauthorized response
            break;
        default:
            callback("Error: Invalid token"); // Return a 500 Invalid token response
    }
};
// Help function to generate an IAM policy
var generatePolicy = function(principalId, effect, resource) {
    var authResponse = {};
    authResponse.principalId = principalId;
    if (effect && resource) {
        var policyDocument = {};
        policyDocument.Version = '2012-10-17'; 
        policyDocument.Statement = [];
        var statementOne = {};
        statementOne.Action = 'execute-api:Invoke'; 
        statementOne.Effect = effect;
        statementOne.Resource = resource;
        policyDocument.Statement[0] = statementOne;
        authResponse.policyDocument = policyDocument;
    }
    // Optional output with custom properties of the String, Number or Boolean type.
    authResponse.context = {
        "stringKey": "stringval",
        "numberKey": 123,
        "booleanKey": true
    };
    return authResponse;
}

根据发生的验证结果,Lambda授权者生成了IAM策略。因此,您可以根据用例生成自定义策略。

最新更新