ChatGPT解决这个技术问题 Extra ChatGPT

将使用 Google OAuth2.0 的登录电子邮件限制为特定域名

我似乎找不到任何有关如何限制登录到我的 Web 应用程序(使用 OAuth2.0 和 Google API)的文档,以仅接受来自具有特定域名或一组域名的电子邮件的用户的身份验证请求。我想加入白名单而不是黑名单。

有没有人有关于如何做到这一点的建议,关于官方接受的这样做方法的文档,或者一个简单、安全的解决方法?

作为记录,在他们尝试通过 Google 的 OAuth 身份验证登录之前,我不知道有关用户的任何信息。我收到的只是基本的用户信息和电子邮件。

我也在研究这个。我有一个应用程序,我希望只有在我们的 Google Apps for Business 域上拥有帐户的人才能访问它。谷歌 OpenID 实现可能更适合我们俩......
如何使用 google sdk 和 c# 实现域用户登录?
请有人看看这个问题stackoverflow.com/questions/34220051/…
请我在这个问题上有一个赏金所以有人可以帮助我

S
Stephen Ostermiller

所以我有一个答案给你。在 OAuth 请求中,您可以添加 hd=example.com,它将限制对该域的用户进行身份验证(我不知道您是否可以执行多个域)。您可以找到记录的 hd 参数 here

我正在使用此处的 Google API 库:http://code.google.com/p/google-api-php-client/wiki/OAuth2,因此我必须手动将 /auth/apiOAuth2.php 文件编辑为:

public function createAuthUrl($scope) {
    $params = array(
        'response_type=code',
        'redirect_uri=' . urlencode($this->redirectUri),
        'client_id=' . urlencode($this->clientId),
        'scope=' . urlencode($scope),
        'access_type=' . urlencode($this->accessType),
        'approval_prompt=' . urlencode($this->approvalPrompt),
        'hd=example.com'
    );

    if (isset($this->state)) {
        $params[] = 'state=' . urlencode($this->state);
    }
    $params = implode('&', $params);
    return self::OAUTH2_AUTH_URL . "?$params";
}

我仍在开发这个应用程序并发现了这个,这可能是这个问题的更正确答案。 https://developers.google.com/google-apps/profiles/


我不知道这个参数,你能链接到你发现它的地方吗?
不幸的是,我不得不从我的一位同事那里获取信息,我在谷歌文档中的任何地方都没有找到。我的同事认为他在 OpenID 规范中找到了参考,并在 OpenAuth 规范中进行了尝试,它似乎有效。我想请谨慎使用,因为它似乎是未记录的功能。
重要提示:即使您在 createAuthUrl 函数中指定了 hd 参数,您仍需要验证用户是否使用您的域电子邮件地址登录。更改链接参数以允许所有电子邮件地址并随后访问您的应用程序非常容易。
有关 hd 参数用法的 Google 文档,请参阅 developers.google.com/identity/work/it-apps 并且可以找到 hd URI 参数的参考 developers.google.com/identity/protocols/… 在概要中,hd 参数应被视为基于域的显示过滤器Google Auth 方面,但仍应在您方面进行验证。
太好了,目前,在hd参数中,我只能限制一个域,现在如果我想限制两个或三个域怎么办?
S
Stephen Ostermiller

客户端:

使用 auth2 初始化函数,您可以传递 hosted_domain 参数以将登录弹出窗口中列出的帐户限制为与您的 hosted_domain 匹配的帐户。您可以在此处的文档中看到这一点:https://developers.google.com/identity/sign-in/web/reference

服务器端:

即使使用受限的客户端列表,您也需要验证 id_token 是否与您指定的托管域匹配。对于某些实施,这意味着在验证令牌后检查您从 Google 收到的 hd 属性。

全栈示例:

网页代码:

