Created
July 28, 2012 18:23
-
-
Save Alxandr/3194281 to your computer and use it in GitHub Desktop.
WebAPIResourceServer - so, yeah.. Have fun -.-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Collections.Specialized; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Security.Cryptography.X509Certificates; | |
using System.Security.Principal; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Web; | |
using DotNetOpenAuth.OAuth2; | |
namespace Samples.OAuth2 | |
{ | |
class WebAPIResourceServer : ResourceServer | |
{ | |
public WebAPIResourceServer(IAccessTokenAnalyzer accessTokenAnalyzer) | |
: base(accessTokenAnalyzer) | |
{ | |
} | |
public async Task<IPrincipal> GetPrincipalAsync(HttpRequestMessage request, params string[] requiredScopes) | |
{ | |
try | |
{ | |
return base.GetPrincipal(await HttpRequestMessageWrapper.FromHttpRequestMessage(request), requiredScopes); | |
} | |
catch(Exception e) | |
{ | |
throw; | |
} | |
} | |
} | |
class HttpRequestMessageWrapper : HttpRequestBase | |
{ | |
string root; | |
HttpMethod method; | |
Uri uri; | |
Version version; | |
HttpRequestHeaders headers; | |
NameValueCollection formData; | |
X509Certificate2 clientCert; | |
public HttpRequestMessageWrapper(string root, HttpMethod method, Uri uri, Version version, X509Certificate2 clientCert, HttpRequestHeaders headers, NameValueCollection formData) | |
: base() | |
{ | |
this.root = root; | |
this.method = method; | |
this.uri = uri; | |
this.version = version; | |
this.clientCert = clientCert; | |
this.headers = headers; | |
this.formData = formData; | |
} | |
public override string[] AcceptTypes | |
{ | |
get | |
{ | |
return headers.Accept.Select(a => a.ToString()).ToArray(); | |
} | |
} | |
public override string AnonymousID | |
{ | |
get | |
{ | |
throw new NotImplementedException("AnonymousID"); | |
} | |
} | |
public override string ApplicationPath | |
{ | |
get | |
{ | |
return root; | |
} | |
} | |
public override HttpBrowserCapabilitiesBase Browser | |
{ | |
get | |
{ | |
throw new NotImplementedException("Browser"); | |
} | |
} | |
public override HttpClientCertificate ClientCertificate | |
{ | |
get | |
{ | |
throw new NotImplementedException("ClientCertificate"); | |
} | |
} | |
public override Encoding ContentEncoding | |
{ | |
get | |
{ | |
throw new NotImplementedException("ContentEncoding"); | |
} | |
set | |
{ | |
throw new NotImplementedException("ContentEncoding"); | |
} | |
} | |
public override int ContentLength | |
{ | |
get | |
{ | |
throw new NotImplementedException("ContentLength"); | |
} | |
} | |
public override string ContentType | |
{ | |
get | |
{ | |
throw new NotImplementedException("ContentType"); | |
} | |
set | |
{ | |
throw new NotImplementedException("ContentType"); | |
} | |
} | |
HttpCookieCollection cookies; | |
public override HttpCookieCollection Cookies | |
{ | |
get | |
{ | |
if (cookies == null) | |
{ | |
HttpCookieCollection tmp = new HttpCookieCollection(); | |
foreach (var cookie in headers.GetCookies().SelectMany(c => c.Cookies)) | |
tmp.Add(new HttpCookie(cookie.Name, cookie.Value)); | |
Interlocked.CompareExchange<HttpCookieCollection>(ref cookies, tmp, null); | |
} | |
return cookies; | |
} | |
} | |
public override string CurrentExecutionFilePath | |
{ | |
get | |
{ | |
throw new NotImplementedException("CurrentExecutionFilePath"); | |
} | |
} | |
public override string CurrentExecutionFilePathExtension | |
{ | |
get | |
{ | |
throw new NotImplementedException("CurrentExecutionFilePathExtension"); | |
} | |
} | |
public override string FilePath | |
{ | |
get | |
{ | |
throw new NotImplementedException("FilePath"); | |
} | |
} | |
public override HttpFileCollectionBase Files | |
{ | |
get | |
{ | |
throw new NotImplementedException("Files"); | |
} | |
} | |
public override System.IO.Stream Filter | |
{ | |
get | |
{ | |
throw new NotImplementedException("Filter"); | |
} | |
set | |
{ | |
throw new NotImplementedException("Filter"); | |
} | |
} | |
public override NameValueCollection Form | |
{ | |
get | |
{ | |
return formData; | |
} | |
} | |
NameValueCollection _headers; | |
public override NameValueCollection Headers | |
{ | |
get | |
{ | |
if (_headers == null) | |
{ | |
var tmp = new NameValueCollection(); | |
foreach (var h in headers) | |
foreach (var v in h.Value) | |
tmp.Add(h.Key, v); | |
Interlocked.CompareExchange<NameValueCollection>(ref _headers, tmp, null); | |
} | |
return _headers; | |
} | |
} | |
public override System.Security.Authentication.ExtendedProtection.ChannelBinding HttpChannelBinding | |
{ | |
get | |
{ | |
throw new NotImplementedException("HttpChannelBinding"); | |
} | |
} | |
public override string HttpMethod | |
{ | |
get | |
{ | |
return method.ToString(); | |
} | |
} | |
public override System.IO.Stream InputStream | |
{ | |
get | |
{ | |
throw new NotImplementedException("InputStream"); | |
} | |
} | |
public override bool IsAuthenticated | |
{ | |
get | |
{ | |
throw new NotImplementedException("IsAuthenticated"); | |
} | |
} | |
public override bool IsLocal | |
{ | |
get | |
{ | |
throw new NotImplementedException("IsLocal"); | |
} | |
} | |
public override bool IsSecureConnection | |
{ | |
get | |
{ | |
throw new NotImplementedException("IsSecureConnection"); | |
} | |
} | |
public override string this[string key] | |
{ | |
get | |
{ | |
return base[key]; // needed? needs to override? | |
} | |
} | |
public override WindowsIdentity LogonUserIdentity | |
{ | |
get | |
{ | |
throw new NotImplementedException("LogonUserIdentity"); | |
} | |
} | |
public override NameValueCollection Params | |
{ | |
get | |
{ | |
throw new NotImplementedException("Params"); | |
} | |
} | |
public override string Path | |
{ | |
get | |
{ | |
return uri.LocalPath; | |
} | |
} | |
public override string PathInfo | |
{ | |
get | |
{ | |
throw new NotImplementedException("PathInfo"); | |
} | |
} | |
public override string PhysicalApplicationPath | |
{ | |
get | |
{ | |
throw new NotImplementedException("PhysicalApplicationPath"); | |
} | |
} | |
public override string PhysicalPath | |
{ | |
get | |
{ | |
throw new NotImplementedException("PhysicalPath"); | |
} | |
} | |
NameValueCollection query; | |
public override NameValueCollection QueryString | |
{ | |
get | |
{ | |
if (query == null) | |
{ | |
Interlocked.CompareExchange<NameValueCollection>(ref query, HttpUtility.ParseQueryString(uri.Query), null); | |
} | |
return query; | |
} | |
} | |
public override ReadEntityBodyMode ReadEntityBodyMode | |
{ | |
get | |
{ | |
throw new NotImplementedException("ReadEntityBodyMode"); | |
} | |
} | |
public override System.Web.Routing.RequestContext RequestContext | |
{ | |
get | |
{ | |
throw new InvalidOperationException("RequestContext"); | |
} | |
set | |
{ | |
throw new InvalidOperationException("RequestContext"); | |
} | |
} | |
public override string RequestType | |
{ | |
get | |
{ | |
return HttpMethod; | |
} | |
set | |
{ | |
throw new NotImplementedException("RequestType"); | |
} | |
} | |
public override NameValueCollection ServerVariables | |
{ | |
get | |
{ | |
return new NameValueCollection(); | |
//throw new NotImplementedException("ServerVariables"); | |
} | |
} | |
public override CancellationToken TimedOutToken | |
{ | |
get | |
{ | |
throw new NotImplementedException("TimedOutToken"); | |
} | |
} | |
public override int TotalBytes | |
{ | |
get | |
{ | |
throw new NotImplementedException("TotalBytes"); | |
} | |
} | |
public override UnvalidatedRequestValuesBase Unvalidated | |
{ | |
get | |
{ | |
throw new NotImplementedException("Unvalidated"); | |
} | |
} | |
public override Uri Url | |
{ | |
get | |
{ | |
return uri; | |
} | |
} | |
public override Uri UrlReferrer | |
{ | |
get | |
{ | |
throw new NotImplementedException("UrlReferrer"); | |
} | |
} | |
public override string UserAgent | |
{ | |
get | |
{ | |
throw new NotImplementedException("UserAgent"); | |
} | |
} | |
public override string UserHostAddress | |
{ | |
get | |
{ | |
throw new NotImplementedException("UserHostAddress"); | |
} | |
} | |
public override string UserHostName | |
{ | |
get | |
{ | |
throw new NotImplementedException("UserHostName"); | |
} | |
} | |
public override string[] UserLanguages | |
{ | |
get | |
{ | |
throw new NotImplementedException("UserLanguages"); | |
} | |
} | |
public override string RawUrl | |
{ | |
get | |
{ | |
return uri.ToString(); | |
//throw new NotImplementedException("RawUrl"); | |
} | |
} | |
public static async Task<HttpRequestMessageWrapper> FromHttpRequestMessage(HttpRequestMessage message) | |
{ | |
var headers = message.Headers; | |
var method = message.Method; | |
var uri = message.RequestUri; | |
var httpVersion = message.Version; | |
var root = message.GetConfiguration().VirtualPathRoot; | |
var cert = message.GetClientCertificate(); | |
if (message.Content.IsFormData()) | |
{ | |
var formData = await message.Content.ReadAsFormDataAsync(); | |
return new HttpRequestMessageWrapper(root, method, uri, httpVersion, cert, headers, formData); | |
} | |
else if (message.Content.IsMimeMultipartContent()) | |
{ | |
var mimeData = await message.Content.ReadAsMultipartAsync(); | |
await mimeData.ExecutePostProcessingAsync(); | |
NameValueCollection formData = new NameValueCollection(); | |
foreach (var md in mimeData.Contents) | |
{ | |
if (md.IsFormData()) | |
{ | |
formData = await md.ReadAsFormDataAsync(); | |
break; | |
} | |
} | |
return new HttpRequestMessageWrapper(root, method, uri, httpVersion, cert, headers, formData); | |
} | |
else | |
{ | |
return new HttpRequestMessageWrapper(root, method, uri, httpVersion, cert, headers, new NameValueCollection()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment