// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Globalization; using System.Net.Http; using System.Security.Claims; using Microsoft.AspNetCore.Http; using Newtonsoft.Json.Linq; namespace Microsoft.AspNetCore.Authentication.OAuth { /// /// Contains information about the login session as well as the user . /// public class OAuthCreatingTicketContext : BaseAuthenticationContext { /// /// Initializes a new . /// /// The . /// The HTTP environment. /// The authentication scheme. /// The options used by the authentication middleware. /// The HTTP client used by the authentication middleware /// The tokens returned from the token endpoint. public OAuthCreatingTicketContext( AuthenticationTicket ticket, HttpContext context, AuthenticationScheme scheme, OAuthOptions options, HttpClient backchannel, OAuthTokenResponse tokens) : this(ticket, context, scheme, options, backchannel, tokens, user: new JObject()) { } /// /// Initializes a new . /// /// The . /// The HTTP environment. /// The authentication scheme. /// The options used by the authentication middleware. /// The HTTP client used by the authentication middleware /// The tokens returned from the token endpoint. /// The JSON-serialized user. public OAuthCreatingTicketContext( AuthenticationTicket ticket, HttpContext context, AuthenticationScheme scheme, OAuthOptions options, HttpClient backchannel, OAuthTokenResponse tokens, JObject user) : base(context, scheme.Name, ticket.Properties) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } if (backchannel == null) { throw new ArgumentNullException(nameof(backchannel)); } if (tokens == null) { throw new ArgumentNullException(nameof(tokens)); } if (user == null) { throw new ArgumentNullException(nameof(user)); } if (scheme == null) { throw new ArgumentNullException(nameof(scheme)); } TokenResponse = tokens; Backchannel = backchannel; User = user; Options = options; Scheme = scheme; Ticket = ticket; } public OAuthOptions Options { get; } public AuthenticationScheme Scheme { get; } /// /// Gets the JSON-serialized user or an empty /// if it is not available. /// public JObject User { get; } /// /// Gets the token response returned by the authentication service. /// public OAuthTokenResponse TokenResponse { get; } /// /// Gets the access token provided by the authentication service. /// public string AccessToken => TokenResponse.AccessToken; /// /// Gets the access token type provided by the authentication service. /// public string TokenType => TokenResponse.TokenType; /// /// Gets the refresh token provided by the authentication service. /// public string RefreshToken => TokenResponse.RefreshToken; /// /// Gets the access token expiration time. /// public TimeSpan? ExpiresIn { get { int value; if (int.TryParse(TokenResponse.ExpiresIn, NumberStyles.Integer, CultureInfo.InvariantCulture, out value)) { return TimeSpan.FromSeconds(value); } return null; } } /// /// Gets the backchannel used to communicate with the provider. /// public HttpClient Backchannel { get; } /// /// The that will be created. /// public AuthenticationTicket Ticket { get; set; } /// /// Gets the main identity exposed by . /// This property returns null when is null. /// public ClaimsIdentity Identity => Ticket?.Principal.Identity as ClaimsIdentity; public void RunClaimActions() { RunClaimActions(User); } public void RunClaimActions(JObject userData) { if (userData == null) { throw new ArgumentNullException(nameof(userData)); } foreach (var action in Options.ClaimActions) { action.Run(userData, Identity, Options.ClaimsIssuer); } } } }