import re


def read_jira_oauth_creds(jira_creds_file):
    """
    Read the Jira Oauth credentials file and return a dictionary containing the
    credentials.
    """

    creds_re = re.compile(
        r'^[\s\S]*access_token\s*:\s*(\S+)\s*'
        r'access_token_secret\s*:\s*(\S+)\s*'
        r'consumer_key\s*:\s*(\S+)\s*'
        r'key_cert\s*:\s*(-.*-)[\s\S]*$')

    oauth_dict = {}
    with open(jira_creds_file, 'rb') as creds_stream:
        creds_data = creds_stream.read().decode('ascii')
        creds_match = creds_re.match(creds_data)
        if creds_match:
            oauth_dict['access_token'] = creds_match.group(1)
            oauth_dict['access_token_secret'] = creds_match.group(2)
            oauth_dict['consumer_key'] = creds_match.group(3)
            # Fix the double-backslash created by the decode() call above
            oauth_dict['key_cert'] = creds_match.group(4).replace("\\n", "\n")

    return oauth_dict