Skip to content

Commit

Permalink
Add support for Alipay's new user identity openid
Browse files Browse the repository at this point in the history
  • Loading branch information
BuknSS committed Oct 30, 2024
1 parent 82d20e3 commit 4c441bd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/AspNet.Security.OAuth.Alipay/AlipayAuthenticationConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,16 @@ public static class Claims
/// The user's gender. F: Female; M: Male.
/// </summary>
public const string Gender = "urn:alipay:gender";

/// <summary>
/// OpenID is the unique identifier of Alipay users in the application dimension.
/// See https://opendocs.alipay.com/mini/0ai2i6
/// </summary>
public const string OpenId = "urn:alipay:open_id";

/// <summary>
/// Alipay user system internal identifier, will no longer be independently open in the future, and will be replaced by OpenID.
/// </summary>
public const string UserId = "urn:alipay:user_id";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
throw new AuthenticationFailureException($"An error (Code:{code}) occurred while retrieving user information.");
}

identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, mainElement.GetString("user_id")!, ClaimValueTypes.String, Options.ClaimsIssuer));

var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, mainElement);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,14 @@ public AlipayAuthenticationOptions()
ClaimActions.MapJsonKey(Claims.Gender, "gender");
ClaimActions.MapJsonKey(Claims.Nickname, "nick_name");
ClaimActions.MapJsonKey(Claims.Province, "province");
ClaimActions.MapJsonKey(Claims.OpenId, "open_id");
ClaimActions.MapJsonKey(Claims.UserId, "user_id");
ClaimActions.MapCustomJson(System.Security.Claims.ClaimTypes.NameIdentifier, user => user.GetString(NameIdentifierKey));
}

/// <summary>
/// Alipay user system internal identifier, which will no longer be open independently in the future and will be replaced by open_id. Currently the default is user_id
/// See https://opendocs.alipay.com/mini/0ai2i6?pathHash=13dd5946
/// </summary>
public string NameIdentifierKey { get; set; } = "user_id";
}

0 comments on commit 4c441bd

Please sign in to comment.