-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomAuth.java
More file actions
102 lines (81 loc) · 2.54 KB
/
CustomAuth.java
File metadata and controls
102 lines (81 loc) · 2.54 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//$Id$
package com.zoho.abtest.report;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.zoho.abtest.common.ZABModel;
import com.zoho.abtest.utility.ZABUtil;
import com.zoho.abtest.webhook.Webhook;
import com.zoho.abtest.webhook.WebhookConstants;
/**
* Base authentication class
*
*/
public abstract class CustomAuth extends ZABModel {
private static final long serialVersionUID = 1L;
private static final Logger LOGGER = Logger.getLogger(CustomAuth.class.getName());
public abstract Webhook authentication(Webhook object) throws Exception;
public static String replaceToken(String url,String token){
try{
if(url.contains(WebhookConstants.REFRESH_TOKEN)){
url= url.replace("{{"+WebhookConstants.REFRESH_TOKEN+"}}",token);
}
if(url.contains(WebhookConstants.ACCESS_TOKEN)){
url= url.replace("{{"+WebhookConstants.ACCESS_TOKEN+"}}",token);
}
}
catch (Exception e) {
e.printStackTrace();
}
return url;
}
/**
* @param hs
* @param token
* @return replacing token names in headers with its value
*/
public static HashMap<String,String> replaceToken(HashMap<String,String> hs ,String token){
try{
Iterator<Map.Entry<String, String>> iterator = hs.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
if(entry.getValue().contains(WebhookConstants.REFRESH_TOKEN)){
hs.put(entry.getKey(),entry.getValue().replace("{{"+WebhookConstants.REFRESH_TOKEN+"}}", token));
break;
}
if(entry.getValue().contains(WebhookConstants.ACCESS_TOKEN)){
hs.put(entry.getKey(),entry.getValue().replace("{{"+WebhookConstants.ACCESS_TOKEN+"}}", token));
break;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return hs;
}
/**
* @param s
* @return converting string to hashmap
* @throws JSONException
*/
public static HashMap<String,String> hashHeaders(String s) throws JSONException{
HashMap<String,String> h=new HashMap();
if(s.length()==0){
return null;}
JSONArray jsonarray=new JSONArray(s);
for(int i=0;i<jsonarray.length();i++){
JSONObject json=(JSONObject) jsonarray.get(i);
Iterator<?> keys=json.keys();
while(keys.hasNext()){
String key=(String) keys.next();
h.put(key,json.getString(key));
}
}
return h;
}
}