【问题标题】:Find a json/dict of the client certificate from a HTTPS request从 HTTPS 请求中查找客户端证书的 json/dict
【发布时间】:2021-10-20 22:14:13
【问题描述】:

我有一个客户端证书 - 我正在构建的 Flask 端点的服务器证书。目前 HTTPS 请求可以通过,我可以在服务器端打印客户端证书的详细信息,如下所示:

@app.route('/predict', methods=['POST'])
def predict():
    res = "hi"
    status = False

    if request.url.startswith('https://'):
        client_cert = request.headers["CLIENT_CERT"]
        res = client_cert["subject"]
        print(client_cert)

它打印出来自请求的客户端证书信息:

"{'subject': ((('countryName', 'DE'),), (('stateOrProvinceName', 'BW'),), (('localityName', 'Walldorf'),), (('organizationName', 'MYCOMPANY'),), (('organizationalUnitName', 'MYDEPARTMENT'),), (('commonName', '*.domainname.com'),)), 'issuer': ((('countryName', 'DE'),), (('stateOrProvinceName', 'BW'),), (('localityName', 'Walldorf'),), (('organizationName', 'MYCOMPANY'),), (('organizationalUnitName', 'MYDEPARTMENT'),), (('commonName', 'SAMPLE CA'),)), 'version': 1, 'serialNumber': '01', 'notBefore': 'Aug 12 07:21:25 2021 GMT', 'notAfter': 'Aug 12 07:21:25 2022 GMT'}"

我希望检索上述字符串中的 commonName 条目以进行进一步验证,因为我只想允许采用我期望的特定格式的 commonName

如您所见,从请求的标头中检索客户端证书会以 字符串 的形式提供证书。因为这不是正确的 json 字符串,所以我不能使用 json.loads 将字符串解析为字典。有什么好的方法可以解析这个字符串以便我可以得到commonName 值,或者有没有其他好的方法来访问请求的客户端证书?

谢谢!

【问题讨论】:

    标签: python ssl request openssl certificate


    【解决方案1】:

    响应的格式似乎定义明确。在这种情况下,这将实现 OP 的目标。毫无疑问,有一个 RE 可以解决问题,但这既简单又有效:-

    def getCommonNames(s):
        rv = []
        offset = 0
        try:
            while True:
                i = s.index('commonName', offset)
                offset = i + 14
                j = s.index("'", offset)
                rv.append(s[offset:j])
        except Exception:
            pass
        return rv
    
    
    S = "{'subject': ((('countryName', 'DE'),), (('stateOrProvinceName', 'BW'),), (('localityName', 'Walldorf'),), (('organizationName', 'MYCOMPANY'),), (('organizationalUnitName', 'MYDEPARTMENT'),), (('commonName', '*.domainname.com'),)), 'issuer': ((('countryName', 'DE'),), (('stateOrProvinceName', 'BW'),), (('localityName', 'Walldorf'),), (('organizationName', 'MYCOMPANY'),), (('organizationalUnitName', 'MYDEPARTMENT'),), (('commonName', 'SAMPLE CA'),)), 'version': 1, 'serialNumber': '01', 'notBefore': 'Aug 12 07:21:25 2021 GMT', 'notAfter': 'Aug 12 07:21:25 2022 GMT'}"
    print(getCommonNames(S))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-02
      • 2019-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多