| | | 1 | | namespace ArturRios.Util.WebApi.Security.Interfaces; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Translates between the app's <see cref="IAuthenticatedUser"/> and the claims carried by its JSON Web |
| | | 5 | | /// Tokens. One implementation owns both directions, so the claims written when a token is issued and the |
| | | 6 | | /// claims read when it is validated cannot drift apart. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks>Implementations must return <see langword="null"/> for claims they cannot interpret rather |
| | | 9 | | /// than throwing. Use <c>TryParse</c>, not <c>Parse</c>.</remarks> |
| | | 10 | | public interface IAuthenticatedUserMapper |
| | | 11 | | { |
| | | 12 | | /// <summary>Builds the claims to embed in a token for the given user, called by the app at login.</summary> |
| | | 13 | | /// <param name="user">The user the token will represent.</param> |
| | | 14 | | /// <returns>The claims to embed, keyed by claim name.</returns> |
| | | 15 | | Dictionary<string, string> ToClaims(IAuthenticatedUser user); |
| | | 16 | | |
| | | 17 | | /// <summary>Rebuilds the user from a token's claims, without any data-store lookup.</summary> |
| | | 18 | | /// <param name="claims">The claims read from the token.</param> |
| | | 19 | | /// <returns>The user, or <see langword="null"/> if the claims cannot produce one.</returns> |
| | | 20 | | IAuthenticatedUser? FromClaims(IReadOnlyDictionary<string, string> claims); |
| | | 21 | | |
| | | 22 | | /// <summary>Reads just the user id from a token's claims, used when the rest of the user is resolved |
| | | 23 | | /// from the data store. Override this when the token carries more than <see cref="FromClaims"/> requires.</summary> |
| | | 24 | | /// <param name="claims">The claims read from the token.</param> |
| | | 25 | | /// <returns>The user id, or <see langword="null"/> if the claims do not carry one.</returns> |
| | 6 | 26 | | Guid? IdFromClaims(IReadOnlyDictionary<string, string> claims) => FromClaims(claims)?.Id; |
| | | 27 | | } |