繁体   English   中英

Python-ldap:是否可以在不显式编写密码的情况下进行绑定?

[英]Python-ldap: is it possible to bind without explicitly writing the password?

编写Python脚本时,我想知道是否可以在不以明文形式编写密码的情况下绑定到LDAP服务器,例如以下示例:

import ldap

l = ldap.open("myserver")
username = "cn=Manager, o=mydomain.com"

## I don't want to write the password here in plaintext
password  = "secret"

l.simple_bind(username, password)

用于解密名为“ .credentials”的文件的示例函数。 当然,这将具有单独的脚本,以便在尝试使用凭据之前首先将凭据加密到文件中。

因此,您将调用此函数:

username, password = decrypt()

l.simple_bind(username, password)

from Crypto.Cipher import AES
import base64
from local_logging import info

def decrypt(dir_path):
    #Read '.credentials' file and return unencrypted credentials (user_decoded, pass_decoded)

    lines = [line.rstrip('\n') for line in open(dir_path + '/.credentials')]

    user_encoded = lines[0]
    user_secret = lines[1]
    pass_encoded = lines[2]
    pass_secret = lines[3]

    # the character used for padding--with a block cipher such as AES, the value
    # you encrypt must be a multiple of BLOCK_SIZE in length.  This character is
    # used to ensure that your value is always a multiple of BLOCK_SIZE
    PADDING = '{'

    DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)

    # create a cipher object using the random secret
    user_cipher = AES.new(user_secret)
    pass_cipher = AES.new(pass_secret)

    # decode the encoded string
    user_decoded = DecodeAES(user_cipher, user_encoded)
    pass_decoded = DecodeAES(pass_cipher, pass_encoded)

    return (user_decoded, pass_decoded)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM