Example usage for org.springframework.security.oauth.provider ConsumerDetailsService loadConsumerByConsumerKey

List of usage examples for org.springframework.security.oauth.provider ConsumerDetailsService loadConsumerByConsumerKey

Introduction

In this page you can find the example usage for org.springframework.security.oauth.provider ConsumerDetailsService loadConsumerByConsumerKey.

Prototype

ConsumerDetails loadConsumerByConsumerKey(String consumerKey) throws OAuthException;

Source Link

Document

Load a consumer by the consumer key.

Usage

From source file:nl.surfnet.coin.api.service.JanusClientDetailsServiceTest.java

/**
 * Test to see if the cache works. Especially the fact that we store items in
 * the same cache with the same key for different return Objects:
 * ClientDetails and ConsumerDetails// ww  w  .  j av a 2  s. c om
 * 
 */
@Test
public void testCache() throws IOException {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(this.getClass());

    ClientDetailsService clientDetailsService = (ClientDetailsService) ctx.getBean("janusClientDetailsService");
    Janus janus = (Janus) ctx.getBean("janus");

    when(janus.getEntityIdsByMetaData(Metadata.OAUTH_CONSUMERKEY, "consumerkey"))
            .thenReturn(Collections.singletonList("sp-entity-id"));
    when(janus.getMetadataByEntityId("sp-entity-id")).thenReturn(getMetadata());
    ClientDetails clientDetails = clientDetailsService.loadClientByClientId("consumerkey");
    assertEquals("secret", clientDetails.getClientSecret());

    // when we do this a second time the cache should kick in
    when(janus.getEntityIdsByMetaData(Metadata.OAUTH_CONSUMERKEY, "consumerkey"))
            .thenThrow(new RuntimeException("Cache did not kick in"));
    clientDetailsService.loadClientByClientId("consumerkey");

    /*
     * now do the same for the loading of ConsumerDetails (and yes, this lengthy
     * test including the reset is necessary) to make sure we don't hit the
     * cache for loading the ConsumerDetails as we store both in the same cache
     * with potentially the same key (e.g. the consumerkey) resulting in
     * java.lang.ClassCastException:
     * nl.surfnet.coin.api.oauth.ExtendedBaseClientDetails cannot be cast to
     * org.springframework.security.oauth.provider.ConsumerDetails without a
     * custom key generator
     */
    reset(janus);
    when(janus.getEntityIdsByMetaData(Metadata.OAUTH_CONSUMERKEY, "consumerkey"))
            .thenReturn(Collections.singletonList("sp-entity-id"));
    when(janus.getMetadataByEntityId("sp-entity-id")).thenReturn(getMetadata());

    ConsumerDetailsService consumerDetailsService = (ConsumerDetailsService) clientDetailsService;
    ConsumerDetails consumerDetails = consumerDetailsService.loadConsumerByConsumerKey("consumerkey");
    assertEquals("secret", ((SharedConsumerSecret) consumerDetails.getSignatureSecret()).getConsumerSecret());

    when(janus.getEntityIdsByMetaData(Metadata.OAUTH_CONSUMERKEY, "consumerkey"))
            .thenThrow(new RuntimeException("Cache did not kick in"));
    consumerDetailsService.loadConsumerByConsumerKey("consumerkey");
}