private String buildCallbackUrlForImplicitResponseType( final J2EContext context, final Authentication authentication, final Service service, final String redirectUri) throws Exception { final String state = authentication.getAttributes().get(OAuthConstants.STATE).toString(); final String nonce = authentication.getAttributes().get(OAuthConstants.NONCE).toString(); final AccessToken accessToken = generateAccessToken(service, authentication, context); logger.debug("Generated Oauth access token: {}", accessToken); final URIBuilder builder = new URIBuilder(redirectUri); final StringBuilder stringBuilder = new StringBuilder(); stringBuilder .append(OAuthConstants.ACCESS_TOKEN) .append('=') .append(accessToken.getId()) .append('&') .append(OAuthConstants.TOKEN_TYPE) .append('=') .append(OAuthConstants.TOKEN_TYPE_BEARER) .append('&') .append(OAuthConstants.EXPIRES_IN) .append('=') .append(casProperties.getTicket().getTgt().getTimeToKillInSeconds()); if (StringUtils.isNotBlank(state)) { stringBuilder .append('&') .append(OAuthConstants.STATE) .append('=') .append(EncodingUtils.urlEncode(state)); } if (StringUtils.isNotBlank(nonce)) { stringBuilder .append('&') .append(OAuthConstants.NONCE) .append('=') .append(EncodingUtils.urlEncode(nonce)); } builder.setFragment(stringBuilder.toString()); return builder.toString(); }
private String decryptCredential(final String cred) { try { final PrivateKeyFactoryBean factory = new PrivateKeyFactoryBean(); factory.setAlgorithm("RSA"); factory.setLocation(new ClassPathResource("RSA1024Private.p8")); factory.setSingleton(false); final PrivateKey privateKey = factory.getObject(); logger.debug("Initializing cipher based on [{}]", privateKey.getAlgorithm()); final Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm()); logger.debug("Decoding value [{}]", cred); final byte[] cred64 = EncodingUtils.decodeBase64(cred); logger.debug("Initializing decrypt-mode via private key [{}]", privateKey.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, privateKey); final byte[] cipherData = cipher.doFinal(cred64); return new String(cipherData); } catch (final Exception e) { throw Throwables.propagate(e); } }