我正在设置一个Sign in with Google按钮,允许用户在我的网站上购买商品。
客户端身份验证看起来非常简单,但我很难理解服务器端身份验证的工作原理。在example code中,他们将客户端“代码”参数传递给服务器,在那里可以交换访问令牌,然后可以用来查看用户朋友的列表。
但我不想看到用户朋友的列表。我只是想确定客户实际上是他们声称的那个人。
在检索令牌之后,示例代码将令牌放入会话中,并且似乎使用令牌的存在来验证用户是否经过身份验证。这是正确/安全吗?应该(不)我的服务器以某种方式重新验证令牌(怎么样?)什么时候进行购买?我是否应该在每次请求时不断向Google重新验证令牌? (希望不是吗?)
答案 0 :(得分:2)
执行购买之前您可能想要做的是通过将用户ID从客户端安全地传递到服务器并根据存储凭据的用户ID验证用户,确认用户是您期望的用户。这可以提供额外的保护,防止重复攻击,即攻击者通过劫持会话伪装成您网站的用户,并且在接受用户付款之前是最相关的检查。
我不会仅依靠用户验证作为防范欺诈的机制。您应使用安全支付系统,例如Google Commerce platform,然后按the best practices for commerce。
提醒一下,每次初始化缓存凭据时,都应使用OAuth2 v2端点检查令牌。检查每个请求似乎有点过分,因为您应该使用已经过验证并存储在服务器端的缓存凭据。最多可以在更新访问令牌时执行检查,但如果您信任刷新令牌,则在创建帐户并设置刷新令牌时执行检查应该足够安全。
除了帐户创建时的用户ID验证外,还采取了以下步骤:
正如您在链接的帖子中所提到的,the Google+ quickstarts中的示例代码应充分展示如何使用各种编程语言执行这些检查以进行帐户授权。
在HTML / JS客户端中,以下代码显示了userId(值,而不是特殊字符串“me”)的位置,用于传递给connect方法以验证Google+ userId:
var request = gapi.client.plus.people.get( {'userId' : 'me'} );
request.execute( function(profile) {
$('#profile').empty();
if (profile.error) {
$('#profile').append(profile.error);
return;
}
helper.connectServer(profile.id);
$('#profile').append(
$('<p><img src=\"' + profile.image.url + '\"></p>'));
$('#profile').append(
$('<p>Hello ' + profile.displayName + '!<br />Tagline: ' +
profile.tagline + '<br />About: ' + profile.aboutMe + '</p>'));
if (profile.cover && profile.coverPhoto) {
$('#profile').append(
$('<p><img src=\"' + profile.cover.coverPhoto.url + '\"></p>'));
}
});
...以下代码显示正在传递的Google+ ID。
connectServer: function(gplusId) {
console.log(this.authResult.code);
$.ajax({
type: 'POST',
url: window.location.href + '/connect?state={{ STATE }}&gplus_id=' +
gplusId,
contentType: 'application/octet-stream; charset=utf-8',
success: function(result) {
console.log(result);
helper.people();
},
processData: false,
data: this.authResult.code
});
}
在Java示例中执行这些检查的相关代码如下:
// Check that the token is valid.
Oauth2 oauth2 = new Oauth2.Builder(
TRANSPORT, JSON_FACTORY, credential).build();
Tokeninfo tokenInfo = oauth2.tokeninfo()
.setAccessToken(credential.getAccessToken()).execute();
// If there was an error in the token info, abort.
if (tokenInfo.containsKey("error")) {
response.status(401);
return GSON.toJson(tokenInfo.get("error").toString());
}
// Make sure the token we got is for the intended user.
if (!tokenInfo.getUserId().equals(gPlusId)) {
response.status(401);
return GSON.toJson("Token's user ID doesn't match given user ID.");
}
// Make sure the token we got is for our app.
if (!tokenInfo.getIssuedTo().equals(CLIENT_ID)) {
response.status(401);
return GSON.toJson("Token's client ID does not match app's.");
}
// Store the token in the session for later use.
request.session().attribute("token", tokenResponse.toString());
return GSON.toJson("Successfully connected user.");
} catch (TokenResponseException e) {
response.status(500);
return GSON.toJson("Failed to upgrade the authorization code.");
} catch (IOException e) {
response.status(500);
return GSON.toJson("Failed to read token data from Google. " +
e.getMessage());
}
在示例中,ClientID来自Google API控制台,与您的应用程序不同。