gluu
関数 | 変数
duo_web 名前空間

関数

def _hmac_sha1 (key, msg)
 
def _sign_vals (key, vals, prefix, expire)
 
def _parse_vals (key, val, prefix, ikey)
 
def _sign_request (ikey, skey, akey, username, prefix)
 
def sign_request (ikey, skey, akey, username)
 
def sign_enroll_request (ikey, skey, akey, username)
 
def _verify_response (ikey, skey, akey, prefix, sig_response)
 
def verify_response (ikey, skey, akey, sig_response)
 
def verify_enroll_response (ikey, skey, akey, sig_response)
 

変数

string DUO_PREFIX = 'TX'
 
string APP_PREFIX = 'APP'
 
string AUTH_PREFIX = 'AUTH'
 
string ENROLL_PREFIX = 'ENROLL'
 
string ENROLL_REQUEST_PREFIX = 'ENROLL_REQUEST'
 
int DUO_EXPIRE = 300
 
int APP_EXPIRE = 3600
 
int IKEY_LEN = 20
 
int SKEY_LEN = 40
 
int AKEY_LEN = 40
 
string ERR_USER = 'ERR|The username passed to sign_request() is invalid.'
 
string ERR_IKEY = 'ERR|The Duo integration key passed to sign_request() is invalid.'
 
string ERR_SKEY = 'ERR|The Duo secret key passed to sign_request() is invalid.'
 
string ERR_AKEY = 'ERR|The application secret key passed to sign_request() must be at least %s characters.' % AKEY_LEN
 
string ERR_UNKNOWN = 'ERR|An unknown error has occurred.'
 

関数詳解

◆ _hmac_sha1()

def duo_web._hmac_sha1 (   key,
  msg 
)
private
32 def _hmac_sha1(key, msg):
33  ctx = hmac.new(key, msg, hashlib.sha1)
34  return ctx.hexdigest()
35 
def _hmac_sha1(key, msg)
Definition: duo_web.py:32

◆ _parse_vals()

def duo_web._parse_vals (   key,
  val,
  prefix,
  ikey 
)
private
46 def _parse_vals(key, val, prefix, ikey):
47  ts = int(time.time())
48  u_prefix, u_b64, u_sig = val.split('|')
49  cookie = '%s|%s' % (u_prefix, u_b64)
50  e_key = key.encode('utf-8')
51  e_cookie = cookie.encode('utf-8')
52 
53  sig = _hmac_sha1(e_key, e_cookie)
54  if _hmac_sha1(e_key, sig.encode('utf-8')) != _hmac_sha1(e_key, u_sig.encode('utf-8')):
55  return None
56 
57  if u_prefix != prefix:
58  return None
59 
60  decoded = base64.b64decode(u_b64).decode('utf-8')
61  user, u_ikey, exp = decoded.split('|')
62 
63  if u_ikey != ikey:
64  return None
65 
66  if ts >= int(exp):
67  return None
68 
69  return user
70 
def _hmac_sha1(key, msg)
Definition: duo_web.py:32
def _parse_vals(key, val, prefix, ikey)
Definition: duo_web.py:46

◆ _sign_request()

def duo_web._sign_request (   ikey,
  skey,
  akey,
  username,
  prefix 
)
private
Generate a signed request for Duo authentication.
The returned value should be passed into the Duo.init() call
in the rendered web page used for Duo authentication.
Arguments:
ikey      -- Duo integration key
skey      -- Duo secret key
akey      -- Application secret key
username  -- Primary-authenticated username
prefix    -- DUO_PREFIX or ENROLL_REQUEST_PREFIX
71 def _sign_request(ikey, skey, akey, username, prefix):
72  """Generate a signed request for Duo authentication.
73  The returned value should be passed into the Duo.init() call
74  in the rendered web page used for Duo authentication.
75  Arguments:
76  ikey -- Duo integration key
77  skey -- Duo secret key
78  akey -- Application secret key
79  username -- Primary-authenticated username
80  prefix -- DUO_PREFIX or ENROLL_REQUEST_PREFIX
81  """
82  if not username:
83  return ERR_USER
84  if '|' in username:
85  return ERR_USER
86  if not ikey or len(ikey) != IKEY_LEN:
87  return ERR_IKEY
88  if not skey or len(skey) != SKEY_LEN:
89  return ERR_SKEY
90  if not akey or len(akey) < AKEY_LEN:
91  return ERR_AKEY
92 
93  vals = [ username, ikey ]
94 
95  try:
96  duo_sig = _sign_vals(skey, vals, prefix, DUO_EXPIRE)
97  app_sig = _sign_vals(akey, vals, APP_PREFIX, APP_EXPIRE)
98  except Exception:
99  return ERR_UNKNOWN
100 
101  return '%s:%s' % (duo_sig, app_sig)
102 
103 
def _sign_request(ikey, skey, akey, username, prefix)
Definition: duo_web.py:71
def _sign_vals(key, vals, prefix, expire)
Definition: duo_web.py:36

