OpenConextOauth1TokenServices extends RandomValueProviderTokenServices implements InitializingBean { @Override protected void storeToken(String value, OAuthProviderTokenImpl token) { Assert.notNull(token, "Token cannot be null"); Assert.notNull(value, "token value cannot be null"); Authentication userAuthentication = token.getUserAuthentication(); String userId = null; if (token.isAccessToken()) { String consumerKey = token.getConsumerKey(); ConsumerDetails consumerDetails = consumerDetailsService.loadConsumerByConsumerKey(consumerKey); if (consumerDetails instanceof OpenConextConsumerDetails) { OpenConextConsumerDetails extendedBaseConsumerDetails = (OpenConextConsumerDetails) consumerDetails; if (userAuthentication instanceof PreAuthenticatedAuthenticationToken) { PreAuthenticatedAuthenticationToken pre = (PreAuthenticatedAuthenticationToken) userAuthentication; Object principal = pre.getPrincipal(); if (principal instanceof ClientMetaDataUser) { ((ClientMetaDataUser) principal).setClientMetaData(extendedBaseConsumerDetails.getClientMetaData()); userId = ((ClientMetaDataUser) principal).getUsername(); } else if (principal instanceof SAMLAuthenticationToken) { ((SAMLAuthenticationToken) principal).setClientMetaData(extendedBaseConsumerDetails .getClientMetaData()); userId = ((SAMLAuthenticationToken) principal).getName(); } else { throw new RuntimeException("The principal on the PreAuthenticatedAuthenticationToken is of the type '" + (principal != null ? principal.getClass() : "null") + "'. Required is a (sub)class of ClientMetaDataUser or a (sub)class of SAMLAuthenticationToken"); } } else if (userAuthentication instanceof SAMLAuthenticationToken) { SAMLAuthenticationToken samlToken = (SAMLAuthenticationToken) userAuthentication; samlToken.setClientMetaData(extendedBaseConsumerDetails.getClientMetaData()); userId = samlToken.getName(); } else { throw new RuntimeException("The userAuthentication is of the type '" + (userAuthentication != null ? userAuthentication.getClass() : "null") + "'. Required is a (sub)class of PreAuthenticatedAuthenticationToken or SAMLAuthenticationToken"); } } else { throw new RuntimeException("The consumerDetails is of the type '" + (consumerDetails != null ? consumerDetails.getClass() : "null") + "'. Required is a (sub)class of ExtendedBaseConsumerDetails"); } } jdbcTemplate.update(deleteTokenSql, value); jdbcTemplate.update(insertTokenSql, value, token.getCallbackUrl(), token.getVerifier(), token.getSecret(), token.getConsumerKey(), userId, token.isAccessToken(), token.getTimestamp(), SerializationUtils.serialize(userAuthentication)); }  @Override void afterPropertiesSet(); void setDataSource(DataSource dataSource);  }
@Test public void storeNullToken() { try { s.storeToken(null, null); fail(); } catch (IllegalArgumentException e) { } try { s.storeToken("", null); fail(); } catch (IllegalArgumentException e) { } try { s.storeToken(null, new OAuthProviderTokenImpl()); fail(); } catch (IllegalArgumentException e) { } }
@Test public void storeTokenHappy() { s.storeToken("footoken", buildToken()); assertEquals("One token should be inserted", 1, getJdbcTemplate().queryForInt("select count(*) from oauth1_tokens")); assertEquals("token id should match inserted one", "footoken", getJdbcTemplate().queryForObject("select * from oauth1_tokens where token = ?", new OpenConextOauth1TokenServices.OAuthProviderTokenRowMapper(), "footoken").getValue()); }