Repository navigation
Expand file tree
/
Copy pathOAuth2Provider.cs
More file actions
244 lines (201 loc) · 11.4 KB
/
Copy pathOAuth2Provider.cs
File metadata and controls
244 lines (201 loc) · 11.4 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using ServiceStack.Configuration;
using ServiceStack.Templates;
using ServiceStack.Text;
namespace ServiceStack.Auth
{
public abstract class OAuth2Provider : OAuthProvider
{
public OAuth2Provider(IAppSettings appSettings, string authRealm, string oAuthProvider)
: base(appSettings, authRealm, oAuthProvider)
{
Scopes = appSettings.Get($"oauth.{Provider}.{nameof(Scopes)}", TypeConstants.EmptyStringArray);
}
public OAuth2Provider(IAppSettings appSettings, string authRealm, string oAuthProvider, string consumerKeyName,
string consumerSecretName)
: base(appSettings, authRealm, oAuthProvider, consumerKeyName, consumerSecretName)
{
Scopes = appSettings.Get($"oauth.{Provider}.{nameof(Scopes)}", TypeConstants.EmptyStringArray);
}
public string[] Scopes { get; set; }
public string ResponseMode { get; set; }
protected override void AssertValidState()
{
base.AssertValidState();
AssertAuthorizeUrl();
AssertAccessTokenUrl();
}
protected virtual void AssertAccessTokenUrl()
{
if (string.IsNullOrEmpty(AccessTokenUrl))
throw new Exception($"oauth.{Provider}.{nameof(AccessTokenUrl)} is required");
}
protected virtual void AssertAuthorizeUrl()
{
if (string.IsNullOrEmpty(AuthorizeUrl))
throw new Exception($"oauth.{Provider}.{nameof(AuthorizeUrl)} is required");
}
public override async Task<object> AuthenticateAsync(IServiceBase authService, IAuthSession session, Authenticate request, CancellationToken token = default)
{
var tokens = Init(authService, ref session, request);
var ctx = CreateAuthContext(authService, session, tokens);
//Transferring AccessToken/Secret from Mobile/Desktop App to Server
if (request?.AccessToken != null && VerifyAccessTokenAsync != null)
{
if (!await VerifyAccessTokenAsync(request.AccessToken, ctx).ConfigAwait())
return HttpError.Unauthorized(ErrorMessages.InvalidAccessToken.Localize(authService.Request));
var isHtml = authService.Request.IsHtml();
var failedResult = await AuthenticateWithAccessTokenAsync(authService, session, tokens, request.AccessToken, ctx.AuthInfo, token: token).ConfigAwait();
if (failedResult != null)
return ConvertToClientError(failedResult, isHtml);
return isHtml
? await authService.Redirect(SuccessRedirectUrlFilter(ctx, session.ReferrerUrl.SetParam("s", "1"))).SuccessAuthResultAsync(authService,session).ConfigAwait()
: null; //return default AuthenticateResponse
}
var httpRequest = authService.Request;
var error = httpRequest.GetQueryStringOrForm("error_reason")
?? httpRequest.GetQueryStringOrForm("error")
?? httpRequest.GetQueryStringOrForm("error_code")
?? httpRequest.GetQueryStringOrForm("error_description");
var hasError = !error.IsNullOrEmpty();
if (hasError)
{
var httpParams = HttpUtils.HasRequestBody(httpRequest.Verb)
? httpRequest.QueryString
: httpRequest.FormData;
Log.Error($"OAuth2 Error callback. {httpParams}");
return authService.Redirect(FailedRedirectUrlFilter(ctx, session.ReferrerUrl.SetParam("f", error)));
}
var code = httpRequest.GetQueryStringOrForm(Keywords.Code);
var isPreAuthCallback = !code.IsNullOrEmpty();
if (!isPreAuthCallback)
{
var oauthstate = session.Id;
var preAuthUrl = AuthorizeUrl
.AddQueryParam("response_type", "code")
.AddQueryParam("client_id", ConsumerKey)
.AddQueryParam("redirect_uri", this.CallbackUrl)
.AddQueryParam("scope", string.Join(" ", Scopes))
.AddQueryParam(Keywords.State, oauthstate);
if (ResponseMode != null)
preAuthUrl = preAuthUrl.AddQueryParam("response_mode", ResponseMode);
if (session is AuthUserSession authSession)
(authSession.Meta ?? (authSession.Meta = new Dictionary<string, string>()))["oauthstate"] = oauthstate;
await this.SaveSessionAsync(authService, session, SessionExpiry, token).ConfigAwait();
return authService.Redirect(PreAuthUrlFilter(ctx, preAuthUrl));
}
try
{
var state = httpRequest.GetQueryStringOrForm(Keywords.State);
if (state != null && session is AuthUserSession authSession)
{
if (authSession.Meta == null)
authSession.Meta = new Dictionary<string, string>();
if (authSession.Meta.TryGetValue("oauthstate", out var oauthState) && state != oauthState)
return authService.Redirect(FailedRedirectUrlFilter(ctx, session.ReferrerUrl.SetParam("f", "InvalidState")));
authSession.Meta.Remove("oauthstate");
}
var contents = await GetAccessTokenJsonAsync(code, ctx, token).ConfigAwait();
var authInfo = (Dictionary<string,object>)JSON.parse(contents);
ctx.AuthInfo = authInfo.ToStringDictionary();
var accessToken = (string)authInfo["access_token"];
var redirectUrl = SuccessRedirectUrlFilter(ctx, session.ReferrerUrl.SetParam("s", "1"));
var errorResult = await AuthenticateWithAccessTokenAsync(authService, session, tokens, accessToken, ctx.AuthInfo, token).ConfigAwait();
if (errorResult != null)
return errorResult;
//Haz Access!
if (HostContext.Config?.UseSameSiteCookies == true)
{
// Workaround Set-Cookie HTTP Header not being honoured in 302 Redirects
var redirectHtml = HtmlTemplates.GetHtmlRedirectTemplate(redirectUrl);
return await new HttpResult(redirectHtml, MimeTypes.Html).SuccessAuthResultAsync(authService,session).ConfigAwait();
}
return await authService.Redirect(redirectUrl).SuccessAuthResultAsync(authService,session).ConfigAwait();
}
catch (WebException we)
{
var errorBody = await we.GetResponseBodyAsync(token).ConfigAwait();
Log.Error($"Failed to get Access Token for '{Provider}': {errorBody}");
var statusCode = ((HttpWebResponse)we.Response).StatusCode;
if (statusCode == HttpStatusCode.BadRequest)
{
return authService.Redirect(FailedRedirectUrlFilter(ctx, session.ReferrerUrl.SetParam("f", "AccessTokenFailed")));
}
}
//Shouldn't get here
return authService.Redirect(FailedRedirectUrlFilter(ctx, session.ReferrerUrl.SetParam("f", "Unknown")));
}
protected virtual async Task<string> GetAccessTokenJsonAsync(string code, AuthContext ctx, CancellationToken token=default)
{
var accessTokenUrl = $"{AccessTokenUrl}?code={code}&client_id={ConsumerKey}&client_secret={ConsumerSecret}&redirect_uri={this.CallbackUrl.UrlEncode()}&grant_type=authorization_code";
var contents = await AccessTokenUrlFilter(ctx, accessTokenUrl).PostToUrlAsync("", token: token).ConfigAwait();
return contents;
}
protected virtual async Task<object> AuthenticateWithAccessTokenAsync(IServiceBase authService, IAuthSession session, IAuthTokens tokens,
string accessToken, Dictionary<string,string> authInfo = null, CancellationToken token=default)
{
tokens.AccessToken = accessToken;
if (authInfo != null)
{
tokens.Items ??= new Dictionary<string, string>();
foreach (var entry in authInfo)
{
tokens.Items[entry.Key] = entry.Value;
}
}
var accessTokenAuthInfo = await this.CreateAuthInfoAsync(accessToken, token).ConfigAwait();
session.IsAuthenticated = true;
return await OnAuthenticatedAsync(authService, session, tokens, accessTokenAuthInfo, token).ConfigAwait();
}
protected abstract Task<Dictionary<string, string>> CreateAuthInfoAsync(string accessToken, CancellationToken token=default);
/// <summary>
/// Override to return User chosen username or Email for this AuthProvider
/// </summary>
protected virtual string GetUserAuthName(IAuthTokens tokens, Dictionary<string, string> authInfo) =>
tokens.Email;
protected override Task LoadUserAuthInfoAsync(AuthUserSession userSession, IAuthTokens tokens, Dictionary<string, string> authInfo, CancellationToken token=default)
{
try
{
tokens.UserId = authInfo.Get("user_id");
tokens.UserName = authInfo.Get("username") ?? tokens.UserId;
tokens.DisplayName = authInfo.Get("name");
tokens.FirstName = authInfo.Get("first_name");
tokens.LastName = authInfo.Get("last_name");
tokens.Email = authInfo.Get("email");
userSession.UserAuthName = GetUserAuthName(tokens, authInfo);
if (authInfo.TryGetValue(AuthMetadataProvider.ProfileUrlKey, out var profileUrl))
{
if (string.IsNullOrEmpty(userSession.ProfileUrl))
userSession.ProfileUrl = profileUrl.SanitizeOAuthUrl();
}
}
catch (Exception ex)
{
Log.Error($"Could not retrieve '{Provider}' profile user info for '{tokens.DisplayName}'", ex);
}
LoadUserOAuthProvider(userSession, tokens);
return TypeConstants.EmptyTask;
}
/// <summary>
/// Custom DisplayName resolver function when not provided
/// </summary>
public Func<IAuthSession,IAuthTokens, string> ResolveUnknownDisplayName { get; set; }
public override void LoadUserOAuthProvider(IAuthSession authSession, IAuthTokens tokens)
{
if (!(authSession is AuthUserSession userSession))
return;
userSession.UserName = tokens.UserName ?? userSession.UserName;
userSession.DisplayName = tokens.DisplayName ?? userSession.DisplayName;
userSession.FirstName = tokens.FirstName ?? userSession.FirstName;
userSession.LastName = tokens.LastName ?? userSession.LastName;
userSession.Email = userSession.PrimaryEmail = tokens.Email ?? userSession.PrimaryEmail ?? userSession.Email;
if (userSession.DisplayName == null && ResolveUnknownDisplayName != null)
userSession.DisplayName = ResolveUnknownDisplayName(authSession, tokens);
}
}
}