94

Web アプリケーション (OAuth2.0 と Google API を使用) へのログインを制限して、特定のドメイン名または一連のドメイン名の電子メールを使用するユーザーからの認証要求のみを受け入れる方法に関するドキュメントが見つからないようです。ブラックリストではなくホワイトリストに登録したいと思います。

これを行う方法、公式に認められた方法に関するドキュメント、または簡単で安全な回避策について誰か提案がありますか?

記録として、ユーザーが Google の OAuth 認証を介してログインを試みるまで、ユーザーに関する情報はわかりません。私が受け取るのは、基本的なユーザー情報と電子メールだけです。

4

6 に答える 6

43

だから私はあなたに答えがあります。oauth リクエストで「hd=domain.com」を追加すると、認証がそのドメインのユーザーに制限されます (複数のドメインを実行できるかどうかはわかりません)。ここに文書化された hd パラメータを見つけることができます

私はここから 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=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/

于 2012-06-12T17:18:12.167 に答える
13

クライアント側:

auth2init 関数を使用すると、hosted_domainパラメーターを渡して、サインイン ポップアップに表示されるアカウントを、hosted_domain. これは、 https ://developers.google.com/identity/sign-in/web/reference のドキュメントで確認できます。

サーバ側:

id_tokenクライアント側のリストが制限されている場合でも、が指定したホスト ドメインと一致することを確認する必要があります。一部の実装では、これはhd、トークンを確認した後に Google から受け取る属性を確認することを意味します。

フルスタックの例:

ウェブコード:

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(); });
                });
        }
    });
});

サーバー コード (googles 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.com';
            return tokenIsOK ? resolve() : reject();
        });
    });
};
于 2016-11-02T16:42:20.687 に答える
9

プロバイダーを定義するときは、最後に「hd」パラメーターを使用してハッシュを渡します。ここでそれについて読むことができます。https://developers.google.com/accounts/docs/OpenIDConnect#hd-param

たとえば、config/initializers/devise.rb の場合

config.omniauth :google_oauth2, 'identifier', 'key', {hd: 'yourdomain.com'}
于 2014-12-13T02:59:20.723 に答える
0

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();
于 2021-12-02T13:55:59.273 に答える