Repository navigation
Expand file tree
/
Copy pathOAuthCreatingTicketContext.cs
More file actions
148 lines (128 loc) · 5.09 KB
/
Copy pathOAuthCreatingTicketContext.cs
File metadata and controls
148 lines (128 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// 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.AspNet.Http;
using Microsoft.AspNet.Http.Authentication;
using Newtonsoft.Json.Linq;
namespace Microsoft.AspNet.Authentication.OAuth
{
/// <summary>
/// Contains information about the login session as well as the user <see cref="System.Security.Claims.ClaimsIdentity"/>.
/// </summary>
public class OAuthCreatingTicketContext : BaseContext
{
/// <summary>
/// Initializes a new <see cref="OAuthCreatingTicketContext"/>.
/// </summary>
/// <param name="context">The HTTP environment.</param>
/// <param name="options">The options used by the authentication middleware.</param>
/// <param name="backchannel">The HTTP client used by the authentication middleware</param>
/// <param name="tokens">The tokens returned from the token endpoint.</param>
public OAuthCreatingTicketContext(
HttpContext context,
OAuthOptions options,
HttpClient backchannel,
OAuthTokenResponse tokens)
: this(context, options, backchannel, tokens, user: new JObject())
{
}
/// <summary>
/// Initializes a new <see cref="OAuthCreatingTicketContext"/>.
/// </summary>
/// <param name="context">The HTTP environment.</param>
/// <param name="options">The options used by the authentication middleware.</param>
/// <param name="backchannel">The HTTP client used by the authentication middleware</param>
/// <param name="tokens">The tokens returned from the token endpoint.</param>
/// <param name="user">The JSON-serialized user.</param>
public OAuthCreatingTicketContext(
HttpContext context,
OAuthOptions options,
HttpClient backchannel,
OAuthTokenResponse tokens,
JObject user)
: base(context)
{
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));
}
TokenResponse = tokens;
Backchannel = backchannel;
User = user;
Options = options;
}
public OAuthOptions Options { get; }
/// <summary>
/// Gets the JSON-serialized user or an empty
/// <see cref="JObject"/> if it is not available.
/// </summary>
public JObject User { get; }
/// <summary>
/// Gets the token response returned by the authentication service.
/// </summary>
public OAuthTokenResponse TokenResponse { get; }
/// <summary>
/// Gets the access token provided by the authentication service.
/// </summary>
public string AccessToken => TokenResponse.AccessToken;
/// <summary>
/// Gets the access token type provided by the authentication service.
/// </summary>
public string TokenType => TokenResponse.TokenType;
/// <summary>
/// Gets the refresh token provided by the authentication service.
/// </summary>
public string RefreshToken => TokenResponse.RefreshToken;
/// <summary>
/// Gets the access token expiration time.
/// </summary>
public TimeSpan? ExpiresIn
{
get
{
int value;
if (int.TryParse(TokenResponse.ExpiresIn, NumberStyles.Integer, CultureInfo.InvariantCulture, out value))
{
return TimeSpan.FromSeconds(value);
}
return null;
}
}
/// <summary>
/// Gets the backchannel used to communicate with the provider.
/// </summary>
public HttpClient Backchannel { get; }
/// <summary>
/// Gets the <see cref="ClaimsPrincipal"/> representing the user.
/// </summary>
public ClaimsPrincipal Principal { get; set; }
/// <summary>
/// Gets the main identity exposed by <see cref="Principal"/>.
/// This property returns <c>null</c> when <see cref="Principal"/> is <c>null</c>.
/// </summary>
public ClaimsIdentity Identity => Principal?.Identity as ClaimsIdentity;
/// <summary>
/// Gets or sets a property bag for common authentication properties.
/// </summary>
public AuthenticationProperties Properties { get; set; }
}
}