|
| 1 | +/** |
| 2 | + * Copyright (c) 2017 by CyberSource |
| 3 | + * Governing licence: https://github.com/CyberSource/cybersource-flex-samples/blob/master/LICENSE.md |
| 4 | + */ |
| 5 | +package com.cybersource.example; |
| 6 | + |
| 7 | +import com.cybersource.flex.sdk.FlexService; |
| 8 | +import com.cybersource.flex.sdk.FlexServiceFactory; |
| 9 | +import com.cybersource.flex.sdk.authentication.CGKCredentials; |
| 10 | +import com.cybersource.flex.sdk.exception.FlexException; |
| 11 | +import com.cybersource.flex.sdk.model.EncryptionType; |
| 12 | +import com.cybersource.flex.sdk.model.FlexPublicKey; |
| 13 | +import com.cybersource.flex.sdk.model.KeysRequestParameters; |
| 14 | +import com.cybersource.flex.sdk.repackaged.JSONObject; |
| 15 | +import javax.servlet.http.HttpSession; |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStream; |
| 18 | + |
| 19 | +public class FlexKeyProvider { |
| 20 | + |
| 21 | + private final FlexService flexService; |
| 22 | + |
| 23 | + FlexKeyProvider(InputStream resource) { |
| 24 | + MerchantCredentials merchantCredentials = null; |
| 25 | + try { |
| 26 | + merchantCredentials = new MerchantCredentials(resource); |
| 27 | + |
| 28 | + CGKCredentials credentials = new CGKCredentials(); |
| 29 | + credentials.setEnvironment(CGKCredentials.Environment.CAS); |
| 30 | + credentials.setMid(merchantCredentials.getMerchantId()); |
| 31 | + credentials.setKeyId(merchantCredentials.getKeyId()); |
| 32 | + credentials.setSharedSecret(merchantCredentials.getSharedSecret()); |
| 33 | + |
| 34 | + flexService = FlexServiceFactory.createInstance(credentials); |
| 35 | + } catch (FlexException fe) { |
| 36 | + throw new RuntimeException(fe); |
| 37 | + } catch (IOException e) { |
| 38 | + throw new RuntimeException(e); |
| 39 | + } finally { |
| 40 | + if (merchantCredentials != null) { |
| 41 | + merchantCredentials.destroy(); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public String bindFlexKeyToSession(HttpSession session) throws FlexException { |
| 47 | + KeysRequestParameters parameters = new KeysRequestParameters(EncryptionType.RsaOaep256, "http://localhost:8080"); |
| 48 | + FlexPublicKey flexPublicKey = flexService.createKey(parameters); |
| 49 | + session.setAttribute(FlexPublicKey.class.getName(), flexPublicKey); |
| 50 | + return new JSONObject(flexPublicKey.getJwk()).toString(); |
| 51 | + } |
| 52 | + |
| 53 | + public boolean verifyTokenResponse(HttpSession session, String flexResponse) throws FlexException { |
| 54 | + FlexPublicKey flexPublicKey = (FlexPublicKey) session.getAttribute(FlexPublicKey.class.getName()); |
| 55 | + return flexService.verify(flexPublicKey, flexResponse); |
| 56 | + } |
| 57 | + |
| 58 | +} |
0 commit comments