Class PrincipalParser

  • All Implemented Interfaces:
    Principal

    public class PrincipalParser
    extends Parser
    implements Principal
    PrincipalParser is a parser class for the HTTP basic authorization header. It decodes the base64 encoding of the user and password pair.

    This follows the parsing tree of RFC 2617. The goal of this parser is to decode the base64 encoding of the user name and password. After the string has been decoded then the user name and password are extracted. This will only parse headers that are from the Basic authorization scheme. The format of the basic scheme can be found in RFC 2617 and is of the form

      Basic SP base64-encoding.
     
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private byte[] four
      Keeps the bytes used for decoding base64.
      private ParseBuffer password
      Keeps the characters consumed for the password token.
      private int read
      Tracks the read offset for the buffer.
      private int ready
      Tracks the ready offset for the four buffer.
      private ParseBuffer user
      Keeps the characters consumed for the user name token.
      private int write
      Tracks the write offset for the buffer.
    • Constructor Summary

      Constructors 
      Constructor Description
      PrincipalParser()
      Creates a Parser for the basic authorization scheme.
      PrincipalParser​(java.lang.String header)
      Creates a Parser for the basic authorization scheme.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void decode()
      This is used to remove decode the base64 encoding of the user name and password.
      private char first​(byte[] four)
      This is used to extract the byte from the set of four bytes given.
      java.lang.String getName()
      Gets the users name from the Authorization header value.
      java.lang.String getPassword()
      Gets the users password parsed from the Authorization header value.
      protected void init()
      This will initialize the Parser when it is ready to parse a new String.
      private void pack()
      This is used to remove all whitespace characters from the String excluding the whitespace within literals.
      protected void parse()
      Used to parse the actual header data.
      private void password()
      Extracts the password from the buffer.
      private char second​(byte[] four)
      This is used to extract the byte from the set of four bytes given.
      private boolean text​(char c)
      This is used to determine wheather or not a character is a TEXT character according to the HTTP specification, that is RFC 2616 specifies a TEXT character as one that is any octet except those less than 32 and not 127.
      private char third​(byte[] four)
      This is used to extract the byte from the set of four bytes given.
      private int translate​(int octet)
      This uses a basic translation from the byte character to the byte number.
      private void userid()
      Extracts the user name from the buffer.
      private void userpass()
      Extracts the name and password of the user from the name : password pair that was given.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • password

        private ParseBuffer password
        Keeps the characters consumed for the password token.
      • user

        private ParseBuffer user
        Keeps the characters consumed for the user name token.
      • four

        private byte[] four
        Keeps the bytes used for decoding base64.
      • write

        private int write
        Tracks the write offset for the buffer.
      • ready

        private int ready
        Tracks the ready offset for the four buffer.
      • read

        private int read
        Tracks the read offset for the buffer.
    • Constructor Detail

      • PrincipalParser

        public PrincipalParser()
        Creates a Parser for the basic authorization scheme. This allows headers that are of this scheme to be broken into its component parts i.e. user name and password.
      • PrincipalParser

        public PrincipalParser​(java.lang.String header)
        Creates a Parser for the basic authorization scheme. This allows headers that are of this scheme to be broken into its component parts i.e. user name and password. This constructor will parse the String given as the header.
        Parameters:
        header - this is a header value from the basic scheme
    • Method Detail

      • getPassword

        public java.lang.String getPassword()
        Gets the users password parsed from the Authorization header value. If there was not password parsed from the base64 value of the header this returns null
        Specified by:
        getPassword in interface Principal
        Returns:
        the password for the user or null
      • getName

        public java.lang.String getName()
        Gets the users name from the Authorization header value. This will return null if there is no user name extracted from the base64 header value.
        Specified by:
        getName in interface Principal
        Returns:
        this returns the name of the user
      • parse

        protected void parse()
        Used to parse the actual header data. This will attempt to read the "Basic" token from the set of characters given, if this is successful then the username and password is extracted.
        Specified by:
        parse in class Parser
      • init

        protected void init()
        This will initialize the Parser when it is ready to parse a new String. This will reset the Parser to a ready state. The init method is invoked by the Parser when the parse method is invoked.
        Specified by:
        init in class Parser
      • pack

        private void pack()
        This is used to remove all whitespace characters from the String excluding the whitespace within literals. The definition of a literal can be found in RFC 2616.

        The definition of a literal for RFC 2616 is anything between 2 quotes but excuding quotes that are prefixed with the backward slash character.

      • userpass

        private void userpass()
        Extracts the name and password of the user from the name : password pair that was given. This will take all data up to the first occurence of a ':' character as the users name and all data after the colon as the users password.
      • userid

        private void userid()
        Extracts the user name from the buffer. This will read up to the first occurence of a colon, ':', character as the user name. For the BNF syntax of this see RFC 2617.
      • password

        private void password()
        Extracts the password from the buffer. This will all characters from the current offset to the first non text character as the password. For the BNF syntax of this see RFC 2617.
      • decode

        private void decode()
        This is used to remove decode the base64 encoding of the user name and password. This uses a standart base64 decoding scheme.

        For information on the decoding scheme used for base64 see the RFC 2045 on MIME, Multipurpose Internet Mail Extensions.

      • translate

        private int translate​(int octet)
        This uses a basic translation from the byte character to the byte number.

        The table for translation the data can be found in RFC 2045 on MIME, Multipurpose Internet Mail Extensions.

        Parameters:
        octet - this is the octet ttat is to be translated
        Returns:
        this returns the translated octet
      • first

        private char first​(byte[] four)
        This is used to extract the byte from the set of four bytes given. This method is used to isolate the correct bits that corrospond to an actual character withing the base64 data.
        Parameters:
        four - this is the four bytes that the character is to be extracted from
        Returns:
        this returns the character extracted
      • second

        private char second​(byte[] four)
        This is used to extract the byte from the set of four bytes given. This method is used to isolate the correct bits that corrospond to an actual character withing the base64 data.
        Parameters:
        four - this is the four bytes that the character is to be extracted from
        Returns:
        this returns the character extracted
      • third

        private char third​(byte[] four)
        This is used to extract the byte from the set of four bytes given. This method is used to isolate the correct bits that corrospond to an actual character withing the base64 data.
        Parameters:
        four - this is the four bytes that the character is to be extracted from
        Returns:
        this returns the character extracted
      • text

        private boolean text​(char c)
        This is used to determine wheather or not a character is a TEXT character according to the HTTP specification, that is RFC 2616 specifies a TEXT character as one that is any octet except those less than 32 and not 127.
        Parameters:
        c - this is the character that is to be determined
        Returns:
        this returns true if the character is a TEXT