Esempio n. 1
0
        public void TestIsSupportedHttpChallenge()
        {
            var prov = ChallengeHandlerExtManager.GetProvider("manual");

            Assert.IsNotNull(prov);
            Assert.IsTrue(prov.IsSupported(HTTP_CHALLENGE));
        }
Esempio n. 2
0
        public void TestProviderInfo()
        {
            var prov = ChallengeHandlerExtManager.GetProviders().First();

            Assert.AreEqual("manual", prov.Name);
            Assert.AreEqual(prov.Info.SupportedTypes,
                            ChallengeTypeKind.DNS | ChallengeTypeKind.HTTP);
        }
Esempio n. 3
0
        public void TestBuiltInProvidersExist()
        {
            var provsEnum = ChallengeHandlerExtManager.GetProviders();

            Assert.IsNotNull(provsEnum);

            var provsArr = provsEnum.ToArray();

            Assert.IsTrue(provsArr.Length > 0);
        }
Esempio n. 4
0
        public AuthorizeChallenge HandleChallenge(AuthorizationState authzState,
                                                  string challengeType,
                                                  string handlerName, IReadOnlyDictionary <string, object> handlerParams,
                                                  bool cleanUp = false)
        {
            var provider = ChallengeHandlerExtManager.GetProvider(handlerName);

            if (provider == null)
            {
                throw new InvalidOperationException("unable to resolve Challenge Handler provider")
                      .With("handlerName", handlerName);
            }

            var authzChallenge = authzState.Challenges.FirstOrDefault(x => x.Type == challengeType);

            if (authzChallenge == null)
            {
                throw new ArgumentOutOfRangeException(nameof(challengeType),
                                                      "no challenge found matching requested type")
                      .With("challengeType", challengeType);
            }

            if (!provider.IsSupported(authzChallenge.Challenge))
            {
                throw new InvalidOperationException("Challenge Handler does not support given Challenge")
                      .With("handlerName", handlerName)
                      .With("challengeType", authzChallenge.Challenge.Type);
            }

            var handler = provider.GetHandler(authzChallenge.Challenge, handlerParams);

            if (handler == null)
            {
                throw new InvalidOperationException("no Challenge Handler provided for given Challenge")
                      .With("handlerName", handlerName)
                      .With("challengeType", authzChallenge.Challenge.Type);
            }

            authzChallenge.HandlerName = handlerName;

            if (cleanUp)
            {
                handler.CleanUp(authzChallenge.Challenge);
                authzChallenge.HandlerCleanUpDate = DateTime.Now;
            }
            else
            {
                handler.Handle(authzChallenge.Challenge);
                authzChallenge.HandlerHandleDate = DateTime.Now;
            }

            handler.Dispose();

            return(authzChallenge);
        }
Esempio n. 5
0
        public void TestHandlerDisposal()
        {
            var prov = ChallengeHandlerExtManager.GetProvider("manual");

            Assert.IsNotNull(prov);
            var h = prov.GetHandler(DNS_CHALLENGE, null);

            Assert.IsFalse(h.IsDisposed);
            h.Dispose();
            Assert.IsTrue(h.IsDisposed);
            h.Handle(DNS_CHALLENGE);
        }
Esempio n. 6
0
        public void TestCleanUpHttpChallenge()
        {
            var prov = ChallengeHandlerExtManager.GetProvider("manual");

            Assert.IsNotNull(prov);
            var h = prov.GetHandler(HTTP_CHALLENGE, new Dictionary <string, object>
            {
                { "WriteOutPath", "DBG" }
            });

            h.CleanUp(HTTP_CHALLENGE);
            h.Dispose();
        }
Esempio n. 7
0
        public void TestHandleDnsChallenge()
        {
            var prov = ChallengeHandlerExtManager.GetProvider("manual");

            Assert.IsNotNull(prov);
            var h = prov.GetHandler(DNS_CHALLENGE, new Dictionary <string, object>
            {
                { "WriteOutPath", "DBG" }
            });

            h.Handle(new ChallengeHandlingContext(DNS_CHALLENGE));
            h.Dispose();
        }