◆ _sign_vals()

def duo_web._sign_vals (   key,
  vals,
  prefix,
  expire 
)
private
36 def _sign_vals(key, vals, prefix, expire):
37  exp = str(int(time.time()) + expire)
38 
39  val = '|'.join(vals + [ exp ])
40  b64 = base64.b64encode(val.encode('utf-8')).decode('utf-8')
41  cookie = '%s|%s' % (prefix, b64)
42 
43  sig = _hmac_sha1(key.encode('utf-8'), cookie.encode('utf-8'))
44  return '%s|%s' % (cookie, sig)
45 
def _hmac_sha1(key, msg)
Definition: duo_web.py:32
def _sign_vals(key, vals, prefix, expire)
Definition: duo_web.py:36

◆ _verify_response()

def duo_web._verify_response (   ikey,
  skey,
  akey,
  prefix,
  sig_response 
)
private
Validate the signed response returned from Duo.
Returns the username of the authenticated user, or None.
Arguments:
ikey          -- Duo integration key
skey          -- Duo secret key
akey          -- Application secret key
prefix        -- AUTH_PREFIX or ENROLL_PREFIX that sig_response
                 must match
sig_response  -- The signed response POST'ed to the server
130 def _verify_response(ikey, skey, akey, prefix, sig_response):
131  """Validate the signed response returned from Duo.
132  Returns the username of the authenticated user, or None.
133  Arguments:
134  ikey -- Duo integration key
135  skey -- Duo secret key
136  akey -- Application secret key
137  prefix -- AUTH_PREFIX or ENROLL_PREFIX that sig_response
138  must match
139  sig_response -- The signed response POST'ed to the server
140  """
141  try:
142  auth_sig, app_sig = sig_response.split(':')
143  auth_user = _parse_vals(skey, auth_sig, AUTH_PREFIX, ikey)
144  app_user = _parse_vals(akey, app_sig, APP_PREFIX, ikey)
145  except Exception:
146  return None
147 
148  if auth_user != app_user:
149  return None
150 
151  return auth_user
152 
153 
def _verify_response(ikey, skey, akey, prefix, sig_response)
Definition: duo_web.py:130
def _parse_vals(key, val, prefix, ikey)
Definition: duo_web.py:46

◆ sign_enroll_request()

def duo_web.sign_enroll_request (   ikey,
  skey,
  akey,
  username 
)
Generate a signed request for Duo authentication.
The returned value should be passed into the Duo.init() call
in the rendered web page used for Duo authentication.
Arguments:
ikey      -- Duo integration key
skey      -- Duo secret key
akey      -- Application secret key
username  -- Primary-authenticated username
117 def sign_enroll_request(ikey, skey, akey, username):
118  """Generate a signed request for Duo authentication.
119  The returned value should be passed into the Duo.init() call
120  in the rendered web page used for Duo authentication.
121  Arguments:
122  ikey -- Duo integration key
123  skey -- Duo secret key
124  akey -- Application secret key
125  username -- Primary-authenticated username
126  """
127  return _sign_request(ikey, skey, akey, username, ENROLL_REQUEST_PREFIX)
128 
129 
def _sign_request(ikey, skey, akey, username, prefix)
Definition: duo_web.py:71
def sign_enroll_request(ikey, skey, akey, username)
Definition: duo_web.py:117

◆ sign_request()

