Exemplo n.º 1
0
        private IHttpServer SetupJwkServer(
            out string host,
            bool hasJwk = true,
            HttpStatusCode jwkStatusCode = HttpStatusCode.OK
            )
        {
            IHttpServer jwksServer = HttpMockFactory.Create(out host);

            jwksServer.Stub(
                x => x.Get(GOOD_PATH + JWKS_PATH)
                ).Return(GOOD_JSON).OK();

            jwksServer.Stub(
                x => x.Get(BAD_PATH)
                ).Return(GOOD_JSON).WithStatus(HttpStatusCode.InternalServerError);

            jwksServer.Stub(
                x => x.Get(HTML_PATH)
                ).Return(HTML).WithStatus(HttpStatusCode.OK);

            jwksServer
            .Stub(x => x.Get(GOOD_PATH + JWK_PATH + GOOD_JWK_ID))
            .Return(hasJwk ? GOOD_JWK : "")
            .WithStatus(jwkStatusCode);

            jwksServer
            .Stub(x => x.Get(GOOD_PATH + JWK_PATH + GOOD_JWK_ID_STRING))
            .Return(hasJwk ? GOOD_JWK_STRING : "")
            .WithStatus(jwkStatusCode);

            return(jwksServer);
        }
        public AuthServiceMock(KeyType keyType = KeyType.RSA)
        {
            m_server = HttpMockFactory.Create(out m_host);

#pragma warning disable 618
            m_publicKeyDataProvider = PublicKeyDataProviderFactory.CreateInternal(new InMemoryPublicKeyDataProvider());
#pragma warning restore 618

            TimeSpan keyLifetime       = TimeSpan.FromDays(365);
            TimeSpan keyRotationPeriod = TimeSpan.FromDays(182);

            switch (keyType)
            {
            case KeyType.ECDSA_P256:
            case KeyType.ECDSA_P384:
            case KeyType.ECDSA_P521: {
                CngAlgorithm curve;
                switch (keyType)
                {
                case KeyType.ECDSA_P521:
                    curve = CngAlgorithm.ECDsaP521;
                    break;

                case KeyType.ECDSA_P384:
                    curve = CngAlgorithm.ECDsaP384;
                    break;

                case KeyType.ECDSA_P256:
                default:
                    curve = CngAlgorithm.ECDsaP256;
                    break;
                }

                m_privateKeyProvider = EcDsaPrivateKeyProvider
                                       .Factory
                                       .Create(
                    m_publicKeyDataProvider,
                    keyLifetime,
                    keyRotationPeriod,
                    curve
                    );
                break;
            }

            case KeyType.RSA:
            default: {
                m_privateKeyProvider = RsaPrivateKeyProvider
                                       .Factory
                                       .Create(
                    m_publicKeyDataProvider,
                    keyLifetime,
                    keyRotationPeriod
                    );
                break;
            }
            }

            m_tokenSigner = new TokenSigner(m_privateKeyProvider);
        }
        public void TestFixtureSetUp()
        {
            m_jwksServer = HttpMockFactory.Create(out m_host);

            m_jwksServer.Stub(
                x => x.Get(GOOD_PATH + JWKS_PATH)
                ).Return(GOOD_JSON).OK();

            m_jwksServer.Stub(
                x => x.Get(BAD_PATH)
                ).Return(GOOD_JSON).WithStatus(HttpStatusCode.InternalServerError);

            m_jwksServer.Stub(
                x => x.Get(HTML_PATH + JWKS_PATH)
                ).Return(HTML).WithStatus(HttpStatusCode.OK);
        }
            public void ValidateAsync_GoodSignature_Succeeds_WebCrypto(string jwk, string token)
            {
                string host;
                var    mockServer = HttpMockFactory.Create(out host);

                mockServer
                .Stub(r => r.Get("/.well-known/jwks"))
                .Return(@"{""keys"":[" + jwk + "]}")
                .OK();

                // We expect these to be expired because they are static
                // The rest of the validation should have otherwise proceeded swimmingly
                Assert.Throws <ExpiredTokenException>(() =>
                                                      AccessTokenValidatorFactory
                                                      .CreateRemoteValidator(new HttpClient(), new Uri(host))
                                                      .ValidateAsync(token)
                                                      .SafeAsync().GetAwaiter().GetResult()
                                                      );
            }
Exemplo n.º 5
0
        private void SetUp(out Uri host, out string token, out Guid id)
        {
            string hostStr;
            var    server = HttpMockFactory.Create(out hostStr);

            host = new Uri(hostStr);

#pragma warning disable 618
            IPublicKeyDataProvider publicKeyDataProvider = new InMemoryPublicKeyDataProvider();
#pragma warning restore 618
            ITokenSigner tokenSigner = GetTokenSigner(publicKeyDataProvider);

            token = tokenSigner
                    .SignAsync(new UnsignedToken(
                                   "some issuer",
                                   "some audience",
                                   new List <Claim>(),
                                   DateTime.Now,
                                   DateTime.Now + TimeSpan.FromDays(1)
                                   ))
                    .SafeAsync()
                    .GetAwaiter()
                    .GetResult();

            var jwk = publicKeyDataProvider
                      .GetAllAsync()
                      .SafeAsync()
                      .GetAwaiter()
                      .GetResult()
                      .First();

            id = jwk.Id;

            server
            .Stub(r => r.Get("/.well-known/jwks"))
            .Return(JsonConvert.SerializeObject(new { keys = new object[] { jwk.ToJwkDto() } }))
            .OK();
        }