2016-09-28 10 views
1

現在、Spring OAuth認証サーバを使用していますが、現在はOAuth仕様の「scope」パラメータを使用していません。これは、Spring OAuth Authorization Serverが認証コードを要求するときにスコープを明示的に要求する必要があるため、少し苦労しました。 DefaultOAuth2RequestValidatorからSpring OAuth認証サーバにスコープが必要

if (requestScopes.isEmpty()) { 
    throw new InvalidScopeException("Empty scope (either the client or the user is not allowed the requested scopes)"); 
} 

これがOAuth 2.0の仕様に対して直接行く:

 
4.1.1. Authorization Request 

    The client constructs the request URI by adding the following 
parameters to the query component of the authorization endpoint URI 
using the "application/x-www-form-urlencoded" format, per Appendix B: 

    response_type 
      REQUIRED. Value MUST be set to "code". 

    client_id 
      REQUIRED. The client identifier as described in Section 2.2. 

    redirect_uri 
      OPTIONAL. As described in Section 3.1.2. 

    scope 
      OPTIONAL. The scope of the access request as described by 
      Section 3.3. 

    state 
      RECOMMENDED. An opaque value used by the client to maintain 
      state between the request and callback. The authorization 
      server includes this value when redirecting the user-agent back 
      to the client. The parameter SHOULD be used for preventing 
      cross-site request forgery as described in Section 10.12. 

は春認証サーバーがこれを行う理由は、明示的な理由はありますか?バリデーターを私のものに置き換えることができると私は知っていますが、これがレガシーな理由のためにこの方法以外の理解を欠いている場合のために、これがなぜデフォルトであるのか不思議です。

ありがとうございます。

仕様を次の代替実装をお探しの方に

EDITは、ここに私のものです。クライアントが特定のスコープに制限されている場合は、要求されたスコープが必要であり、要求されたスコープが割り当てられたクライアントスコープのリストに含まれている必要があるかどうかを確認するだけです。クライアントにスコープが割り当てられていない場合、この実装ではスコープの使用が許可されているものとみなされます(リソースについても同様です)。それが本当に正しいかどうかはまだ分かりません。そうでない場合は教えてください。

import java.util.Set; 

import org.apache.commons.collections.CollectionUtils; 
import org.springframework.security.oauth2.common.exceptions.InvalidScopeException; 
import org.springframework.security.oauth2.provider.AuthorizationRequest; 
import org.springframework.security.oauth2.provider.ClientDetails; 
import org.springframework.security.oauth2.provider.TokenRequest; 

public class OAuth2RequestValidator 
    implements org.springframework.security.oauth2.provider.OAuth2RequestValidator { 

    @Override 
    public void validateScope(final AuthorizationRequest authorizationRequest, 
     final ClientDetails client) 
     throws InvalidScopeException { 
    this.validateScope(authorizationRequest.getScope(), client.getScope()); 
    } 

    @Override 
    public void validateScope(final TokenRequest tokenRequest, final ClientDetails client) 
     throws InvalidScopeException { 
    this.validateScope(tokenRequest.getScope(), client.getScope()); 
    } 

    private void validateScope(
     final Set<String> requestScopes, 
     final Set<String> clientScopes) { 
    if (!CollectionUtils.isEmpty(clientScopes)) { 
     if (CollectionUtils.isEmpty(requestScopes)) { 
     throw new InvalidScopeException(
      "Empty scope (either the client or the user is " 
       + "not allowed the requested scopes)"); 
     } 

     for (final String scope : requestScopes) { 
     if (!clientScopes.contains(scope)) { 
      throw new InvalidScopeException("Invalid scope: " + scope, clientScopes); 
     } 
     } 
    } 
    } 

} 
+0

これは既にバグとして報告されているようですが、応答はありません。 https://github.com/spring-projects/spring-security-oauth/issues/775 – loesak

答えて

0

スコープがクライアントによって供給されていない場合DefaultOAuth2RequestFactoryによれば、クライアントのために登録された範囲が使用されます。

DefaultOAuth2RequestFactory.java

private Set<String> extractScopes(Map<String, String> requestParameters, String clientId) { 
    Set<String> scopes = OAuth2Utils.parseParameterList(requestParameters.get(OAuth2Utils.SCOPE)); 
    ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientId); 

    if ((scopes == null || scopes.isEmpty())) { 
     // If no scopes are specified in the incoming data, use the default values registered with the client 
     // (the spec allows us to choose between this option and rejecting the request completely, so we'll take the 
     // least obnoxious choice as a default). 
     scopes = clientDetails.getScope(); 
    } 

    if (checkUserScopes) { 
     scopes = checkUserScopes(scopes, clientDetails); 
    } 
    return scopes; 
} 

ですから、「すべて」のデフォルトスコープか何か似て例えばを使用してクライアントを設定することができ

public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
    clients.inMemory() 
      .withClient("client").secret("secret") 
      .authorizedGrantTypes("authorization_code", "client_credentials") 
      .scopes("all"); 
+0

この時点ではスコープを使用していませんが、スコープをクライアントに割り当てることは、将来的にアプリケーションにスコープ制限が追加されたときにアクションを実行する権限を持つ可能性があります。問題はSpring Security OAuth(私が使用していたバージョン/使用していたバージョン)が仕様を守っていない理由の詳細でした。 – loesak

関連する問題