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