public override string BuildSignature(WebRequest webRequest, IOAuthConsumer consumer, IOAuthToken ioAuthToken) { if (consumer == null) { throw new ArgumentNullException(ERROR_CONSUMER_NULL); } if (ioAuthToken == null) { throw new ArgumentNullException(ERROR_TOKEN_NULL); } var requestUri = string.Format("{0}://{1}{2}", webRequest.RequestUri.Scheme, webRequest.RequestUri.Authority, webRequest.RequestUri.AbsolutePath); var request = WebRequest.Create(requestUri); request.Method = webRequest.Method; var key = string.Format(FORMAT_PARAMETER, OAuthParameter.UrlEncode(consumer.ConsumerSecret), OAuthParameter.UrlEncode(ioAuthToken.TokenSecret)); HashAlgorithm hashAlgorithm = new HMACSHA1(Encoding.UTF8.GetBytes(key)); var canonicalString = this.GetCanonicalString(request, consumer, ioAuthToken); var encoded = Encoding.UTF8.GetBytes(canonicalString); var result = Convert.ToBase64String(hashAlgorithm.ComputeHash(encoded)); return(result); }
protected string GetCanonicalString(WebRequest webRequest, IOAuthConsumer consumer, IOAuthToken ioAuthToken) { if (string.IsNullOrEmpty(webRequest.Method)) { throw new ArgumentNullException(ERROR_HTTP_METHOD); } var stringToSign = new StringBuilder(); stringToSign.Append(webRequest.Method.ToUpper().Trim()); stringToSign.Append(OAuthParameter.QUERYSTRING_SEPERATOR); //TODO: check if querystring should be part of it stringToSign.Append(OAuthParameter.UrlEncode(webRequest.RequestUri.AbsoluteUri)); stringToSign.Append(OAuthParameter.QUERYSTRING_SEPERATOR); if (RequestParameters[OAuthParameter.OAUTH_REALM] != null) { RequestParameters.Remove(OAuthParameter.OAUTH_REALM); } if (RequestParameters[OAuthParameter.OAUTH_SIGNATURE] != null) { RequestParameters.Remove(OAuthParameter.OAUTH_SIGNATURE); } //TODO: input correct parameters if (RequestParameters[OAuthParameter.OAUTH_SIGNATURE_METHOD] == null) { RequestParameters.Add(OAuthParameter.OAUTH_SIGNATURE_METHOD, GeneralUtil.SignatureMethodTypeToString(consumer.OAuthSignatureMethod)); } if (RequestParameters[OAuthParameter.OAUTH_CONSUMER_KEY] == null) { RequestParameters.Add(OAuthParameter.OAUTH_CONSUMER_KEY, consumer.ConsumerKey); } if (RequestParameters[OAuthParameter.OAUTH_VERSION] == null) { RequestParameters.Add(OAuthParameter.OAUTH_VERSION, GeneralUtil.OAuthVersionTypeToString(consumer.OAuthVersion)); } if (RequestParameters[OAuthParameter.OAUTH_TIMESTAMP] == null) { RequestParameters.Add(OAuthParameter.OAUTH_TIMESTAMP, GenerateTimeStamp()); } if (RequestParameters[OAuthParameter.OAUTH_NONCE] == null) { RequestParameters.Add(OAuthParameter.OAUTH_NONCE, GenerateNonce()); } if (RequestParameters[OAuthParameter.OAUTH_TOKEN] == null && !string.IsNullOrEmpty(ioAuthToken.TokenKey)) { RequestParameters.Add(OAuthParameter.OAUTH_TOKEN, ioAuthToken.TokenKey); } stringToSign.Append(OAuthParameter.UrlEncode(GetNormalizedParameterString(RequestParameters).Trim())); return(stringToSign.ToString()); }
public WebRequest SignRequest(WebRequest webRequest, IOAuthConsumer consumer, IOAuthToken ioAuthToken, NameValueCollection additionalParameters, out string oAuthQueryString) { oAuthQueryString = string.Empty; var requestUri = string.Format("{0}://{1}{2}", webRequest.RequestUri.Scheme, webRequest.RequestUri.Authority, webRequest.RequestUri.AbsolutePath); var signatureMethod = GetSignatureMethod(consumer.OAuthSignatureMethod); //add querystring var query = HttpUtility.ParseQueryString(webRequest.RequestUri.Query); signatureMethod.RequestParameters.Add(query); if (!string.IsNullOrEmpty(ioAuthToken.Openid)) { var openidParms = new NameValueCollection(); openidParms.Add(OAuthParameter.OPENID, ioAuthToken.Openid); signatureMethod.RequestParameters.Add(openidParms); } //add body content signatureMethod.RequestParameters.Add(additionalParameters); var signature = signatureMethod.BuildSignature(webRequest, consumer, ioAuthToken); signatureMethod.RequestParameters[OAuthParameter.OAUTH_SIGNATURE] = signature; switch (consumer.Scheme) { case AuthorizationSchemeType.Header: var oauthHeader = signatureMethod.ToOAuthHeader(); var request1 = WebRequest.Create(requestUri); request1.ContentType = webRequest.ContentType; request1.Headers.Add(OAuthConstants.AUTHORIZATION_HEADER, oauthHeader); request1.Method = webRequest.Method; return(request1); case AuthorizationSchemeType.QueryString: oAuthQueryString = signatureMethod.ToOAuthQueryString(); var request2 = WebRequest.Create(requestUri + "?" + oAuthQueryString); request2.ContentType = webRequest.ContentType; request2.Method = webRequest.Method; return(request2); case AuthorizationSchemeType.Body: throw new UnSupportedHttpMethodException(webRequest.Method); default: throw new ArgumentOutOfRangeException(ARG_SCHEME); } }
public override string BuildSignature(WebRequest webRequest, IOAuthConsumer consumer, IOAuthToken ioAuthToken) { if (consumer == null) { throw new ArgumentNullException(ERROR_CONSUMER_NULL); } if (ioAuthToken == null) { throw new ArgumentNullException(ERROR_TOKEN_NULL); } var result = string.Format(FORMAT_PARAMETER, OAuthParameter.UrlEncode(consumer.ConsumerSecret), OAuthParameter.UrlEncode(ioAuthToken.TokenSecret)); return result; }
public override string BuildSignature(WebRequest webRequest, IOAuthConsumer consumer, IOAuthToken ioAuthToken) { if (consumer == null) { throw new ArgumentNullException(ERROR_CONSUMER_NULL); } if (ioAuthToken == null) { throw new ArgumentNullException(ERROR_TOKEN_NULL); } var result = string.Format(FORMAT_PARAMETER, OAuthParameter.UrlEncode(consumer.ConsumerSecret), OAuthParameter.UrlEncode(ioAuthToken.TokenSecret)); return(result); }
public WebRequest SignRequest(WebRequest webRequest, IOAuthConsumer consumer, IOAuthToken ioAuthToken, NameValueCollection additionalParameters, out string oAuthQueryString) { oAuthQueryString = string.Empty; var requestUri = string.Format("{0}://{1}{2}", webRequest.RequestUri.Scheme, webRequest.RequestUri.Authority, webRequest.RequestUri.AbsolutePath); var signatureMethod = GetSignatureMethod(consumer.OAuthSignatureMethod); //add querystring var query = HttpUtility.ParseQueryString(webRequest.RequestUri.Query); signatureMethod.RequestParameters.Add(query); if (!string.IsNullOrEmpty(ioAuthToken.Openid)) { var openidParms = new NameValueCollection(); openidParms.Add(OAuthParameter.OPENID,ioAuthToken.Openid); signatureMethod.RequestParameters.Add(openidParms); } //add body content signatureMethod.RequestParameters.Add(additionalParameters); var signature = signatureMethod.BuildSignature(webRequest, consumer, ioAuthToken); signatureMethod.RequestParameters[OAuthParameter.OAUTH_SIGNATURE] = signature; switch (consumer.Scheme) { case AuthorizationSchemeType.Header: var oauthHeader = signatureMethod.ToOAuthHeader(); var request1 = WebRequest.Create(requestUri); request1.ContentType = webRequest.ContentType; request1.Headers.Add(OAuthConstants.AUTHORIZATION_HEADER, oauthHeader); request1.Method = webRequest.Method; return request1; case AuthorizationSchemeType.QueryString: oAuthQueryString = signatureMethod.ToOAuthQueryString(); var request2 = WebRequest.Create(requestUri + "?" + oAuthQueryString); request2.ContentType = webRequest.ContentType; request2.Method = webRequest.Method; return request2; case AuthorizationSchemeType.Body: throw new UnSupportedHttpMethodException(webRequest.Method); default: throw new ArgumentOutOfRangeException(ARG_SCHEME); } }
public override string BuildSignature(WebRequest webRequest, IOAuthConsumer consumer, IOAuthToken ioAuthToken) { if (consumer == null) { throw new ArgumentNullException(ERROR_CONSUMER_NULL); } if (ioAuthToken == null) { throw new ArgumentNullException(ERROR_TOKEN_NULL); } var key = string.Format(FORMAT_PARAMETER, OAuthParameter.UrlEncode(consumer.ConsumerSecret), OAuthParameter.UrlEncode(ioAuthToken.TokenSecret)); HashAlgorithm hashAlgorithm = new HMACSHA1(Encoding.UTF8.GetBytes(key)); var encoded = Encoding.UTF8.GetBytes(this.GetCanonicalString(webRequest, consumer, ioAuthToken)); var result = Convert.ToBase64String(hashAlgorithm.ComputeHash(encoded)); return result; }
public override string BuildSignature(WebRequest webRequest, IOAuthConsumer consumer, IOAuthToken ioAuthToken) { if (consumer == null) { throw new ArgumentNullException(ERROR_CONSUMER_NULL); } if (ioAuthToken == null) { throw new ArgumentNullException(ERROR_TOKEN_NULL); } var key = string.Format(FORMAT_PARAMETER, OAuthParameter.UrlEncode(consumer.ConsumerSecret), OAuthParameter.UrlEncode(ioAuthToken.TokenSecret)); HashAlgorithm hashAlgorithm = new HMACSHA1(Encoding.UTF8.GetBytes(key)); var encoded = Encoding.UTF8.GetBytes(this.GetCanonicalString(webRequest, consumer, ioAuthToken)); var result = Convert.ToBase64String(hashAlgorithm.ComputeHash(encoded)); return(result); }
private string CreateApiResourceRequest(IOAuthConsumer consumer, string relativePath) { // to request a protected resource via GET we: // 1. Construct the uri we want to access // 2. create an "OAuthContext" representing the request. // 3. Sign the context using our OAuth consumer implementation (calculates and sets the signature etc.) // 4. Generate a Uri from the OAuthContext context and return it. var absoloutePath = new Uri(Request.Uri, relativePath); var factory = new OAuthContextFactory(); OAuthContext context = factory.FromUri("GET", absoloutePath); consumer.SignContext(context, _testAccessToekn); return(context.GenerateUrl()); }
private string CreateApiResourceRequest(IOAuthConsumer consumer, string relativePath) { // to request a protected resource via GET we: // 1. Construct the uri we want to access // 2. create an "OAuthContext" representing the request. // 3. Sign the context using our OAuth consumer implementation (calculates and sets the signature etc.) // 4. Generate a Uri from the OAuthContext context and return it. var absoloutePath = new Uri(Request.Uri, relativePath); var factory = new OAuthContextFactory(); OAuthContext context = factory.FromUri("GET", absoloutePath); consumer.SignContext(context, _testAccessToekn); return context.GenerateUrl(); }
public override string BuildSignature(WebRequest webRequest, IOAuthConsumer consumer, IOAuthToken ioAuthToken) { if (consumer == null) { throw new ArgumentNullException(ERROR_CONSUMER_NULL); } if (ioAuthToken == null) { throw new ArgumentNullException(ERROR_TOKEN_NULL); } var requestUri = string.Format("{0}://{1}{2}", webRequest.RequestUri.Scheme, webRequest.RequestUri.Authority, webRequest.RequestUri.AbsolutePath); var request = WebRequest.Create(requestUri); request.Method = webRequest.Method; var key = string.Format(FORMAT_PARAMETER, OAuthParameter.UrlEncode(consumer.ConsumerSecret), OAuthParameter.UrlEncode(ioAuthToken.TokenSecret)); HashAlgorithm hashAlgorithm = new HMACSHA1(Encoding.UTF8.GetBytes(key)); var canonicalString = this.GetCanonicalString(request, consumer, ioAuthToken); var encoded = Encoding.UTF8.GetBytes(canonicalString); var result = Convert.ToBase64String(hashAlgorithm.ComputeHash(encoded)); return result; }
public AccessToken(IOAuthConsumer ioAuthConsumer, string oauthToken, string oauthTokenSecret) : base(ioAuthConsumer, oauthToken, oauthTokenSecret) { this.TokenType = OAuthTokenType.AccessToken; }
protected string GetCanonicalString(WebRequest webRequest, IOAuthConsumer consumer, IOAuthToken ioAuthToken) { if (string.IsNullOrEmpty(webRequest.Method)) { throw new ArgumentNullException(ERROR_HTTP_METHOD); } var stringToSign = new StringBuilder(); stringToSign.Append(webRequest.Method.ToUpper().Trim()); stringToSign.Append(OAuthParameter.QUERYSTRING_SEPERATOR); stringToSign.Append(OAuthParameter.UrlEncode(webRequest.RequestUri.AbsoluteUri)); stringToSign.Append(OAuthParameter.QUERYSTRING_SEPERATOR); if (RequestParameters[OAuthParameter.OAUTH_REALM] != null) RequestParameters.Remove(OAuthParameter.OAUTH_REALM); if (RequestParameters[OAuthParameter.OAUTH_SIGNATURE] != null) RequestParameters.Remove(OAuthParameter.OAUTH_SIGNATURE); if (RequestParameters[OAuthParameter.OAUTH_SIGNATURE_METHOD] == null) RequestParameters.Add(OAuthParameter.OAUTH_SIGNATURE_METHOD, GeneralUtil.SignatureMethodTypeToString(consumer.OAuthSignatureMethod)); if (RequestParameters[OAuthParameter.OAUTH_CONSUMER_KEY] == null) RequestParameters.Add(OAuthParameter.OAUTH_CONSUMER_KEY, consumer.ConsumerKey); if (RequestParameters[OAuthParameter.OAUTH_VERSION] == null) RequestParameters.Add(OAuthParameter.OAUTH_VERSION, GeneralUtil.OAuthVersionTypeToString(consumer.OAuthVersion)); if (RequestParameters[OAuthParameter.OAUTH_TIMESTAMP] == null) RequestParameters.Add(OAuthParameter.OAUTH_TIMESTAMP, GenerateTimeStamp()); if (RequestParameters[OAuthParameter.OAUTH_NONCE] == null) RequestParameters.Add(OAuthParameter.OAUTH_NONCE, GenerateNonce()); if (RequestParameters[OAuthParameter.OAUTH_TOKEN] == null && !string.IsNullOrEmpty(ioAuthToken.TokenKey)) RequestParameters.Add(OAuthParameter.OAUTH_TOKEN, ioAuthToken.TokenKey); stringToSign.Append(OAuthParameter.UrlEncode(GetNormalizedParameterString(RequestParameters).Trim())); return stringToSign.ToString(); }
public ConsumerToken(IOAuthConsumer ioAuthConsumer, string oauthToken, string oauthTokenSecret, string openId, bool?callbackConfirmed) : base(oauthToken, oauthTokenSecret, openId, callbackConfirmed) { this.CurrentConsumer = ioAuthConsumer; this.TokenType = OAuthTokenType.Unknown; }
public AccessToken(IOAuthConsumer ioAuthConsumer, IOAuthToken ioAuthToken) : base(ioAuthConsumer, ioAuthToken.TokenKey, ioAuthToken.TokenSecret) { this.TokenType = OAuthTokenType.AccessToken; }
public AccessToken(IOAuthConsumer ioAuthConsumer, IOAuthToken ioAuthToken, string openId) : base(ioAuthConsumer, ioAuthToken.TokenKey, ioAuthToken.TokenSecret, openId) { this.TokenType = OAuthTokenType.AccessToken; }
public AccessToken(IOAuthConsumer ioAuthConsumer, IOAuthToken ioAuthToken,string openId) : base(ioAuthConsumer, ioAuthToken.TokenKey,ioAuthToken.TokenSecret,openId) { this.TokenType = OAuthTokenType.AccessToken; }
public ConsumerToken(IOAuthConsumer ioAuthConsumer, string oauthToken, string oauthTokenSecret, string openId, bool? callbackConfirmed) : base(oauthToken, oauthTokenSecret,openId,callbackConfirmed) { this.CurrentConsumer = ioAuthConsumer; this.TokenType = OAuthTokenType.Unknown; }
//public RequestToken(IOAuthConsumer ioAuthConsumer, IOAuthToken ioAuthToken) // : this(ioAuthConsumer, ioAuthToken,null) //{ //} public RequestToken(IOAuthConsumer ioAuthConsumer, IOAuthToken ioAuthToken, string openId) : this(ioAuthConsumer, ioAuthToken, openId, null) { }
public WebRequest SignRequest(WebRequest webRequest, IOAuthConsumer consumer, IOAuthToken ioAuthToken) { return(this.SignRequest(webRequest, consumer, ioAuthToken)); }
public abstract string BuildSignature(WebRequest webRequest, IOAuthConsumer consumer, IOAuthToken ioAuthToken);
public AccessToken(IOAuthConsumer ioAuthConsumer, IOAuthToken ioAuthToken) : this(ioAuthConsumer, ioAuthToken, ioAuthToken.Openid) { }
public WebRequest SignRequest(WebRequest webRequest, IOAuthConsumer consumer, IOAuthToken ioAuthToken, NameValueCollection additionalParameters) { var requestUri = string.Format("{0}://{1}{2}", webRequest.RequestUri.Scheme, webRequest.RequestUri.Authority, webRequest.RequestUri.AbsolutePath); var signatureMethod = GetSignatureMethod(consumer.OAuthSignatureMethod); //add querystring var query = HttpUtility.ParseQueryString(webRequest.RequestUri.Query); signatureMethod.RequestParameters.Add(query); //add custom headers //? //add body content signatureMethod.RequestParameters.Add(additionalParameters); var signature = signatureMethod.BuildSignature(webRequest, consumer, ioAuthToken); signatureMethod.RequestParameters[OAuthParameter.OAUTH_SIGNATURE] = signature; switch (consumer.Scheme) { case AuthorizationSchemeType.Header: var oauthHeader = signatureMethod.ToOAuthHeader(); //var request1 = WebRequest.Create(webRequest.RequestUri.AbsoluteUri); var request1 = WebRequest.Create(requestUri); request1.ContentType = webRequest.ContentType; request1.Headers.Add(OAuthConstants.AUTHORIZATION_HEADER, oauthHeader); request1.Method = webRequest.Method; return(request1); case AuthorizationSchemeType.QueryString: var queryString = signatureMethod.ToOAuthQueryString(); //var sep = webRequest.RequestUri.AbsoluteUri.Contains("?") ? "&" : "?"; //var request2 = WebRequest.Create(webRequest.RequestUri.AbsoluteUri + sep + queryString); var request2 = WebRequest.Create(requestUri + "?" + queryString); request2.ContentType = webRequest.ContentType; request2.Method = webRequest.Method; return(request2); case AuthorizationSchemeType.Body: var supportedBodyRequestMethod = new List <HttpMethodType>() { HttpMethodType.PUT, HttpMethodType.POST }; var foundRequestMethod = supportedBodyRequestMethod.FindAll(httpMethod => webRequest.Method.Equals(httpMethod.ToString())); if (webRequest.ContentType == null) { throw new ContentTypeRequiredException("Content-Type wasn't speficied"); } var supportedContentType = new List <string>() { OAuthConstants.X_WWW_FORM_URLENCODED, OAuthConstants.MULTIPART_FORM_DATA }; var foundContentType = supportedContentType.FindAll(contentType => webRequest.ContentType.Contains(contentType)); if (foundRequestMethod.Count > 0 && foundContentType.Count > 0) { var request3 = WebRequest.Create(requestUri); request3.Method = webRequest.Method; request3.ContentType = webRequest.ContentType; //NOTE: this will cause any additional writes be invalid, try not to use this with other POST, PUT var body = signatureMethod.ToOAuthQueryString(); request3.ContentLength = body.Length; var requestStream = request3.GetRequestStream(); requestStream.Write(Encoding.UTF8.GetBytes(body), 0, body.Length); return(request3); } throw new UnSupportedHttpMethodException(webRequest.Method); default: throw new ArgumentOutOfRangeException(ARG_SCHEME); } }
public RequestToken(IOAuthConsumer ioAuthConsumer, IOAuthToken ioAuthToken,string openId, bool? callbackConfirmed) : base(ioAuthConsumer, ioAuthToken.TokenKey, ioAuthToken.TokenSecret,openId,callbackConfirmed) { this.TokenType = OAuthTokenType.RequestToken; }
//public RequestToken(IOAuthConsumer ioAuthConsumer, IOAuthToken ioAuthToken) // : this(ioAuthConsumer, ioAuthToken,null) //{ //} public RequestToken(IOAuthConsumer ioAuthConsumer, IOAuthToken ioAuthToken, string openId) : this(ioAuthConsumer, ioAuthToken,openId, null) { }
public RequestToken(IOAuthConsumer ioAuthConsumer, string oauthToken, string oauthTokenSecret) : base(ioAuthConsumer, oauthToken, oauthTokenSecret) { this.TokenType = OAuthTokenType.RequestToken; }
public ConsumerToken(IOAuthConsumer ioAuthConsumer, string oauthToken, string oauthTokenSecret, string openId) : this(ioAuthConsumer,oauthToken, oauthTokenSecret, openId, null) { }
public WebRequest SignRequest(WebRequest webRequest, IOAuthConsumer consumer, IOAuthToken ioAuthToken) { return this.SignRequest(webRequest, consumer, ioAuthToken); }
public RequestToken(IOAuthConsumer ioAuthConsumer, IOAuthToken ioAuthToken, string openId, bool?callbackConfirmed) : base(ioAuthConsumer, ioAuthToken.TokenKey, ioAuthToken.TokenSecret, openId, callbackConfirmed) { this.TokenType = OAuthTokenType.RequestToken; }
public ConsumerToken(IOAuthConsumer ioAuthConsumer, string oauthToken, string oauthTokenSecret, string openId) : this(ioAuthConsumer, oauthToken, oauthTokenSecret, openId, null) { }
public RequestToken(IOAuthConsumer ioAuthConsumer, IOAuthToken ioAuthToken) : base(ioAuthConsumer, ioAuthToken.TokenKey, ioAuthToken.TokenSecret) { this.TokenType = OAuthTokenType.RequestToken; }