par*_*870 85 google-api oauth-2.0 google-oauth
我似乎无法找到有关如何限制登录我的Web应用程序(使用OAuth2.0和Google API)的任何文档,只接受来自用户的身份验证请求以及特定域名或域名集的电子邮件.我想白名单而不是黑名单.
有没有人有关于如何做到这一点的建议,关于官方接受的方法的文档,或者一个简单,安全的工作?
为了记录,在他们尝试通过Google的OAuth身份验证登录之前,我不知道有关该用户的任何信息.我收到的只是基本的用户信息和电子邮件.
Aar*_*uce 40
所以我有一个答案给你.在oauth请求中,您可以添加"hd = domain.com",它将限制对该域中用户的身份验证(我不知道您是否可以执行多个域).您可以在此处找到hd参数
我在这里使用谷歌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";
}
Run Code Online (Sandbox Code Playgroud)
编辑:我仍在使用这个应用程序并找到了这个,这可能是这个问题更正确的答案.https://developers.google.com/google-apps/profiles/
定义提供程序时,使用'hd'参数在末尾传入哈希.你可以在这里阅读.https://developers.google.com/accounts/docs/OpenIDConnect#hd-param
例如,对于config/initializers/devise.rb
config.omniauth :google_oauth2, 'identifier', 'key', {hd: 'yourdomain.com'}
Run Code Online (Sandbox Code Playgroud)
使用auth2
init函数,您可以传递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(); });
});
}
});
});
Run Code Online (Sandbox Code Playgroud)
如果您不使用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();
});
});
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
50719 次 |
最近记录: |