【问题标题】:Is there an API in OpenSAML library to check expiration of SAML2 token?OpenSAML 库中是否有用于检查 SAML2 令牌到期的 API?
【发布时间】:2013-05-23 22:45:11
【问题描述】:

我正在使用 OpenSAML 库来生成 SAML2 令牌。我的印象是令牌的验证签名也会检查它的到期情况,显然情况并非如此。是否有库提供的 API 可用于检查过期? 就像下面代码sn-p中的checkIfExpired()

public static boolean validateSignature(String token, Credential credential)
    {
       try {

        InputStream in = new ByteArrayInputStream(token.getBytes());
        Document inCommonMDDoc = ppMgr.parse(in);
        AssertionUnmarshaller unmarshaller = new AssertionUnmarshaller();
        Assertion assertion = (Assertion) unmarshaller
                .unmarshall(inCommonMDDoc.getDocumentElement());
        SignatureValidator validator = new SignatureValidator(credential);
        try {
            validator.validate(assertion.getSignature());

             return checkIfExpired(assertion) ; // -- Checks if assertion has expired and return true/false

        } catch (ValidationException e) {
            log.error("Invalid Signature", e);
            return false;
        }
    } catch (Exception e) {
        log.error("Unable to perform Signature Validation", e);

    }
}

注意:如果 OpenSAML 已经有 API,我想避免手动操作。

【问题讨论】:

    标签: java opensaml


    【解决方案1】:

    判断断言是否过期的方法是检查断言中的条件。像这样的。

    if (assertion.getConditions().getNotBefore() != null && assertion.getConditions().getNotBefore().isAfterNow()) {
        throw new ValidationException("Condition states that assertion is not yet valid (is the server time correct?)");
    }
    
    if (assertion.getConditions().getNotOnOrAfter() != null
                    && (assertion.getConditions().getNotOnOrAfter().isBeforeNow() || assertion.getConditions().getNotOnOrAfter().isEqualNow())) {
        throw new ValidationException("Condition states that assertion is no longer valid (is the server time correct?)");
    }
    

    就我现在而言,没有更简单的方法可以做到这一点。正确的方法可能是编写一个验证器,也许扩展ConditionSpecValidator。此验证器不会自行验证所有条件

    【讨论】:

    • 谢谢 Stefan。这很有用。由于这是一个足够常见的用例,我希望他们将其合并到他们的标准 API 中。
    • 是的,正如我所说,他们有一个ConditionSpecValidator,但它似乎不完整。我会在他们的邮件列表上给他们发一封邮件,看看他们怎么说。发布一个缺少如此重要的东西的验证器似乎很奇怪。
    • 我一直在与开发团队联系,他们似乎在以后的版本中一起删除了验证器。这是邮件线程marc.info/?t=137354098500007&r=1&w=2
    猜你喜欢
    • 2018-12-19
    • 2021-12-21
    • 1970-01-01
    • 2023-03-27
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    相关资源
    最近更新 更多