コード例 #1
0
 public void WhenExtractJwsPayloadFromAuthorizationRequest(string name)
 {
     var jws = WebApiSteps.ParseValue(name, _scenarioContext).ToString();
     var jwsGenerator = new JwsGeneratorFactory().BuildJwsGenerator();
     _scenarioContext.Set(jwsGenerator.ExtractPayload(jws), "tokenPayload");
     _scenarioContext.Set(jwsGenerator.ExtractHeader(jws), "jwsHeader");
 }
コード例 #2
0
        public void WhenExtractJwsPayloadFromAuthorizationRequest(string name)
        {
            var jws          = Parse(name).ToString();
            var jwsGenerator = new JwsGeneratorFactory().BuildJwsGenerator();

            _scenarioContext.Set(JObject.Parse(JsonConvert.SerializeObject(jwsGenerator.ExtractPayload(jws))), "tokenPayload");
            _scenarioContext.Set(jwsGenerator.ExtractHeader(jws), "jwsHeader");
        }
コード例 #3
0
        public async Task <IActionResult> Authenticate(AuthenticateViewModel authenticateViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(authenticateViewModel));
            }

            using (var httpClient = new HttpClient())
            {
                var request = new HttpRequestMessage
                {
                    RequestUri = new Uri($"{TraditionalWebsiteConstants.BASE_OPENID_URL}/token"),
                    Method     = HttpMethod.Post,
                    Content    = new FormUrlEncodedContent(new Dictionary <string, string>
                    {
                        { "client_id", TraditionalWebsiteConstants.CLIENT_ID },
                        { "client_secret", TraditionalWebsiteConstants.CLIENT_SECRET },
                        { "grant_type", "password" },
                        { "username", authenticateViewModel.Login },
                        { "password", authenticateViewModel.Password },
                        { "scope", "openid profile" }
                    })
                };
                var httpResult = await httpClient.SendAsync(request);

                if (!httpResult.IsSuccessStatusCode)
                {
                    ModelState.AddModelError("invalid_credentials", "Bad credentials");
                    return(View(authenticateViewModel));
                }

                var json = await httpResult.Content.ReadAsStringAsync();

                var jObj = JObject.Parse(json);
                var jwsGeneratorFactory = new JwsGeneratorFactory();
                var idToken             = jObj["id_token"].ToString();
                var jwsPayload          = jwsGeneratorFactory.BuildJwsGenerator().ExtractPayload(idToken);
                var claimsPrincipal     = BuildClaimsPrincipal(jwsPayload);
                var tokens = new List <AuthenticationToken>
                {
                    new AuthenticationToken {
                        Name = "id_token", Value = idToken
                    }
                };
                var authProperties = new AuthenticationProperties();
                authProperties.StoreTokens(tokens);
                await HttpContext.SignInAsync(claimsPrincipal, authProperties);

                return(RedirectToAction("Index"));
            }
        }
コード例 #4
0
        private static IServiceCollection AddOAuthStore(this IServiceCollection services)
        {
            var        jwsGenerator = new JwsGeneratorFactory().BuildJwsGenerator();
            JsonWebKey sigJsonWebKey;
            JsonWebKey encJsonWebKey;

            using (var rsa = RSA.Create())
            {
                sigJsonWebKey = new JsonWebKeyBuilder().NewSign("1", new[]
                {
                    KeyOperations.Sign,
                    KeyOperations.Verify
                }).SetAlg(rsa, "RS256").Build();
            }

            using (var rsa = RSA.Create())
            {
                encJsonWebKey = new JsonWebKeyBuilder().NewEnc("2", new[]
                {
                    KeyOperations.Encrypt,
                    KeyOperations.Decrypt
                }).SetAlg(rsa, RSAOAEPCEKHandler.ALG_NAME).Build();
            }

            var jsonWebKeys = new List <JsonWebKey>
            {
                sigJsonWebKey,
                encJsonWebKey
            };
            var clients = new List <OAuthClient>();
            var users   = new List <OAuthUser>();
            var scopes  = new List <OAuthScope>();
            var tokens  = new ConcurrentBag <Token>();

            services.TryAddSingleton <IJsonWebKeyQueryRepository>(new DefaultJsonWebKeyQueryRepository(jsonWebKeys));
            services.TryAddSingleton <IJsonWebKeyCommandRepository>(new DefaultJsonWebKeyCommandRepository(jsonWebKeys));
            services.TryAddSingleton <IOAuthClientQueryRepository>(new DefaultOAuthClientQueryRepository(clients));
            services.TryAddSingleton <IOAuthClientCommandRepository>(new DefaultOAuthClientCommandRepository(clients));
            services.TryAddSingleton <IOAuthUserQueryRepository>(new DefaultOAuthUserQueryRepository(users));
            services.TryAddSingleton <IOAuthUserCommandRepository>(new DefaultOAuthUserCommandRepository(users));
            services.TryAddSingleton <IOAuthScopeQueryRepository>(new DefaultOAuthScopeQueryRepository(scopes));
            services.TryAddSingleton <IOAuthScopeCommandRepository>(new DefaultOAuthScopeCommandRepository(scopes));
            services.TryAddSingleton <ITokenCommandRepository>(new DefaultTokenCommandRepository(tokens));
            services.TryAddSingleton <ITokenQueryRepository>(new DefaultTokenQueryRepository(tokens));
            return(services);
        }