簡體   English   中英

OAuth2是否允許使用非密碼或自定義憑據進行授權?

[英]Does OAuth2 allow for authorization using non-password or custom credentials?

我正在使用Spring Security OAuth2。 客戶端應用程序(我們擁有)發出“密碼”授權請求,該請求傳遞用戶的用戶名和密碼。 就像草案規定的一樣。

我需要這種機制來支持其他類型的憑證,例如卡號,PIN,甚至是預先認證的,不需要密碼的授權。

請記住,這些請求只能由特權client_id允許,該特權client_id只能從我們擁有的應用程序中使用。

戴夫,謝謝你的快速反應。 我實際上找到了一個完美的解決方案,一個你參與的解決方案。它與“自定義授權”令牌制作者有關... https://jira.spring.io/browse/SECOAUTH-347

如果我更新了我可能已經知道的相當舊的1.0.0.M5版本。

我的方法是使用支持自定義授權類型的類(我將其稱為“studentCard”)擴展AbstractTokenGranter 一旦身份驗證請求到達此處,我就像ResourceOwnerPasswordTokenGranter一樣檢查參數列表,而是查找我的自定義“cardNumber”參數。 然后,我將自己的基於身份的UsernamePasswordAuthenticationToken版本傳遞給我的AuthenticationProvider,后者知道如何根據身份證對用戶進行身份驗證。

這是我提出的自定義令牌granter類:

public class StudentCardTokenGranter extends AbstractTokenGranter {
    private static final String         GRANT_TYPE = "studentCard";

    private final AuthenticationManager authenticationManager;

    public StudentCardTokenGranter(AuthenticationManager authenticationManager,
        AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService) {
    super(tokenServices, clientDetailsService, GRANT_TYPE);
    this.authenticationManager = authenticationManager;
    }

    @Override
    protected OAuth2Authentication getOAuth2Authentication(AuthorizationRequest clientToken) {

    Map<String, String> parameters = clientToken.getAuthorizationParameters();
    String cardNumber = parameters.get("cardNumber");

    Authentication userAuth = new StudentCardAuthenticationToken(cardNumber);
    try {
        userAuth = authenticationManager.authenticate(userAuth);
    } catch (BadCredentialsException e) {
        // If the username/password are wrong the spec says we should send 400/bad grant
        throw new InvalidGrantException(e.getMessage());
    }
    if (userAuth == null || !userAuth.isAuthenticated()) {
        throw new InvalidGrantException("Could not authenticate student: " + cardNumber);
    }

    return new OAuth2Authentication(clientToken, userAuth);
    }
}

我的授權服務器配置:

<!-- Issues tokens for both client and client/user authorization requests -->
<oauth:authorization-server client-details-service-ref="clientDetails" token-services-ref="tokenServices">
    <oauth:refresh-token />
    <oauth:client-credentials />
    <oauth:password authentication-manager-ref="myUserManager" />
    <oauth:custom-grant token-granter-ref="studentCardGranter" />
</oauth:authorization-server>
<bean id="studentCardGranter" class="com.api.security.StudentCardTokenGranter">
    <constructor-arg name="authenticationManager" ref="myUserManager" />
    <constructor-arg name="tokenServices" ref="tokenServices" />
    <constructor-arg name="clientDetailsService" ref="clientDetails" />
</bean>

規范沒有明確允許直接在客戶端和auth服務器之間直接,基於非密碼的用戶令牌交換。 我認為將密碼授權擴展到其他形式的身份驗證是很自然的。 這符合規范的精神,如果沒有這封信,那么如果你擁有這種關系的雙方,就沒有太大的錯誤。 Spring OAuth並沒有明確支持以這種方式擴展密碼授權的任何東西,但它並不難做(它實際上只是關於/ token端點的安全性)。 我看到的另一種方法是堅持使用密碼授予協議,但是將“密碼”設置為客戶端只能通過知道用戶已經通過其中一種替代方式進行身份驗證才能獲得的一次性令牌。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM