2

I keep receiving an error when using grails oauth plugin

{"error":"unauthorized","error_description":"Full authentication is required to access this resource"}

Found out the authentication header was not set when making the token request

My Configuration is shown below

oauth {
 providers {
 passport {
  api = com.company.package.security.passport.api.PassportApi
  key = 'myproject'
  secret = 'secret'
  authorizationUrl = "http://${providerURL}/oauth/authorize"
  tokenEndpointUrl = "http://${providerURL}/oauth/token"
  callback = "http://{applicationContextPath}/project/oauth/passport/callback"
  successUri = '/login/passportSuccess'
  failureUri = '/login/passportFailure'
  signatureType = org.scribe.model.SignatureType.Header
  scope = 'profile'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  } 
 }
 debug = true
 connectTimeout = 5000
 receiveTimeout = 5000
}

I am using a Custom providerapi also shown below

 public class PassportApi extends DefaultApi20{

 private static final String AUTHORIZE_URL_WITH_STATE = "%s?   client_id=%s&client_secret=%s&redirect_uri=%s&state=%s&response_type=code&grant_type=authorization_code";
private static final String SCOPED_AUTHORIZE_URL_WITH_STATE = AUTHORIZE_URL_WITH_STATE + "&scope=%s";

@Override
public String getAccessTokenEndpoint() { 
    Properties appConfig = Holders.getConfig().toProperties();
    String tokenEndpointUrl = appConfig.getProperty("oauth.providers.passport.tokenEndpointUrl") + "";

     if(tokenEndpointUrl != null){

        return tokenEndpointUrl;

    }else{

        throw new RuntimeException("Passport API Token URL not Configured Please check config property: oauth.providers.passport.tokenEndpointUrl");

    }       
}

@Override
public String getAuthorizationUrl(OAuthConfig passportOAuthConfig) {

    Properties appConfig = Holders.getConfig().toProperties();
    String authorizationUrl = appConfig.getProperty("oauth.providers.passport.authorizationUrl");

    if(authorizationUrl != null){

        if(passportOAuthConfig.hasScope()){
                return String.format(SCOPED_AUTHORIZE_URL_WITH_STATE, authorizationUrl, passportOAuthConfig.getApiKey(), passportOAuthConfig.getApiSecret(), 
                        OAuthEncoder.encode(passportOAuthConfig.getCallback()),OAuthEncoder.encode("Yithe"),passportOAuthConfig.getScope());
        }else{
            return String.format(AUTHORIZE_URL_WITH_STATE,authorizationUrl, passportOAuthConfig.getApiKey(),
                    OAuthEncoder.encode(passportOAuthConfig.getCallback()), OAuthEncoder.encode("Yithe"));
        }
    }else{

        throw new RuntimeException("Passport API Authorization URL not Configured Please check config property: oauth.providers.passport.authorizationUrl");

    }
  }
}

Also, what is the proper way to make the second /oauth/token Request because if I use a normal POST setting Authorization Header : Basic client_id:client_secret(encoded using base 64), It works See this postman request

JohnTheBeloved
  • 2,323
  • 3
  • 19
  • 24

0 Answers0