public void Test0141_HandleHttpChallenge() { using (var signer = new RS256Signer()) { signer.Init(); using (var fs = new FileStream(_testRegister_AcmeSignerFile, FileMode.Open)) { signer.Load(fs); } AcmeRegistration reg; using (var fs = new FileStream(_testRegister_AcmeRegFile, FileMode.Open)) { reg = AcmeRegistration.Load(fs); } using (var client = BuildClient(testTagHeader: nameof(Test0141_HandleHttpChallenge))) { client.RootUrl = _rootUrl; client.Signer = signer; client.Registration = reg; client.Init(); client.GetDirectory(true); AuthorizationState authzState; using (var fs = new FileStream(_testAuthz_AcmeAuthzFile, FileMode.Open)) { authzState = AuthorizationState.Load(fs); } var authzChallenge = client.GenerateAuthorizeChallengeAnswer(authzState, AcmeProtocol.CHALLENGE_TYPE_HTTP); _testAuthzChallengeHttpHandled_AcmeAuthzFile = $"{_baseLocalStore}\\TestAuthz-ChallengeAnswersHandleHttp.acmeAuthz"; using (var fs = new FileStream(_testAuthzChallengeHttpHandled_AcmeAuthzFile, FileMode.Create)) { authzState.Save(fs); } var wsFilePath = authzChallenge.ChallengeAnswer.Key; var wsFileBody = authzChallenge.ChallengeAnswer.Value; var wsInfo = WebServerInfo.Load(File.ReadAllText("config\\webServerInfo.json")); using (var s = new MemoryStream(Encoding.UTF8.GetBytes(wsFileBody))) { var fileUrl = new Uri($"http://{authzState.Identifier}/{wsFilePath}"); wsInfo.Provider.UploadFile(fileUrl, s); } } } Thread.Sleep(90 * 1000); }
public void Test0140_HandleHttpChallenge() { using (var signer = new RS256Signer()) { signer.Init(); using (var fs = new FileStream($"{BASE_LOCAL_STORE}TestRegister.acmeSigner", FileMode.Open)) { signer.Load(fs); } AcmeRegistration reg; using (var fs = new FileStream($"{BASE_LOCAL_STORE}TestRegister.acmeReg", FileMode.Open)) { reg = AcmeRegistration.Load(fs); } using (var client = BuildClient()) { client.RootUrl = _rootUrl; client.Signer = signer; client.Registration = reg; client.Init(); client.GetDirectory(true); AuthorizationState authzState; using (var fs = new FileStream($"{BASE_LOCAL_STORE}TestAuthz.acmeAuthz", FileMode.Open)) { authzState = AuthorizationState.Load(fs); } var authzChallenge = client.GenerateAuthorizeChallengeAnswer(authzState, "simpleHttp"); using (var fs = new FileStream($"{BASE_LOCAL_STORE}TestAuthz-ChallengeAnswersHandleHttp.acmeAuthz", FileMode.Create)) { authzState.Save(fs); } var wsFilePath = authzChallenge.ChallengeAnswer.Key; var wsFileBody = authzChallenge.ChallengeAnswer.Value; var wsInfo = WebServerInfo.Load(File.ReadAllText("webServerInfo.json")); using (var s = new MemoryStream(Encoding.UTF8.GetBytes(wsFileBody))) { var fileUrl = new Uri($"http://{authzState.Identifier}/{wsFilePath}"); wsInfo.Provider.UploadFile(fileUrl, s); } } } Thread.Sleep(90 * 1000); }
protected override void ProcessRecord() { using (var vp = InitializeVault.GetVaultProvider(VaultProfile)) { vp.OpenStorage(); var v = vp.LoadVault(); if (v.Registrations == null || v.Registrations.Count < 1) { throw new InvalidOperationException("No registrations found"); } var ri = v.Registrations[0]; var r = ri.Registration; if (v.Identifiers == null || v.Identifiers.Count < 1) { throw new InvalidOperationException("No identifiers found"); } var ii = v.Identifiers.GetByRef(Ref); if (ii == null) { throw new Exception("Unable to find an Identifier for the given reference"); } var authzState = ii.Authorization; if (ii.Challenges == null) { ii.Challenges = new Dictionary <string, AuthorizeChallenge>(); } if (ii.ChallengeCompleted == null) { ii.ChallengeCompleted = new Dictionary <string, DateTime?>(); } if (v.ProviderConfigs == null || v.ProviderConfigs.Count < 1) { throw new InvalidOperationException("No provider configs found"); } var pc = v.ProviderConfigs.GetByRef(ProviderConfig); if (pc == null) { throw new InvalidOperationException("Unable to find a Provider Config for the given reference"); } AuthorizeChallenge challenge = null; DateTime? challengCompleted = null; ii.Challenges.TryGetValue(Challenge, out challenge); ii.ChallengeCompleted.TryGetValue(Challenge, out challengCompleted); if (challenge == null || Regenerate) { using (var c = ClientHelper.GetClient(v, ri)) { c.Init(); c.GetDirectory(true); challenge = c.GenerateAuthorizeChallengeAnswer(authzState, Challenge); ii.Challenges[Challenge] = challenge; } } if (Repeat || challengCompleted == null) { var pcFilePath = $"{pc.Id}.json"; var pcAsset = vp.GetAsset(Vault.VaultAssetType.ProviderConfigInfo, pcFilePath); // TODO: There's *way* too much logic buried in here // this needs to be refactored and extracted out to be // more manageble and more reusable if (Challenge == AcmeProtocol.CHALLENGE_TYPE_DNS) { if (string.IsNullOrEmpty(pc.DnsProvider)) { throw new InvalidOperationException("Referenced Provider Configuration does not support the selected Challenge"); } var dnsName = challenge.ChallengeAnswer.Key; var dnsValue = Regex.Replace(challenge.ChallengeAnswer.Value, "\\s", ""); var dnsValues = Regex.Replace(dnsValue, "(.{100,100})", "$1\n").Split('\n'); using (var s = vp.LoadAsset(pcAsset)) // new FileStream(pcFilePath, FileMode.Open)) { var dnsInfo = DnsInfo.Load(s); dnsInfo.Provider.EditTxtRecord(dnsName, dnsValues); ii.ChallengeCompleted[Challenge] = DateTime.Now; } } else if (Challenge == AcmeProtocol.CHALLENGE_TYPE_HTTP) { if (string.IsNullOrEmpty(pc.WebServerProvider)) { throw new InvalidOperationException("Referenced Provider Configuration does not support the selected Challenge"); } var wsFilePath = challenge.ChallengeAnswer.Key; var wsFileBody = challenge.ChallengeAnswer.Value; var wsFileUrl = new Uri($"http://{authzState.Identifier}/{wsFilePath}"); using (var s = vp.LoadAsset(pcAsset)) // new FileStream(pcFilePath, FileMode.Open)) { var webServerInfo = WebServerInfo.Load(s); using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(wsFileBody))) { webServerInfo.Provider.UploadFile(wsFileUrl, ms); ii.ChallengeCompleted[Challenge] = DateTime.Now; } } } } vp.SaveVault(v); WriteObject(authzState); } }