Esempio n. 8
0
        public void TestProviderParams()
        {
            var prov = ChallengeHandlerExtManager.GetProvider("manual");

            Assert.IsNotNull(prov);

            var paramsEnum = prov.DescribeParameters();

            Assert.IsNotNull(paramsEnum);

            var paramsArr = paramsEnum.ToArray();

            Assert.IsTrue(paramsArr.Length > 0);
        }
        protected override void ProcessRecord()
        {
            // We have to invoke this here because we *may not* invoke
            // any Vault access but we do rely on Ext mechanism access.
            Util.PoshHelper.BeforeExtAccess();

            if (ReloadProviders)
            {
                ChallengeDecoderExtManager.Reload();
                ChallengeHandlerExtManager.Reload();
            }
            else if (!string.IsNullOrEmpty(GetChallengeType))
            {
                WriteVerbose("Getting details of Challenge Type Decoder");
                var tInfo = ChallengeDecoderExtManager.GetProviderInfo(GetChallengeType);
                var t     = ChallengeDecoderExtManager.GetProvider(GetChallengeType);
                WriteObject(new {
                    ChallengeType = tInfo.Type,
                    tInfo.Label,
                    tInfo.SupportedType,
                    tInfo.Description,
                });
            }
            else if (ListChallengeTypes)
            {
                WriteVerbose("Listing all Challenge Type Decoders");
                WriteObject(ChallengeDecoderExtManager.GetProviderInfos().Select(_ => _.Name), true);
            }
            else if (!string.IsNullOrEmpty(GetChallengeHandler))
            {
                WriteVerbose("Getting details of Challenge Type Handler");
                var pInfo = ChallengeHandlerExtManager.GetProviderInfo(GetChallengeHandler);
                var p     = ChallengeHandlerExtManager.GetProvider(GetChallengeHandler);
                if (ParametersOnly)
                {
                    WriteVerbose("Showing parameter details only");
                    WriteObject(p.DescribeParameters().Select(_ => new {
                        _.Name,
                        _.Label,
                        _.Type,
                        _.IsRequired,
                        _.IsMultiValued,
                        _.Description,
                    }), true);
                }
                else
                {
                    WriteObject(new {
                        pInfo.Name,
                        pInfo.Label,
                        pInfo.SupportedTypes,
                        pInfo.Description,
                        Parameters = p.DescribeParameters().Select(_ => new {
                            _.Name,
                            _.Label,
                            _.Type,
                            _.IsRequired,
                            _.IsMultiValued,
                            _.Description,
                        }),
                    });
                }
            }
            else if (ListChallengeHandlers)
            {
                WriteVerbose("Listing all Challenge Type Handlers");
                WriteObject(ChallengeHandlerExtManager.GetProviderInfos().Select(_ => _.Name), true);
            }
            else
            {
                WriteVerbose("Getting details of preconfigured Challenge Handler Profile");
                using (var vlt = Util.VaultHelper.GetVault(VaultProfile))
                {
                    vlt.OpenStorage();
                    var v = vlt.LoadVault();

                    if (ListProfiles)
                    {
                        WriteObject(v.ProviderProfiles?.Values, true);
                    }
                    else
                    {
                        var ppi = v.ProviderProfiles?.GetByRef(ProfileRef, throwOnMissing: false);
                        if (ppi == null)
                        {
                            WriteObject(ppi);
                        }
                        else
                        {
                            var asset = vlt.GetAsset(Vault.VaultAssetType.ProviderConfigInfo,
                                                     ppi.Id.ToString());
                            using (var s = vlt.LoadAsset(asset))
                            {
                                WriteObject(JsonHelper.Load <ProviderProfile>(s), false);
                            }
                        }
                    }
                }
            }
        }
        protected override void ProcessRecord()
        {
            using (var vlt = Util.VaultHelper.GetVault(VaultProfile))
            {
                vlt.OpenStorage();
                var v = vlt.LoadVault();

                if (v.ProviderProfiles == null)
                {
                    WriteVerbose("Initializing Provider Profile collection");
                    v.ProviderProfiles = new Vault.Util.EntityDictionary <Vault.Model.ProviderProfileInfo>();
                }

                WriteVerbose($"Searching for existing Provider Profile for reference [{ProfileName}]");
                var ppi = v.ProviderProfiles.GetByRef(ProfileName, throwOnMissing: false);
                if (ppi == null)
                {
                    WriteVerbose("No existing Profile found");
                }
                else
                {
                    WriteVerbose($"Existing Profile found [{ppi.Id}][{ppi.Alias}]");
                }

                if (!string.IsNullOrEmpty(Rename))
                {
                    if (ppi == null)
                    {
                        throw new KeyNotFoundException("no existing profile found that can be renamed");
                    }

                    v.ProviderProfiles.Rename(ProfileName, Rename);
                    ppi.Alias = Rename;
                }
                else if (Remove)
                {
                    WriteVerbose($"Removing named Provider Profile for name [{ProfileName}]");
                    if (ppi == null)
                    {
                        WriteVerbose("No Provider Profile found for given name");
                        return;
                    }
                    else
                    {
                        v.ProviderProfiles.Remove(ppi.Id);
                        WriteVerbose("Provider Profile removed");
                    }
                }
                else
                {
                    if (ppi != null)
                    {
                        if (!Force)
                        {
                            throw new InvalidOperationException("existing profile found;"
                                                                + " specify -Force to overwrite");
                        }

                        WriteVerbose("Removing existing Profile");
                        v.ProviderProfiles.Remove(ppi.Id);
                    }

                    if (ChallengeHandlerExtManager.GetProviderInfo(Handler) == null)
                    {
                        throw new ArgumentException("Unknown or invalid Handler provider name")
                              .With(nameof(Handler), Handler);
                    }

                    WriteVerbose("Adding new Provider Profile Info");
                    ppi = new Vault.Model.ProviderProfileInfo
                    {
                        Id    = Guid.NewGuid(),
                        Alias = ProfileName,
                        Label = Label,
                        Memo  = Memo,
                    };
                    var pp = new ProviderProfile
                    {
                        ProfileParameters = new Dictionary <string, object>
                        {
                            [nameof(ChallengeType)] = ChallengeType,
                        },
                        ProviderType       = ProviderType.CHALLENGE_HANDLER,
                        ProviderName       = Handler,
                        InstanceParameters = (IReadOnlyDictionary <string, object>
                                              )HandlerParameters.Convert <string, object>(),
                    };

                    var asset = vlt.CreateAsset(Vault.VaultAssetType.ProviderConfigInfo, ppi.Id.ToString());
                    using (var s = vlt.SaveAsset(asset))
                    {
                        JsonHelper.Save(s, pp);
                    }
                    v.ProviderProfiles.Add(ppi);
                }

                vlt.SaveVault(v);
            }
        }