public static bool IsValid(this IAuthCredentials credentials)
        {
            switch (credentials)
            {
            case ICredentialInfoEx credentialInfoEx:
                return(credentialInfoEx.IsValid());

            case ICredentialInfo credentialInfo:
                return(credentialInfo.IsValid());

            case ICredentialUser credentialUser:
                return(credentialUser.IsValid());

            case ICredentialToken credentialToken:
                return(credentialToken.IsValid());

            case null:
                //throw new ArgumentNullException(nameof(credentials));
                return(false);

            default:
                //throw new ArgumentException(
                //    FormattableString.Invariant(
                //        $"Specified {nameof(IAuthCredentials)} implementation not supported."
                //    ),
                //    nameof(credentials)
                //);
                return(false);
            }
        }
示例#2
0
        public OAuth(IOAuthSigning signing, IAuthCredentials auth, HttpMethod method, string baseUri, IDictionary <string, string> requestParameters, string nonce, string timestamp)
        {
            if (string.IsNullOrEmpty(baseUri))
            {
                throw new ArgumentNullException("baseUri");
            }

            if (requestParameters == null)
            {
                throw new ArgumentNullException("requestParameters");
            }

            if (requestParameters.Count == 0)
            {
                throw new ArgumentException("Invalid number of request parameters", "requestParameters");
            }

            if (string.IsNullOrEmpty(nonce))
            {
                throw new ArgumentNullException("baseUri");
            }

            if (string.IsNullOrEmpty(timestamp))
            {
                throw new ArgumentNullException("timestamp");
            }

            _signing           = signing ?? throw new ArgumentNullException("signing");
            _auth              = auth ?? throw new ArgumentNullException("auth");
            _method            = method;
            _baseUri           = baseUri;
            _requestParameters = requestParameters;
            _nonce             = nonce;
            _timestamp         = timestamp;
        }
        public IAuthResult Authenticate(IAuthCredentials theCredentials)
        {
            if (theCredentials.Scheme == Scheme.Form)
            {
                return(doLookUp(theCredentials.Username, theCredentials.Password));
            }
            else if (theCredentials.Scheme == Scheme.Basic && theCredentials.HttpRequestHeaders != null) // Basic
            {
                KeyValuePair <string, IEnumerable <string> > AuthorizationHeader = theCredentials.HttpRequestHeaders.FirstOrDefault(Header => Header.Key == c_HttpRequestHeaders[0]);

                if (AuthorizationHeader.Equals(new KeyValuePair <string, IEnumerable <string> >()))
                {
                    return(new AuthResult(false, string.Empty, "Missing Authorization Header"));
                }

                if (AuthorizationHeader.Value != null && AuthorizationHeader.Value.Count() > 0)
                {
                    string EncodedValue      = AuthorizationHeader.Value.First();
                    string DecodedValue      = Encoding.UTF8.GetString(Convert.FromBase64String(EncodedValue));
                    string DomainAndUsername = DecodedValue.Substring(0, DecodedValue.IndexOf(":"));
                    string Password          = DecodedValue.Substring(DecodedValue.IndexOf(":") + 1);

                    int IndexOfSlash = DomainAndUsername.IndexOf("\\");

                    string Domain;
                    string Username;

                    if (IndexOfSlash != -1)
                    {
                        Domain   = DomainAndUsername.Substring(0, IndexOfSlash);
                        Username = DomainAndUsername.Substring(IndexOfSlash + 1);
                    }
                    else
                    {
                        return(new AuthResult(false, string.Empty, "Missing Domain"));
                    }

                    if (Domain.Equals(m_Domains[0], StringComparison.OrdinalIgnoreCase))
                    {
                        return(doLookUp(Username, Password));
                    }
                    else
                    {
                        return(new AuthResult(false, string.Empty, "Domain mismatch"));
                    }
                }
                else
                {
                    return(new AuthResult(false, string.Empty, "Missing value in Authorization Header"));
                }
            }
            else
            {
                return(new AuthResult(false, string.Empty, "Not a supported Scheme"));
            }
        }
 public TwitterHttpClient(IHttpClient httpClient, IAuthCredentials auth)
 {
     _httpClient             = httpClient ?? throw new ArgumentNullException("httpClient");
     _auth                   = auth ?? throw new ArgumentNullException("auth");
     httpClient.PreResponse += (uri, method, responseHeaders, data) => OAuth.SetAuthorisationHeader(_auth, uri, method, responseHeaders, data);
 }
        public IAuthResult Authenticate(IAuthCredentials theCredentials)
        {
            AuthLocalFile AuthFileRoot = null;

            if (!File.Exists(m_AuthFile))
            {
                return(new AuthResult {
                    Result = false, Message = "System Error: Lookup file does not exist"
                });
            }

            try
            {
                AuthFileRoot = ReadFile();
            }
            catch (InvalidOperationException ex)
            {
                return(new AuthResult {
                    Result = false, Message = "System Error: " + ex.Message
                });
            }
            catch (FileNotFoundException ex)
            {
                return(new AuthResult {
                    Result = false, Message = "System Error: " + ex.Message
                });
            }
            catch (Exception ex)
            {
                return(new AuthResult {
                    Result = false, Message = "System Error: " + ex.Message
                });
            }

            if (theCredentials.Scheme == Scheme.Form)
            {
                if (AuthFileRoot.Users == null)
                {
                    return(new AuthResult {
                        Result = false, Message = "System Error: Lookup file contains no users"
                    });
                }

                var UserSearch = AuthFileRoot.Users.FirstOrDefault(u => u.Username.Equals(theCredentials.Username, StringComparison.OrdinalIgnoreCase));

                if (UserSearch == null)
                {
                    return(new AuthResult {
                        Result = false, Message = "Username or Password is incorrect"
                    });
                }
                else
                {
                    if (!UserSearch.Enabled)
                    {
                        return(new AuthResult {
                            Result = false, Message = "User is disabled"
                        });
                    }
                    else
                    {
                        if (UserSearch.Password != theCredentials.Password)
                        {
                            return(new AuthResult {
                                Result = false, Message = "Username or Password is incorrect"
                            });
                        }
                        else
                        {
                            return(new AuthResult {
                                Result = true, Identity = c_Domains[0] + "\\" + UserSearch.Username, Message = "OK"
                            });
                        }
                    }
                }
            }
            else if (theCredentials.Scheme == Scheme.Basic && theCredentials.HttpRequestHeaders != null)
            {
                KeyValuePair <string, IEnumerable <string> > AuthorizationHeader = theCredentials.HttpRequestHeaders.FirstOrDefault(Header => Header.Key == c_HttpRequestHeaders[0]);

                if (AuthorizationHeader.Equals(new KeyValuePair <string, IEnumerable <string> >()))
                {
                    return(new AuthResult {
                        Result = false, Message = "Missing Authorization Header"
                    });
                }

                if (AuthorizationHeader.Value != null && AuthorizationHeader.Value.Count() > 0)
                {
                    string EncodedValue      = AuthorizationHeader.Value.First();
                    string DecodedValue      = Encoding.UTF8.GetString(Convert.FromBase64String(EncodedValue));
                    string DomainAndUsername = DecodedValue.Substring(0, DecodedValue.IndexOf(":"));
                    string Password          = DecodedValue.Substring(DecodedValue.IndexOf(":") + 1);

                    int IndexOfSlash = DomainAndUsername.IndexOf("\\");

                    string Domain;
                    string Username;

                    if (IndexOfSlash != -1)
                    {
                        Domain   = DomainAndUsername.Substring(0, IndexOfSlash);
                        Username = DomainAndUsername.Substring(IndexOfSlash + 1);
                    }
                    else
                    {
                        return(new AuthResult {
                            Result = false, Message = "Missing Domain"
                        });
                    }

                    if (Domain.Equals(c_Domains[0], StringComparison.OrdinalIgnoreCase))
                    {
                        return(doLookUp(AuthFileRoot, Username, Password));
                    }
                    else
                    {
                        return(new AuthResult {
                            Result = false, Message = "Domain mismatch"
                        });
                    }
                }
                else
                {
                    return(new AuthResult {
                        Result = false, Message = "Missing value in Authorization Header"
                    });
                }
            }
            else if (theCredentials.Scheme == Scheme.BearerToken && theCredentials.HttpRequestHeaders != null)
            {
                KeyValuePair <string, IEnumerable <string> > AuthorizationHeader = theCredentials.HttpRequestHeaders.FirstOrDefault(Header => Header.Key == c_HttpRequestHeaders[0]);

                if (AuthorizationHeader.Equals(new KeyValuePair <string, IEnumerable <string> >()))
                {
                    return(new AuthResult {
                        Result = false, Message = "Missing Authorization Header"
                    });
                }

                if (AuthorizationHeader.Value != null && AuthorizationHeader.Value.Count() > 0)
                {
                    return(doLookUp(AuthFileRoot, AuthorizationHeader.Value.First()));
                }
                else
                {
                    return(new AuthResult {
                        Result = false, Message = "Missing value in Authorization Header"
                    });
                }
            }
            else
            {
                return(new AuthResult {
                    Result = false, Message = "Not a supported Scheme"
                });
            }
        }
 public DefaultAuthProvider(IAuthCredentials authCredentials)
 {
     _authCredentials = authCredentials ?? throw new ArgumentNullException(nameof(authCredentials));
 }
 public TwitterPostWrapper(string title, string text,
                           List <Attachement> attachements, Poster poster, TimeSpan delay, IAuthCredentials authCredentials)
 {
     if (title == null)
     {
         RaisePostingFinished(false, new ArgumentNullException(nameof(title)));
     }
     if (text == null)
     {
         RaisePostingFinished(false, new ArgumentNullException(nameof(text)));
     }
     if (attachements == null)
     {
         RaisePostingFinished(false, new ArgumentNullException(nameof(attachements)));
     }
     if (poster == null)
     {
         RaisePostingFinished(false, new ArgumentNullException(nameof(poster)));
     }
     if (authCredentials == null)
     {
         RaisePostingFinished(false, new ArgumentNullException(nameof(authCredentials)));
     }
     this.poster          = poster;
     this.authCredentials = authCredentials;
     this.Post            = new Post(title, text, attachements);
     this.postDelayTimer  = new PostDelayTimer(delay, TryPost);
 }
示例#8
0
 public OAuth(IAuthCredentials auth, HttpMethod method, string baseUri, IDictionary <string, string> requestParameters)
     : this(auth, method, baseUri, requestParameters, NewNonce(), NewTimestamp())
 {
 }
示例#9
0
 public OAuth(IAuthCredentials auth, HttpMethod method, string baseUri, IDictionary <string, string> requestParameters, string nonce, string timestamp)
     : this(new HMACSigning(), auth, method, baseUri, requestParameters, nonce, timestamp)
 {
 }
示例#10
0
 public static void SetAuthorisationHeader(IAuthCredentials auth, Uri uri, HttpMethod method, NameValueCollection responseHeaders, IDictionary <string, string> data = null)
 {
     responseHeaders = responseHeaders ?? new NameValueCollection(0);
     new OAuth(auth, method, uri.ToString(), data).SetAuthorisationHeader(responseHeaders);
 }
示例#11
0
 public static ITwitter Create(IAuthCredentials authCredentials)
 {
     return(Create(new DefaultConfiguration(authCredentials)));
 }
示例#12
0
 public DefaultConfiguration(IAuthCredentials authCredentials)
 {
     Provider = new DefaultAuthProvider(authCredentials);
 }