forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthProvider.cs
More file actions
203 lines (169 loc) · 7.82 KB
/
AuthProvider.cs
File metadata and controls
203 lines (169 loc) · 7.82 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
using System;
using System.Collections.Generic;
using System.Net;
using ServiceStack.Common;
using ServiceStack.Common.Web;
using ServiceStack.Configuration;
using ServiceStack.Logging;
using ServiceStack.ServiceHost;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints.Extensions;
namespace ServiceStack.ServiceInterface.Auth
{
public abstract class AuthProvider : IAuthProvider
{
protected static readonly ILog Log = LogManager.GetLogger(typeof(AuthProvider));
public static TimeSpan DefaultSessionExpiry = TimeSpan.FromDays(7 * 2); //2 weeks
public TimeSpan? SessionExpiry { get; set; }
public string AuthRealm { get; set; }
public string Provider { get; set; }
public string CallbackUrl { get; set; }
public string RedirectUrl { get; set; }
protected AuthProvider() { }
protected AuthProvider(IResourceManager appSettings, string authRealm, string oAuthProvider)
{
// Enhancement per https://github.com/ServiceStack/ServiceStack/issues/741
this.AuthRealm = appSettings != null ? appSettings.Get("OAuthRealm", authRealm) : authRealm;
this.Provider = oAuthProvider;
if (appSettings != null)
{
this.CallbackUrl = appSettings.GetString("oauth.{0}.CallbackUrl".Fmt(oAuthProvider));
this.RedirectUrl = appSettings.GetString("oauth.{0}.RedirectUrl".Fmt(oAuthProvider));
}
this.SessionExpiry = DefaultSessionExpiry;
}
/// <summary>
/// Remove the Users Session
/// </summary>
/// <param name="service"></param>
/// <param name="request"></param>
/// <returns></returns>
public virtual object Logout(IServiceBase service, Auth request)
{
var session = service.GetSession();
var referrerUrl = (request != null ? request.Continue : null)
?? session.ReferrerUrl
?? service.RequestContext.GetHeader("Referer")
?? this.CallbackUrl;
session.OnLogout(service);
service.RemoveSession();
if (service.RequestContext.ResponseContentType == ContentType.Html && !String.IsNullOrEmpty(referrerUrl))
return service.Redirect(referrerUrl.AddHashParam("s", "-1"));
return new AuthResponse();
}
/// <summary>
/// Saves the Auth Tokens for this request. Called in OnAuthenticated().
/// Overrideable, the default behaviour is to call IUserAuthRepository.CreateOrMergeAuthSession().
/// </summary>
protected virtual void SaveUserAuth(IServiceBase authService, IAuthSession session, IUserAuthRepository authRepo, IOAuthTokens tokens)
{
if (authRepo == null) return;
if (tokens != null)
{
session.UserAuthId = authRepo.CreateOrMergeAuthSession(session, tokens);
}
authRepo.LoadUserAuth(session, tokens);
foreach (var oAuthToken in session.ProviderOAuthAccess)
{
var authProvider = AuthService.GetAuthProvider(oAuthToken.Provider);
if (authProvider == null) continue;
var userAuthProvider = authProvider as OAuthProvider;
if (userAuthProvider != null)
{
userAuthProvider.LoadUserOAuthProvider(session, oAuthToken);
}
}
authRepo.SaveUserAuth(session);
var httpRes = authService.RequestContext.Get<IHttpResponse>();
if (httpRes != null)
{
httpRes.Cookies.AddPermanentCookie(HttpHeaders.XUserAuthId, session.UserAuthId);
}
OnSaveUserAuth(authService, session);
}
public virtual void OnSaveUserAuth(IServiceBase authService, IAuthSession session) { }
public virtual void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo)
{
var userSession = session as AuthUserSession;
if (userSession != null)
{
LoadUserAuthInfo(userSession, tokens, authInfo);
}
var authRepo = authService.TryResolve<IUserAuthRepository>();
if (authRepo != null)
{
if (tokens != null)
{
authInfo.ForEach((x, y) => tokens.Items[x] = y);
session.UserAuthId = authRepo.CreateOrMergeAuthSession(session, tokens);
}
//SaveUserAuth(authService, userSession, authRepo, tokens);
authRepo.LoadUserAuth(session, tokens);
foreach (var oAuthToken in session.ProviderOAuthAccess)
{
var authProvider = AuthService.GetAuthProvider(oAuthToken.Provider);
if (authProvider == null) continue;
var userAuthProvider = authProvider as OAuthProvider;
if (userAuthProvider != null)
{
userAuthProvider.LoadUserOAuthProvider(session, oAuthToken);
}
}
var httpRes = authService.RequestContext.Get<IHttpResponse>();
if (httpRes != null)
{
httpRes.Cookies.AddPermanentCookie(HttpHeaders.XUserAuthId, session.UserAuthId);
}
}
//OnSaveUserAuth(authService, session);
authService.SaveSession(session, SessionExpiry);
session.OnAuthenticated(authService, session, tokens, authInfo);
}
protected virtual void LoadUserAuthInfo(AuthUserSession userSession, IOAuthTokens tokens, Dictionary<string, string> authInfo) { }
protected static bool LoginMatchesSession(IAuthSession session, string userName)
{
if (userName == null) return false;
var isEmail = userName.Contains("@");
if (isEmail)
{
if (!userName.EqualsIgnoreCase(session.Email))
return false;
}
else
{
if (!userName.EqualsIgnoreCase(session.UserName))
return false;
}
return true;
}
public abstract bool IsAuthorized(IAuthSession session, IOAuthTokens tokens, Auth request = null);
public abstract object Authenticate(IServiceBase authService, IAuthSession session, Auth request);
public virtual void OnFailedAuthentication(IAuthSession session, IHttpRequest httpReq, IHttpResponse httpRes)
{
httpRes.StatusCode = (int)HttpStatusCode.Unauthorized;
httpRes.AddHeader(HttpHeaders.WwwAuthenticate, "{0} realm=\"{1}\"".Fmt(this.Provider, this.AuthRealm));
httpRes.EndRequest();
}
public static void HandleFailedAuth(IAuthProvider authProvider,
IAuthSession session, IHttpRequest httpReq, IHttpResponse httpRes)
{
var baseAuthProvider = authProvider as AuthProvider;
if (baseAuthProvider != null)
{
baseAuthProvider.OnFailedAuthentication(session, httpReq, httpRes);
return;
}
httpRes.StatusCode = (int)HttpStatusCode.Unauthorized;
httpRes.AddHeader(HttpHeaders.WwwAuthenticate, "{0} realm=\"{1}\""
.Fmt(authProvider.Provider, authProvider.AuthRealm));
httpRes.EndRequest();
}
}
public static class AuthConfigExtensions
{
public static bool IsAuthorizedSafe(this IAuthProvider authProvider, IAuthSession session, IOAuthTokens tokens)
{
return authProvider != null && authProvider.IsAuthorized(session, tokens);
}
}
}