def duo_web.sign_request (   ikey,
  skey,
  akey,
  username 
)
Generate a signed request for Duo authentication.
The returned value should be passed into the Duo.init() call
in the rendered web page used for Duo authentication.
Arguments:
ikey      -- Duo integration key
skey      -- Duo secret key
akey      -- Application secret key
username  -- Primary-authenticated username
104 def sign_request(ikey, skey, akey, username):
105  """Generate a signed request for Duo authentication.
106  The returned value should be passed into the Duo.init() call
107  in the rendered web page used for Duo authentication.
108  Arguments:
109  ikey -- Duo integration key
110  skey -- Duo secret key
111  akey -- Application secret key
112  username -- Primary-authenticated username
113  """
114  return _sign_request(ikey, skey, akey, username, DUO_PREFIX)
115 
116 
def _sign_request(ikey, skey, akey, username, prefix)
Definition: duo_web.py:71
def sign_request(ikey, skey, akey, username)
Definition: duo_web.py:104

◆ verify_enroll_response()

def duo_web.verify_enroll_response (   ikey,
  skey,
  akey,
  sig_response 
)
Validate the signed response returned from Duo.
Returns the username of the enrolled user, or None.
Arguments:
ikey          -- Duo integration key
skey          -- Duo secret key
akey          -- Application secret key
sig_response  -- The signed response POST'ed to the server
166 def verify_enroll_response(ikey, skey, akey, sig_response):
167  """Validate the signed response returned from Duo.
168  Returns the username of the enrolled user, or None.
169  Arguments:
170  ikey -- Duo integration key
171  skey -- Duo secret key
172  akey -- Application secret key
173  sig_response -- The signed response POST'ed to the server
174  """
175  return _verify_response(ikey, skey, akey, ENROLL_PREFIX, sig_response)
176 
def _verify_response(ikey, skey, akey, prefix, sig_response)
Definition: duo_web.py:130
def verify_enroll_response(ikey, skey, akey, sig_response)
Definition: duo_web.py:166

◆ verify_response()

def duo_web.verify_response (   ikey,
  skey,
  akey,
  sig_response 
)
Validate the signed response returned from Duo.
Returns the username of the authenticated user, or None.
Arguments:
ikey          -- Duo integration key
skey          -- Duo secret key
akey          -- Application secret key
sig_response  -- The signed response POST'ed to the server
154 def verify_response(ikey, skey, akey, sig_response):
155  """Validate the signed response returned from Duo.
156  Returns the username of the authenticated user, or None.
157  Arguments:
158  ikey -- Duo integration key
159  skey -- Duo secret key
160  akey -- Application secret key
161  sig_response -- The signed response POST'ed to the server
162  """
163  return _verify_response(ikey, skey, akey, AUTH_PREFIX, sig_response)
164 
165 
def _verify_response(ikey, skey, akey, prefix, sig_response)
Definition: duo_web.py:130
def verify_response(ikey, skey, akey, sig_response)
Definition: duo_web.py:154

変数詳解

◆ AKEY_LEN

int duo_web.AKEY_LEN = 40

◆ APP_EXPIRE

int duo_web.APP_EXPIRE = 3600

◆ APP_PREFIX

string duo_web.APP_PREFIX = 'APP'

◆ AUTH_PREFIX

string duo_web.AUTH_PREFIX = 'AUTH'

◆ DUO_EXPIRE

int duo_web.DUO_EXPIRE = 300

◆ DUO_PREFIX

string duo_web.DUO_PREFIX = 'TX'

◆ ENROLL_PREFIX

string duo_web.ENROLL_PREFIX = 'ENROLL'

◆ ENROLL_REQUEST_PREFIX

string duo_web.ENROLL_REQUEST_PREFIX = 'ENROLL_REQUEST'

◆ ERR_AKEY

string duo_web.ERR_AKEY = 'ERR|The application secret key passed to sign_request() must be at least %s characters.' % AKEY_LEN

◆ ERR_IKEY

string duo_web.ERR_IKEY = 'ERR|The Duo integration key passed to sign_request() is invalid.'

◆ ERR_SKEY

string duo_web.ERR_SKEY = 'ERR|The Duo secret key passed to sign_request() is invalid.'

◆ ERR_UNKNOWN

string duo_web.ERR_UNKNOWN = 'ERR|An unknown error has occurred.'

◆ ERR_USER

string duo_web.ERR_USER = 'ERR|The username passed to sign_request() is invalid.'

◆ IKEY_LEN

int duo_web.IKEY_LEN = 20

◆ SKEY_LEN

int duo_web.SKEY_LEN = 40