# -*- coding: utf-8 -*-
'''
Authentication runner for creating, deleting, and managing eauth tokens.

.. versionadded:: 2016.11.0

'''

# Import Python libs
from __future__ import absolute_import
import os

# Import Salt libs
import salt.auth
import salt.exceptions
import salt.netapi


def mk_token(**load):
    '''
    Create an eauth token using provided credentials

    Non-root users may specify an expiration date (if allowed via the
    :conf_master:`token_expire_user_override` setting) by passing an additional
    ``token_expire`` param.

    CLI Example:

    .. code-block:: shell

        salt-run auth.mk_token username=saltdev password=saltdev eauth=auto
        salt-run auth.mk_token username=saltdev password=saltdev eauth=auto \\
            token_expire=94670856
    '''
    # This will hang if the master daemon is not running.
    netapi = salt.netapi.NetapiClient(__opts__)
    if not netapi._is_master_running():
        raise salt.exceptions.SaltDaemonNotRunning(
                'Salt Master must be running.')

    auth = salt.auth.Resolver(__opts__)
    return auth.mk_token(load)


def del_token(token):
    '''
    Delete an eauth token by name

    CLI Example:

    .. code-block:: shell

        salt-run auth.del_token 6556760736e4077daa601baec2b67c24
    '''
    token_path = os.path.join(__opts__['token_dir'], token)
    if os.path.exists(token_path):
        return os.remove(token_path) is None
    return False

@@ -302,24 +302,25 @@
 *load):%0A    
+r
 '''%0A    Crea
@@ -415,17 +415,19 @@
 on date 
-(
+-- 
 if allow
@@ -490,17 +490,19 @@
  setting
-)
+ --
  by pass
@@ -507,16 +507,20 @@
 ssing an
+%0A   
  additio
@@ -522,20 +522,16 @@
 ditional
-%0A   
  %60%60token
@@ -545,16 +545,165 @@
 %60%60 param
+. This overrides the%0A    :conf_master:%60token_expire%60 setting of the same name in the Master config%0A    and is how long a token should live in seconds
 .%0A%0A    C
@@ -817,16 +817,65 @@
 uth=auto
+%0A%0A        # Create a token valid for three years.
 %0A       
@@ -944,17 +944,16 @@
 h=auto %5C
-%5C
 %0A       
@@ -978,16 +978,215 @@
 94670856
+%0A%0A        # Calculate the number of seconds using expr.%0A        salt-run auth.mk_token username=saltdev password=saltdev eauth=auto %5C%0A            token_expire=$(expr %5C( 365 %5C* 24 %5C* 60 %5C* 60 %5C) %5C* 3)
 %0A    '''

Add more docs on the token_expire param