|
31 | 31 | import javax.crypto.spec.PBEKeySpec; |
32 | 32 | import javax.crypto.spec.SecretKeySpec; |
33 | 33 |
|
| 34 | +import com.loginradius.sdk.api.advanced.ConfigurationApi; |
34 | 35 | import com.loginradius.sdk.helper.LoginRadiusValidator; |
35 | 36 | import com.loginradius.sdk.models.responsemodels.otherobjects.ServiceInfoModel; |
36 | 37 |
|
37 | 38 | public class Sott { |
38 | 39 |
|
39 | 40 | private static String initVector = "tu89geji340t89u2"; |
40 | | - |
| 41 | + private static String plaintext=""; |
41 | 42 | // <summary> |
42 | 43 | // Generate SOTT Manually. |
43 | 44 | // </summary> |
44 | 45 | // <param name="service">ServiceInfoModel Model Class containing Definition of payload for SOTT</param> |
45 | 46 | // <param name="apiKey">LoginRadius Api Key.</param> |
46 | 47 | // <param name="apiSecret">LoginRadius Api Secret.</param> |
| 48 | + // <param name="getLrServerTime">If true it will call LoginRadius Get Server Time Api and fetch basic server information and server time information which is useful when generating an SOTT token..</param> |
47 | 49 | // <returns>Sott data</returns> |
48 | 50 |
|
49 | | - public static String getSott(ServiceInfoModel service,String apiKey,String apiSecret) throws java.lang.Exception { |
| 51 | + public static String getSott(ServiceInfoModel service,String apiKey,String apiSecret,boolean getLrServerTime) throws java.lang.Exception { |
50 | 52 | String secret = !LoginRadiusValidator.isNullOrWhiteSpace(apiSecret)? apiSecret:LoginRadiusSDK.getApiSecret(); |
51 | 53 | String key = !LoginRadiusValidator.isNullOrWhiteSpace(apiKey)? apiKey:LoginRadiusSDK.getApiKey(); |
52 | 54 | String token = null; |
53 | | - |
| 55 | + String timeDifference =(service!=null && !LoginRadiusValidator.isNullOrWhiteSpace(service.getSott().getTimeDifference())) ?service.getSott().getTimeDifference():"10"; |
| 56 | + |
54 | 57 | if (service != null && !LoginRadiusValidator.isNullOrWhiteSpace(service.getSott().getStartTime()) && !LoginRadiusValidator.isNullOrWhiteSpace(service.getSott().getEndTime()) ) { |
55 | | - String plaintext = service.getSott().getStartTime() + "#" + key + "#" + service.getSott().getEndTime(); |
56 | | - token = encrypt(plaintext, secret); |
57 | | - } else { |
58 | | - |
59 | | - String timeDifference =(service!=null && !LoginRadiusValidator.isNullOrWhiteSpace(service.getSott().getTimeDifference())) ?service.getSott().getTimeDifference():"10"; |
| 58 | + plaintext = service.getSott().getStartTime() + "#" + key + "#" + service.getSott().getEndTime(); |
| 59 | + } |
| 60 | + |
| 61 | + if(getLrServerTime) { |
| 62 | + ConfigurationApi config = new ConfigurationApi(); |
| 63 | + config.getServerInfo(Integer.parseInt(timeDifference), new AsyncHandler < ServiceInfoModel > () { |
| 64 | + |
| 65 | + @Override |
| 66 | + public void onFailure(ErrorResponse errorResponse) {} |
| 67 | + |
| 68 | + @Override |
| 69 | + public void onSuccess(ServiceInfoModel service) { |
| 70 | + |
| 71 | + if(service!=null && !LoginRadiusValidator.isNullOrWhiteSpace( service.getSott().getStartTime()) && !LoginRadiusValidator.isNullOrWhiteSpace( service.getSott().getEndTime()) ) { |
| 72 | + plaintext = service.getSott().getStartTime() + "#" + key + "#" + service.getSott().getEndTime(); |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + if(plaintext.isEmpty()) { |
60 | 81 | TimeZone timeZone = TimeZone.getTimeZone("UTC"); |
61 | 82 | Calendar calendar = Calendar.getInstance(timeZone); |
62 | 83 | DateFormat dateFormat = new SimpleDateFormat("yyyy/M/d H:m:s", Locale.US); |
63 | 84 | dateFormat.setTimeZone(timeZone); |
64 | | - String plaintext = dateFormat.format(calendar.getTime()) + "#" + key + "#"; |
| 85 | + plaintext = dateFormat.format(calendar.getTime()) + "#" + key + "#"; |
65 | 86 | calendar.add(Calendar.MINUTE, Integer.parseInt(timeDifference)); |
66 | 87 | plaintext += dateFormat.format(calendar.getTime()); |
67 | | - token = encrypt(plaintext, secret); |
68 | | - |
69 | 88 | } |
70 | | - |
| 89 | + token = encrypt(plaintext, secret); |
| 90 | + |
71 | 91 | String finalToken = token + "*" + createMd5(token); |
72 | 92 | return finalToken; |
73 | 93 | } |
74 | 94 |
|
| 95 | + |
| 96 | + // <summary> |
| 97 | + // Generate SOTT Manually. |
| 98 | + // </summary> |
| 99 | + // <param name="service">ServiceInfoModel Model Class containing Definition of payload for SOTT</param> |
| 100 | + // <param name="apiKey">LoginRadius Api Key.</param> |
| 101 | + // <param name="apiSecret">LoginRadius Api Secret.</param> |
| 102 | + // <returns>Sott data</returns> |
| 103 | + @Deprecated |
| 104 | + public static String getSott(ServiceInfoModel service,String apiKey,String apiSecret) throws java.lang.Exception { |
| 105 | + String secret = !LoginRadiusValidator.isNullOrWhiteSpace(apiSecret)? apiSecret:LoginRadiusSDK.getApiSecret(); |
| 106 | + String key = !LoginRadiusValidator.isNullOrWhiteSpace(apiKey)? apiKey:LoginRadiusSDK.getApiKey(); |
| 107 | + String token = null; |
| 108 | + |
| 109 | + if (service != null && !LoginRadiusValidator.isNullOrWhiteSpace(service.getSott().getStartTime()) && !LoginRadiusValidator.isNullOrWhiteSpace(service.getSott().getEndTime()) ) { |
| 110 | + String plaintext = service.getSott().getStartTime() + "#" + key + "#" + service.getSott().getEndTime(); |
| 111 | + token = encrypt(plaintext, secret); |
| 112 | + } else { |
| 113 | + |
| 114 | + String timeDifference =(service!=null && !LoginRadiusValidator.isNullOrWhiteSpace(service.getSott().getTimeDifference())) ?service.getSott().getTimeDifference():"10"; |
| 115 | + TimeZone timeZone = TimeZone.getTimeZone("UTC"); |
| 116 | + Calendar calendar = Calendar.getInstance(timeZone); |
| 117 | + DateFormat dateFormat = new SimpleDateFormat("yyyy/M/d H:m:s", Locale.US); |
| 118 | + dateFormat.setTimeZone(timeZone); |
| 119 | + String plaintext = dateFormat.format(calendar.getTime()) + "#" + key + "#"; |
| 120 | + calendar.add(Calendar.MINUTE, Integer.parseInt(timeDifference)); |
| 121 | + plaintext += dateFormat.format(calendar.getTime()); |
| 122 | + token = encrypt(plaintext, secret); |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + String finalToken = token + "*" + createMd5(token); |
| 127 | + return finalToken; |
| 128 | + } |
| 129 | + |
75 | 130 | private static String encrypt(final String plaintext, final String passPhrase) throws NoSuchAlgorithmException, |
76 | 131 | InvalidKeySpecException, UnsupportedEncodingException, NoSuchPaddingException, InvalidKeyException, |
77 | 132 | InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { |
|
0 commit comments