我正在尝试找出一种方法,使我的 API 能够将 Facebook 中的用户与我的身份用户关联起来。
应用上下文
我正在开发一个移动应用程序(在 Xamarin 中),需要使用用户名/密码和 Facebook 进行登录。我已经设置了app.UseOpenIdConnectServer
配置并创建了自定义Provider
所以我的应用程序已经可以使用用户名/密码。
现在我正在尝试与 Facebook 进行集成,但没有找到实现这一目标的方法。
我正在考虑在 API 中创建一个服务,比如/api/auth/login-facebook/
通过access-token
来自 Facebook,但我需要归还access-token
我的 API 应用程序到移动应用程序,以便移动应用程序可以调用所有其他需要授权的服务。
有什么帮助吗?
我想要得到的视觉方式:
- 用户在移动应用程序中按“使用 Facebook 登录”
- 移动应用程序调用
/api/auth/login-facebook/
通过access-token
来自脸书
- 在 API 应用程序中,我将检查
access-token
与Facebook
package
- 如果用户不存在,我将使用 Facebook 返回给我的数据创建他,然后生成
access-token
授予对我的 API 应用程序的访问权限
- 如果用户存在,我将生成
access-token
授予对我的 API 应用程序的访问权限
- 返回
access-token
到移动应用程序,以便它可以调用其他服务
如果我的知识是错误的,我应该以其他方式进行此集成/登录,请随时告诉我!
您描述的流程与“断言授予”非常相似,该流程是标准化去年。
要使用此流程,您通常必须从外部提供商检索标准令牌(例如 JWT 或 SAML 断言),以便您自己的授权服务器可以验证它并提取其公开的声明。不幸的是,Facebook 或大多数社交提供商无法做到这一点。
新的 OAuth2 草案未来可能会有助于改变这种情况,但主要服务可能需要一段时间才能开始实施它。
好消息是,没有什么可以阻止您同时创建自己的“Facebook 访问令牌”授予类型。以下是如何使用 ASOS beta6 实现断言授权:
public override Task ValidateTokenRequest(ValidateTokenRequestContext context)
{
// Reject the token request if it doesn't use grant_type=password, refresh_token
// or urn:ietf:params:oauth:grant-type:facebook_access_token.
if (!context.Request.IsPasswordGrantType() &&
!context.Request.IsRefreshTokenGrantType() &&
context.Request.GrantType != "urn:ietf:params:oauth:grant-type:facebook_access_token")
{
context.Reject(
error: OpenIdConnectConstants.Errors.UnsupportedGrantType,
description: "The specified grant type is not supported by this server.");
return Task.FromResult(0);
}
// Reject the token request if the assertion parameter is missing.
if (context.Request.GrantType == "urn:ietf:params:oauth:grant-type:facebook_access_token" &&
string.IsNullOrEmpty(context.Request.Assertion))
{
context.Reject(
error: OpenIdConnectConstants.Errors.InvalidRequest,
description: "The assertion is missing.");
return Task.FromResult(0);
}
// Since there's only one application and since it's a public client
// (i.e a client that cannot keep its credentials private), call Skip()
// to inform the server the request should be accepted without
// enforcing client authentication.
context.Skip();
return Task.FromResult(0);
}
public override Task HandleTokenRequest(HandleTokenRequestContext context)
{
// Only handle grant_type=password and urn:ietf:params:oauth:grant-type:facebook_access_token
// requests and let the OpenID Connect server middleware handle the refresh token requests.
if (context.Request.IsPasswordGrantType())
{
// Skipped for brevity.
}
else if (context.Request.GrantType == "urn:ietf:params:oauth:grant-type:facebook_access_token")
{
// The assertion corresponds to the Facebook access token.
var assertion = context.Request.Assertion;
// Create a new ClaimsIdentity containing the claims that
// will be used to create an id_token and/or an access token.
var identity = new ClaimsIdentity(OpenIdConnectServerDefaults.AuthenticationScheme);
// Validate the access token using Facebook's token validation
// endpoint and add the user claims you retrieved here.
identity.AddClaim(ClaimTypes.NameIdentifier, "FB user identifier");
// Create a new authentication ticket holding the user identity.
var ticket = new AuthenticationTicket(
new ClaimsPrincipal(identity),
new AuthenticationProperties(),
OpenIdConnectServerDefaults.AuthenticationScheme);
// Set the list of scopes granted to the client application.
ticket.SetScopes(new[]
{
/* openid: */ OpenIdConnectConstants.Scopes.OpenId,
/* email: */ OpenIdConnectConstants.Scopes.Email,
/* profile: */ OpenIdConnectConstants.Scopes.Profile,
/* offline_access: */ OpenIdConnectConstants.Scopes.OfflineAccess
}.Intersect(context.Request.GetScopes()));
context.Validate(ticket);
}
return Task.FromResult(0);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)