gapi.load('auth2', function () {
    // init auth2 with your hosted_domain
    // only matching accounts will show up in the list or be accepted
    var auth2 = gapi.auth2.init({
        client_id: "your-client-id.apps.googleusercontent.com",
        hosted_domain: 'your-special-domain.example'
    });

    // setup your signin button
    auth2.attachClickHandler(yourButtonElement, {});

    // when the current user changes
    auth2.currentUser.listen(function (user) {
        // if the user is signed in
        if (user && user.isSignedIn()) {
            // validate the token on your server,
            // your server will need to double check that the
            // `hd` matches your specified `hosted_domain`;
            validateTokenOnYourServer(user.getAuthResponse().id_token)
                .then(function () {
                    console.log('yay');
                })
                .catch(function (err) {
                    auth2.then(function() { auth2.signOut(); });
                });
        }
    });
});

服务器代码(使用谷歌 Node.js 库):

如果您不使用 Node.js,可以在此处查看其他示例:https://developers.google.com/identity/sign-in/web/backend-auth

const GoogleAuth = require('google-auth-library');
const Auth = new GoogleAuth();
const authData = JSON.parse(fs.readFileSync(your_auth_creds_json_file));
const oauth = new Auth.OAuth2(authData.web.client_id, authData.web.client_secret);

const acceptableISSs = new Set(
    ['accounts.google.com', 'https://accounts.google.com']
);

const validateToken = (token) => {
    return new Promise((resolve, reject) => {
        if (!token) {
            reject();
        }
        oauth.verifyIdToken(token, null, (err, ticket) => {
            if (err) {
                return reject(err);
            }
            const payload = ticket.getPayload();
            const tokenIsOK = payload &&
                  payload.aud === authData.web.client_id &&
                  new Date(payload.exp * 1000) > new Date() &&
                  acceptableISSs.has(payload.iss) &&
                  payload.hd === 'your-special-domain.example';
            return tokenIsOK ? resolve() : reject();
        });
    });
};

K
Kosmonaut

在定义您的提供者时,请在末尾使用 'hd' 参数传入一个哈希值。你可以在这里阅读。 https://developers.google.com/accounts/docs/OpenIDConnect#hd-param

例如,对于 config/initializers/devise.rb

config.omniauth :google_oauth2, 'identifier', 'key', {hd: 'yourdomain.com'}

这很容易被规避,允许访问其他域的登录。它仅适用于限制向用户显示的可用帐户。
m
mjoyce91

这是我在 node.js 中使用护照所做的。 profile 是尝试登录的用户。

//passed, stringified email login
var emailString = String(profile.emails[0].value);
//the domain you want to whitelist
var yourDomain = '@google.com';
//check the x amount of characters including and after @ symbol of passed user login.
//This means '@google.com' must be the final set of characters in the attempted login 
var domain = emailString.substr(emailString.length - yourDomain.length);

//I send the user back to the login screen if domain does not match 
if (domain != yourDomain)
   return done(err);

然后只需创建逻辑来查找多个域而不仅仅是一个域。我相信这种方法是安全的,因为 1. '@' 符号在电子邮件地址的第一部分或第二部分中不是有效字符。我无法通过创建像 mike@fake@google.com 2 这样的电子邮件地址来欺骗该功能。在传统的登录系统中我可以,但这个电子邮件地址永远不会存在于 Google 中。如果不是有效的 Google 帐户,则无法登录。


J
JBithell

自 2015 年以来,有一个 function in the library 来设置它,而无需像 aaron-bruce 在 workaround 中那样编辑库的源代码

在生成 URL 之前,只需针对您的 Google 客户端调用 setHostedDomain

$client->setHostedDomain("HOSTED DOMAIN")

D
DEV Tiago França

使用 Laravel Socialite 登录 Google https://laravel.com/docs/8.x/socialite#optional-parameters

use Laravel\Socialite\Facades\Socialite;

return Socialite::driver('google')
    ->with(['hd' => 'pontomais.com.br'])
    ->redirect();

关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