public void Credential() { var pass = k.Security.RandomChars(8, false); var cred = new TestCredential(); cred.SetPassword(pass); if (!cred.IsValidPassword(pass)) { throw new Exception("the password is invalid"); } cred.User = "******"; if (cred.IsValidPassword(pass)) { throw new Exception("the password cannot be equals"); } var epass = cred.EPassword; cred.SetPassword(pass); if (cred.EPassword == epass) { throw new Exception("the password cannot be equals"); } var id = cred.Save(); var credClone = new TestCredential(id); if (cred.User != credClone.User || cred.EPassword != credClone.EPassword) { throw new Exception("Credential cannot load by id"); } }
public bool AddCredentials(string identity, string userName, string password, bool replaceExisting, IEnumerable <string> purposes) { var purposeList = purposes == null ? new List <string>() : purposes.Where(p => !string.IsNullOrEmpty(p)).ToList(); var existing = _credentials.FirstOrDefault(c => string.Equals(c.Username, userName, StringComparison.OrdinalIgnoreCase)); if (existing != null) { if (string.Equals(identity, existing.Identity, StringComparison.OrdinalIgnoreCase)) { existing.Password = password; existing.Purposes = purposeList; } else { return(false); } } if (replaceExisting) { _credentials = _credentials .Where(c => !string.Equals(c.Identity, identity, StringComparison.OrdinalIgnoreCase) || ReferenceEquals(c, existing)) .ToList(); } if (existing == null) { var newCredential = new TestCredential { Identity = identity, Username = userName, Password = password, Purposes = purposeList }; _credentials.Add(newCredential); } _identityDirectory.UpdateClaim(identity, new IdentityClaim { Name = ClaimNames.Username, Value = userName, Status = ClaimStatus.Verified }); return(true); }
public void TestGetAuthorizationCodeRequestUrl_StandardSettings_Passed() { var credential = new TestCredential(); var trSettings = new OAuth2TokenRequestSettings(); trSettings.RedirectUri = credential.RedirectUrls[0]; trSettings.Scopes.Add("test-scope1"); trSettings.Scopes.Add("test-scope2"); trSettings.State = "state999"; var oauth2 = new OAuth2Utility(new DummyNetUtility()); string url = oauth2.GetAuthorizationCodeRequestUrl(credential, trSettings); string expected = "https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=test-client-id&redirect_uri=http%3A%2F%2Flocalhost%3A1234&scope=test-scope1%20test-scope2&state=state999"; Assert.Equal(expected, url); }
public void GetAuthorizationCodeRequestUrl_CheckInvalidParams_Passed() { OAuth2Credential credential = null; OAuth2TokenRequestSettings trSettings = null; var oauth2 = new OAuth2Utility(new DummyNetUtility()); var argNullEx = Assert.Throws <ArgumentNullException>(() => oauth2.GetAuthorizationCodeRequestUrl(credential, trSettings)); Assert.Equal("credential", argNullEx.ParamName); credential = new TestCredential(); argNullEx = Assert.Throws <ArgumentNullException>(() => oauth2.GetAuthorizationCodeRequestUrl(credential, trSettings)); Assert.Equal("requestSettings", argNullEx.ParamName); trSettings = new OAuth2TokenRequestSettings(); var argEx = Assert.Throws <ArgumentException>(() => oauth2.GetAuthorizationCodeRequestUrl(credential, trSettings)); Assert.True(argEx.Message.Contains("RedirectUri must match one of the values in credential.RedirectUrls")); }
public async Task TestGetTokenInfoAsync_ByRefreshToken_Passed() { var credential = new TestCredential(); string refreshToken = "refreshToken"; var net = new DummyNetUtility(); var response = new DummyHttpWebResponse(HttpStatusCode.OK, @"{ ""access_token"": ""accessToken"", ""expires_in"": 123 }"); net.AddResponse(response); var oauth2 = new OAuth2Utility(net); DateTime timeBefore = DateTime.UtcNow; var tokenInfo = await oauth2.GetTokenInfoAsync(credential, refreshToken); DateTime timeAfter = DateTime.UtcNow; Assert.Equal("accessToken", tokenInfo.AccessToken); Assert.Equal(123, tokenInfo.ExpiresIn); Assert.True(timeBefore <= tokenInfo.IssuedTime && tokenInfo.IssuedTime <= timeAfter, $"Expect {timeBefore} <= Issued Time {tokenInfo.IssuedTime} <= {timeAfter}"); }
public void TestGetAuthorizationCodeRequestUrl_AllSettingsUsed_Passed() { var credential = new TestCredential(); var trSettings = new OAuth2TokenRequestSettings(); trSettings.IncludeGrantedScopes = true; trSettings.IsOnlineAccess = false; trSettings.LoginHint = "*****@*****.**"; trSettings.Prompts.Add(OAuth2TokenRequestPrompt.Consent); trSettings.Prompts.Add(OAuth2TokenRequestPrompt.SelectAccount); trSettings.RedirectUri = credential.RedirectUrls[0]; trSettings.Scopes.Add("test-scope"); trSettings.State = "state999"; trSettings.IsOnlineAccess = false; trSettings.IncludeGrantedScopes = true; var oauth2 = new OAuth2Utility(new DummyNetUtility()); string url = oauth2.GetAuthorizationCodeRequestUrl(credential, trSettings); string expected = "https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=test-client-id&redirect_uri=http%3A%2F%2Flocalhost%3A1234&scope=test-scope&state=state999&access_type=offline&prompt=consent%20select_account&[email protected]&include_granted_scopes=true"; Assert.Equal(expected, url); }