/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/openiddict/openiddict-core for more information concerning
* the license and the contributors participating to this project.
*/
using System;
using System.Security.Claims;
using JetBrains.Annotations;
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Tokens;
using static OpenIddict.Abstractions.OpenIddictConstants;
namespace OpenIddict.Server
{
public static partial class OpenIddictServerEvents
{
///
/// Represents an abstract base class used for certain event contexts.
///
public abstract class BaseSerializingContext : BaseContext
{
///
/// Creates a new instance of the class.
///
public BaseSerializingContext([NotNull] OpenIddictServerTransaction transaction)
: base(transaction)
{
}
///
/// Gets or sets the security principal containing the claims to serialize.
///
public ClaimsPrincipal Principal { get; set; }
///
/// Gets or sets the encrypting credentials used to encrypt the token.
///
public EncryptingCredentials EncryptingCredentials { get; set; }
///
/// Gets or sets the signing credentials used to sign the token.
///
public SigningCredentials SigningCredentials { get; set; }
///
/// Gets or sets the security token handler used to serialize the token.
///
public JsonWebTokenHandler SecurityTokenHandler { get; set; }
///
/// Gets or sets the token returned to the client application.
///
public string Token { get; set; }
///
/// Gets or sets the token usage.
///
public string TokenUsage { get; set; }
///
/// Gets a boolean indicating whether the
/// method was called.
///
public bool IsHandled { get; private set; }
///
/// Marks the serialization process as handled by the application code.
///
public void HandleSerialization() => IsHandled = true;
}
///
/// Represents an abstract base class used for certain event contexts.
///
public abstract class BaseDeserializingContext : BaseContext
{
///
/// Creates a new instance of the class.
///
public BaseDeserializingContext([NotNull] OpenIddictServerTransaction transaction)
: base(transaction)
{
}
///
/// Gets or sets the security principal containing the deserialized claims.
///
public ClaimsPrincipal Principal { get; set; }
///
/// Gets or sets the validation parameters used to verify the authenticity of access tokens.
/// Note: this property is only used when is not null.
///
public TokenValidationParameters TokenValidationParameters { get; set; } = new TokenValidationParameters();
///
/// Gets or sets the security token handler used to
/// deserialize the authentication ticket.
///
public JsonWebTokenHandler SecurityTokenHandler { get; set; }
///
/// Gets or sets the token used by the client application.
///
public string Token { get; set; }
///
/// Gets or sets the token usage.
///
public string TokenUsage { get; set; }
///
/// Gets a boolean indicating whether the
/// method was called.
///
public bool IsHandled { get; private set; }
///
/// Marks the deserialization process as handled by the application code.
///
public void HandleDeserialization() => IsHandled = true;
}
///
/// Represents an event called when serializing an access token.
///
public class SerializeAccessTokenContext : BaseSerializingContext
{
///
/// Creates a new instance of the class.
///
public SerializeAccessTokenContext([NotNull] OpenIddictServerTransaction transaction)
: base(transaction)
=> TokenUsage = TokenUsages.AccessToken;
}
///
/// Represents an event called when serializing an authorization code.
///
public class SerializeAuthorizationCodeContext : BaseSerializingContext
{
///
/// Creates a new instance of the class.
///
public SerializeAuthorizationCodeContext([NotNull] OpenIddictServerTransaction transaction)
: base(transaction)
=> TokenUsage = TokenUsages.AuthorizationCode;
}
///
/// Represents an event called when serializing an identity token.
///
public class SerializeIdentityTokenContext : BaseSerializingContext
{
///
/// Creates a new instance of the class.
///
public SerializeIdentityTokenContext([NotNull] OpenIddictServerTransaction transaction)
: base(transaction)
=> TokenUsage = TokenUsages.IdToken;
}
///
/// Represents an event called when serializing a refresh token.
///
public class SerializeRefreshTokenContext : BaseSerializingContext
{
///
/// Creates a new instance of the class.
///
public SerializeRefreshTokenContext([NotNull] OpenIddictServerTransaction transaction)
: base(transaction)
=> TokenUsage = TokenUsages.RefreshToken;
}
///
/// Represents an event called when deserializing an access token.
///
public class DeserializeAccessTokenContext : BaseDeserializingContext
{
///
/// Creates a new instance of the class.
///
public DeserializeAccessTokenContext([NotNull] OpenIddictServerTransaction transaction)
: base(transaction)
=> TokenUsage = TokenUsages.AccessToken;
}
///
/// Represents an event called when deserializing an authorization code.
///
public class DeserializeAuthorizationCodeContext : BaseDeserializingContext
{
///
/// Creates a new instance of the class.
///
public DeserializeAuthorizationCodeContext([NotNull] OpenIddictServerTransaction transaction)
: base(transaction)
=> TokenUsage = TokenUsages.AuthorizationCode;
}
///
/// Represents an event called when deserializing an identity token.
///
public class DeserializeIdentityTokenContext : BaseDeserializingContext
{
///
/// Creates a new instance of the class.
///
public DeserializeIdentityTokenContext([NotNull] OpenIddictServerTransaction transaction)
: base(transaction)
=> TokenUsage = TokenUsages.IdToken;
}
///
/// Represents an event called when deserializing a refresh token.
///
public class DeserializeRefreshTokenContext : BaseDeserializingContext
{
///
/// Creates a new instance of the class.
///
public DeserializeRefreshTokenContext([NotNull] OpenIddictServerTransaction transaction)
: base(transaction)
=> TokenUsage = TokenUsages.RefreshToken;
}
}
}