Saml2TokenGranter extends AbstractTokenGranter { protected Authentication validateRequest(TokenRequest request) { if(request == null || request.getRequestParameters() == null) { throw new InvalidGrantException("Missing token request object"); } if(request.getRequestParameters().get("grant_type") == null) { throw new InvalidGrantException("Missing grant type"); } if(!GRANT_TYPE_SAML2_BEARER.equals(request.getRequestParameters().get("grant_type"))) { throw new InvalidGrantException("Invalid grant type"); } if (securityContextAccessor.isUser()) { return SecurityContextHolder.getContext().getAuthentication(); } throw new InvalidGrantException("User authentication not found"); } Saml2TokenGranter(final AuthorizationServerTokenServices tokenServices,
                             final MultitenantClientServices clientDetailsService,
                             final OAuth2RequestFactory requestFactory,
                             final SecurityContextAccessor securityContextAccessor);   }
@Test public void test_not_authenticated() { when(authentication.isAuthenticated()).thenReturn(false); granter.validateRequest(tokenRequest); }
@Test public void test_not_a_user_authentication() { when(authentication.isAuthenticated()).thenReturn(true); when(authentication.getUserAuthentication()).thenReturn(null); granter.validateRequest(tokenRequest); }
@Test public void invalid_grant_type() { SecurityContextHolder.getContext().setAuthentication(authentication); exception.expect(InvalidGrantException.class); exception.expectMessage("Invalid grant type"); requestParameters.put(GRANT_TYPE, "password"); tokenRequest.setRequestParameters(requestParameters); granter.validateRequest(tokenRequest); }
@Test public void test_no_user_authentication() { SecurityContextHolder.getContext().setAuthentication(authentication); exception.expect(InvalidGrantException.class); exception.expectMessage("User authentication not found"); when(mockSecurityAccessor.isUser()).thenReturn(false); granter.validateRequest(tokenRequest); }
@Test(expected = InvalidGrantException.class) public void test_missing_token_Request() { granter.validateRequest(null); }