public void CogniStreamerAuthenticationProvider_DefaultOnAuthenticatedImplementation_ShouldNotThrowException() { var options = new CogniStreamerAuthenticationOptions(); var context = new CogniStreamerAuthenticatedContext(this.owinContextMock.Object, options, this.user, string.Empty, string.Empty); Assert.That(() => this.providerUnderTest.Authenticated(context), Throws.Nothing); }
public void CogniStreamerAuthenticationProvider_CallAuthenticated_ShouldInvokeOnAuthenticated() { var callbacksMock = new Mock <IProviderCallbacks>(); var options = new CogniStreamerAuthenticationOptions(); var context = new CogniStreamerAuthenticatedContext(this.owinContextMock.Object, options, this.user, string.Empty, string.Empty); this.providerUnderTest.OnAuthenticated = callbacksMock.Object.OnAuthenticated; this.providerUnderTest.Authenticated(context); callbacksMock.Verify(x => x.OnAuthenticated(context), Times.Once); }
public void CogniStreamerAuthenticatedContext_PassArgumentsToConstructor_ShouldSetProperties() { var owinContext = new Mock <IOwinContext>().Object; var accessToken = Guid.NewGuid().ToString("N"); var user = JObject.Parse("{ a: 12345 }"); var options = new CogniStreamerAuthenticationOptions(); var context = new CogniStreamerAuthenticatedContext(owinContext, options, user, accessToken, "1400"); Assert.That(context.OwinContext, Is.EqualTo(owinContext)); Assert.That(context.Options, Is.EqualTo(options)); Assert.That(context.User["a"].Value <int>(), Is.EqualTo(12345)); Assert.That(context.AccessToken, Is.EqualTo(accessToken)); Assert.That(context.ExpiresIn, Is.EqualTo(TimeSpan.FromSeconds(1400))); }
public void CogniStreamerAuthenticatedContext_PassUserObjectToConstructor_ShouldSetProperties() { var owinContext = new Mock <IOwinContext>().Object; var user = JObject.Parse(@"{ id: 'dcec99ad-28e1-4194-a4e0-f22148963cc5', username: '******', firstName: 'Johnny', lastName: 'Cash', email: '*****@*****.**' }"); var options = new CogniStreamerAuthenticationOptions(); var context = new CogniStreamerAuthenticatedContext(owinContext, options, user, string.Empty, "3600"); Assert.That(context.Id, Is.EqualTo(new Guid("dcec99ad-28e1-4194-a4e0-f22148963cc5"))); Assert.That(context.Username, Is.EqualTo("cashj")); Assert.That(context.FirstName, Is.EqualTo("Johnny")); Assert.That(context.LastName, Is.EqualTo("Cash")); Assert.That(context.Email, Is.EqualTo("*****@*****.**")); }
protected override async Task <AuthenticationTicket> AuthenticateCoreAsync() { AuthenticationProperties properties = null; try { string code = null; string state = null; IList <string> values = this.Request.Query.GetValues("error"); if (values != null && values.Any()) { this.logger.WriteVerbose("Remote server returned an error: " + this.Request.QueryString); } values = this.Request.Query.GetValues("code"); if (values != null && values.Count == 1) { code = values[0]; } values = this.Request.Query.GetValues("state"); if (values != null && values.Count == 1) { state = values[0]; } properties = this.Options.StateDataFormat.Unprotect(state); if (properties == null) { this.logger.WriteVerbose("Remote server sends corrupt state"); return(null); } // OAuth2 10.12 CSRF if (!this.ValidateCorrelationId(properties, logger)) { return(new AuthenticationTicket(null, properties)); } if (code == null) { return(new AuthenticationTicket(null, properties)); } string requestPrefix = this.Request.Scheme + Uri.SchemeDelimiter + this.Request.Host; string redirectUri = requestPrefix + this.Request.PathBase + this.Options.CallbackPath; var tokenResponse = await this.RequestToken(code, redirectUri); var user = await this.RequestUserInfo(tokenResponse.AccessToken); var context = new CogniStreamerAuthenticatedContext(this.Context, this.Options, user, tokenResponse.AccessToken, tokenResponse.ExpiresIn); context.Identity = new ClaimsIdentity(this.Options.AuthenticationType, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType); if (context.Id.HasValue) { context.Identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, context.Id.Value.ToString(), XmlSchemaString, this.Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.Username)) { context.Identity.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, context.Username, XmlSchemaString, this.Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.FirstName)) { context.Identity.AddClaim(new Claim(ClaimTypes.GivenName, context.FirstName, XmlSchemaString, this.Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.LastName)) { context.Identity.AddClaim(new Claim(ClaimTypes.Surname, context.LastName, XmlSchemaString, this.Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.Email)) { context.Identity.AddClaim(new Claim(ClaimTypes.Email, context.Email, XmlSchemaString, this.Options.AuthenticationType)); } context.Properties = properties; await this.Options.Provider.Authenticated(context); return(new AuthenticationTicket(context.Identity, context.Properties)); } catch (Exception ex) { this.logger.WriteError("Authentication failed", ex); return(new AuthenticationTicket(null, properties)); } }