Ejemplo n.º 1
0
        /// <summary>
        /// Attempts to retrieve the username from a password file.
        /// </summary>
        /// <param name="passwordFile">
        /// A <see cref="ParsedPasswordFile"/> representing the password file from which the username should be fetched.
        /// </param>
        /// <returns>
        /// A string containing the username if the password file contains one, null if no username was found.
        /// </returns>
        public string GetUsername(ParsedPasswordFile passwordFile)
        {
            var options = usernameDetection.Options;

            switch (usernameDetection.Method)
            {
            case UsernameDetectionMethod.FileName:
                return(passwordFile.FileNameWithoutExtension);

            case UsernameDetectionMethod.LineNumber:
                var extraLines = passwordFile.Metadata.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
                var lineNumber = options.LineNumber - 2;
                if (lineNumber < 0)
                {
                    throw new PasswordParseException($"The username may not be located on line #{options.LineNumber}.");
                }
                return(lineNumber < extraLines.Length ? extraLines[lineNumber] : null);

            case UsernameDetectionMethod.Regex:
                var rgxOptions = options.RegexOptions.IgnoreCase ? RegexOptions.IgnoreCase : RegexOptions.None;
                rgxOptions = rgxOptions | (options.RegexOptions.Multiline ? RegexOptions.Multiline : RegexOptions.None);
                rgxOptions = rgxOptions | (options.RegexOptions.Singleline ? RegexOptions.Singleline : RegexOptions.None);
                var match = Regex.Match(passwordFile.Metadata, options.Regex, rgxOptions);
                return(match.Groups["username"].Success ? match.Groups["username"].Value : null);

            default:
                throw new ArgumentOutOfRangeException("username-detection.method", "Invalid username detection method.");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds a new password file at the specified path.
        /// </summary>
        /// <param name="path">A path, relative to the password store, indicating where the password should be created.</param>
        /// <param name="password">The password to be encrypted.</param>
        /// <param name="metadata">Any metadata that should be added.</param>
        /// <exception cref="InvalidOperationException">If a file already exists at the given location.</exception>
        public PasswordFile AddPassword(string path, string password, string metadata)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (FileSystem.Path.IsPathRooted(path))
            {
                throw new ArgumentException("Path to the password file must be relative.");
            }

            var file   = CreatePasswordFileFromPath(path);
            var parsed = new ParsedPasswordFile(file, password, metadata);

            return(EncryptPasswordInternal(parsed, false));
        }