首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将Google OAuth2.0的登录电子邮件限制为特定域名

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

Stack Overflow用户
提问于 2012-06-02 08:16:06
回答 6查看 67.2K关注 0票数 93

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

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

根据记录,在用户尝试通过谷歌的OAuth身份验证登录之前,我不知道任何关于该用户的信息。所有我收到的是基本的用户信息和电子邮件。

EN

回答 6

Stack Overflow用户

发布于 2012-06-13 01:18:12

所以我给你一个答案。在oauth请求中,您可以添加"hd=domain.com“,它会将身份验证限制为来自该域的用户(我不知道您是否可以使用多个域)。您可以找到hd参数文档here

我在这里使用google api库:http://code.google.com/p/google-api-php-client/wiki/OAuth2,所以我必须手动编辑/auth/apiOAuth2.php文件如下:

代码语言:javascript
复制
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=domain.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/

票数 42
EN

Stack Overflow用户

发布于 2016-11-03 00:42:21

客户端:

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

服务器端:

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

完整堆栈示例:

Web代码:

代码语言:javascript
复制
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.com'
    });

    // 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

代码语言:javascript
复制
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.com';
            return tokenIsOK ? resolve() : reject();
        });
    });
};
票数 13
EN

Stack Overflow用户

发布于 2014-12-13 10:59:21

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

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

代码语言:javascript
复制
config.omniauth :google_oauth2, 'identifier', 'key', {hd: 'yourdomain.com'}
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10858813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档