private static (JwtCreatorOptions, bool) ValidateArguments( IReporter reporter, CommandOption projectOption, CommandOption schemeNameOption, CommandOption nameOption, CommandOption audienceOption, CommandOption issuerOption, CommandOption notBeforeOption, CommandOption expiresOnOption, CommandOption validForOption, CommandOption rolesOption, CommandOption scopesOption, CommandOption claimsOption) { var isValid = true; var project = DevJwtCliHelpers.GetProject(projectOption.Value()); var scheme = schemeNameOption.HasValue() ? schemeNameOption.Value() : "Bearer"; var name = nameOption.HasValue() ? nameOption.Value() : Environment.UserName; var audience = audienceOption.HasValue() ? audienceOption.Values : DevJwtCliHelpers.GetAudienceCandidatesFromLaunchSettings(project).ToList(); if (audience is null) { reporter.Error(Resources.CreateCommand_NoAudience_Error); isValid = false; } var issuer = issuerOption.HasValue() ? issuerOption.Value() : DevJwtsDefaults.Issuer; var notBefore = DateTime.UtcNow; if (notBeforeOption.HasValue()) { if (!ParseDate(notBeforeOption.Value(), out notBefore)) { reporter.Error(Resources.FormatCreateCommand_InvalidDate_Error("--not-before")); isValid = false; } } var expiresOn = notBefore.AddMonths(3); if (expiresOnOption.HasValue()) { if (!ParseDate(expiresOnOption.Value(), out expiresOn)) { reporter.Error(Resources.FormatCreateCommand_InvalidDate_Error("--expires-on")); isValid = false; } } if (validForOption.HasValue()) { if (!TimeSpan.TryParseExact(validForOption.Value(), _timeSpanFormats, CultureInfo.InvariantCulture, out var validForValue)) { reporter.Error(Resources.FormatCreateCommand_InvalidPeriod_Error("--valid-for")); } expiresOn = notBefore.Add(validForValue); } var roles = rolesOption.HasValue() ? rolesOption.Values : new List <string>(); var scopes = scopesOption.HasValue() ? scopesOption.Values : new List <string>(); var claims = new Dictionary <string, string>(); if (claimsOption.HasValue()) { if (!DevJwtCliHelpers.TryParseClaims(claimsOption.Values, out claims)) { reporter.Error(Resources.CreateCommand_InvalidClaims_Error); isValid = false; } } return(new JwtCreatorOptions(scheme, name, audience, issuer, notBefore, expiresOn, roles, scopes, claims), isValid);
private static (JwtCreatorOptions, bool, string) ValidateArguments( IReporter reporter, CommandOption projectOption, CommandOption schemeNameOption, CommandOption nameOption, CommandOption audienceOption, CommandOption issuerOption, CommandOption notBeforeOption, CommandOption expiresOnOption, CommandOption validForOption, CommandOption rolesOption, CommandOption scopesOption, CommandOption claimsOption) { var isValid = true; var project = DevJwtCliHelpers.GetProject(projectOption.Value()); if (project == null) { reporter.Error(Resources.ProjectOption_ProjectNotFound); isValid = false; // Break out early if we haven't been able to resolve a project // since we depend on it for the managing of JWT tokens return( null, isValid, string.Empty ); } var scheme = schemeNameOption.HasValue() ? schemeNameOption.Value() : "Bearer"; var optionsString = schemeNameOption.HasValue() ? $"{Resources.JwtPrint_Scheme}: {scheme}{Environment.NewLine}" : string.Empty; var name = nameOption.HasValue() ? nameOption.Value() : Environment.UserName; optionsString += $"{Resources.JwtPrint_Name}: {name}{Environment.NewLine}"; var audience = audienceOption.HasValue() ? audienceOption.Values : DevJwtCliHelpers.GetAudienceCandidatesFromLaunchSettings(project); optionsString += audienceOption.HasValue() ? $"{Resources.JwtPrint_Audiences}: {string.Join(", ", audience)}{Environment.NewLine}" : string.Empty; if (audience is null || audience.Count == 0) { reporter.Error(Resources.CreateCommand_NoAudience_Error); isValid = false; } var issuer = issuerOption.HasValue() ? issuerOption.Value() : DevJwtsDefaults.Issuer; optionsString += issuerOption.HasValue() ? $"{Resources.JwtPrint_Issuer}: {issuer}{Environment.NewLine}" : string.Empty; var notBefore = DateTime.UtcNow; if (notBeforeOption.HasValue()) { if (!ParseDate(notBeforeOption.Value(), out notBefore)) { reporter.Error(Resources.FormatCreateCommand_InvalidDate_Error("--not-before")); isValid = false; } optionsString += $"{Resources.JwtPrint_NotBefore}: {notBefore:O}{Environment.NewLine}"; } var expiresOn = notBefore.AddMonths(3); if (expiresOnOption.HasValue()) { if (!ParseDate(expiresOnOption.Value(), out expiresOn)) { reporter.Error(Resources.FormatCreateCommand_InvalidDate_Error("--expires-on")); isValid = false; } if (validForOption.HasValue()) { reporter.Error(Resources.CreateCommand_InvalidExpiresOn_Error); isValid = false; } else { optionsString += $"{Resources.JwtPrint_ExpiresOn}: {expiresOn:O}{Environment.NewLine}"; } } if (validForOption.HasValue()) { if (!TimeSpan.TryParseExact(validForOption.Value(), _timeSpanFormats, CultureInfo.InvariantCulture, out var validForValue)) { reporter.Error(Resources.FormatCreateCommand_InvalidPeriod_Error("--valid-for")); } expiresOn = notBefore.Add(validForValue); if (expiresOnOption.HasValue()) { reporter.Error(Resources.CreateCommand_InvalidExpiresOn_Error); isValid = false; } else { optionsString += $"{Resources.JwtPrint_ExpiresOn}: {expiresOn:O}{Environment.NewLine}"; } } var roles = rolesOption.HasValue() ? rolesOption.Values : new List <string>(); optionsString += rolesOption.HasValue() ? $"{Resources.JwtPrint_Roles}: [{string.Join(", ", roles)}]{Environment.NewLine}" : string.Empty; var scopes = scopesOption.HasValue() ? scopesOption.Values : new List <string>(); optionsString += scopesOption.HasValue() ? $"{Resources.JwtPrint_Scopes}: {string.Join(", ", scopes)}{Environment.NewLine}" : string.Empty; var claims = new Dictionary <string, string>(); if (claimsOption.HasValue()) { if (!DevJwtCliHelpers.TryParseClaims(claimsOption.Values, out claims)) { reporter.Error(Resources.CreateCommand_InvalidClaims_Error); isValid = false; } optionsString += $"{Resources.JwtPrint_CustomClaims}: [{string.Join(", ", claims.Select(kvp => $"{kvp.Key}={kvp.Value}"))}]{Environment.NewLine}"; } return( new JwtCreatorOptions(scheme, name, audience, issuer, notBefore, expiresOn, roles, scopes, claims), isValid, optionsString);