使用Auth0通过单个应用程序登录对多个资源进行身份验证

时间:2017-11-08 15:20:43

标签: authentication auth0

我的应用使用Auth0进行身份验证。用户输入他们的用户名+密码,Auth0会返回一个我的应用用来呼叫后端的令牌。

我的应用程序也在幕后使用第三方应用程序,这些第三方应用程序都有自己的身份验证。当用户登录我的应用程序时,它需要外出并获取所有这些第三方应用程序的身份验证令牌。

例如,我的应用需要在Watson上使用API​​,该API使用不同的用户名+密码进行身份验证。用户无法知道用户名+密码是什么。相反,它们被我的应用程序“知道”,它使用它们登录Watson并获取身份验证令牌以进行API调用。

Watson API并不是我的应用需要使用的唯一第三方API。第三方API都有自己的身份验证方案。一些使用用户名+密码,另一些使用App ID和Secret等。但在所有这些情况下,身份验证凭据由我的应用程序拥有和管理 - 而不是由我的应用程序的用户拥有和管理。

所以问题是,当用户登录我的应用程序时,如何使用Auth0自动出去获取所有这些第三方认证令牌?

2 个答案:

答案 0 :(得分:1)

验证Auth0身份验证令牌后,在初始化事件中传递您的watson API,使用API​​配置文件模块获取您的watson API令牌。希望它会给出一些想法。

答案 1 :(得分:1)

当用户使用Auth0进行身份验证时,客户端将收到可与API服务器或其他第三方服务器一起使用的JSON Web令牌(JWT)。它还包含有关用户的信息,如UID或电子邮件。

请参阅https://auth0.com/learn/json-web-tokens/

为了使用Auth0自动获取所有第三方认证令牌,您只需使用规则获取这些令牌并将其与您的idToken合并即可。然后,客户端可以解码JWT并获取所需的令牌或秘密。

规则是在用户通过身份验证后执行的JavaScript代码。

以下是Auth0的示例代码

function (user, context, callback) {

// this is the private key you downloaded from your service account.
// make sure you remove the password from the key and convert it to PEM using the following
// openssl pkcs12 -in yourkey.p12 -out yourkey.pem -nocerts -nodes
// finally, you should put this as a configuration encrypted in Auth0
var KEY = '....RSA private key downloaded from service account...'; 

// this is the email address of the service account created (NOT the Client ID)
var GOOGLE_CLIENT_ID_EMAIL = '.....@developer.gserviceaccount.com';

// the scope you want access to. Full list of scopes https://developers.google.com/admin-sdk/directory/v1/guides/authorizing
var SCOPE = 'https://www.googleapis.com/auth/admin.directory.user.readonly';

// a user of your Google Apps domain that this rule would impersonate
var ADMIN_EMAIL = 'foo@corp.com';

var token = jwt.sign({ scope: SCOPE, sub: ADMIN_EMAIL }, KEY, { audience: "https://accounts.google.com/o/oauth2/token", issuer: GOOGLE_CLIENT_ID_EMAIL, expiresInMinutes: 60, algorithm: 'RS256'});

request.post({ url: 'https://accounts.google.com/o/oauth2/token', form: { grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', assertion: token } }, function(err, resp, body) {
    if (err) return callback(null, user, context);
    var result = JSON.parse(body);
    if (result.error) {
    console.log(body);
    // log and swallow
    return callback(null, user, context);
    }

    context.idToken['https://example.com/admin_access_token'] = result.access_token;
    callback(null, user, context);
});

}

用户登录后,此规则将从Google请求Access_token,然后将其放入响应idToken中。您可以将此类似模式用于其他API。

请参阅https://auth0.com/docs/rules

相关问题