-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOauth.java
More file actions
143 lines (119 loc) · 4.49 KB
/
Oauth.java
File metadata and controls
143 lines (119 loc) · 4.49 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//$Id$
package com.zoho.abtest.report;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.adventnet.ds.query.Column;
import com.adventnet.ds.query.Criteria;
import com.adventnet.ds.query.QueryConstants;
import com.adventnet.persistence.DataObject;
import com.adventnet.persistence.Row;
import com.zoho.abtest.OAUTH;
import com.zoho.abtest.OAUTH_SYS;
import com.zoho.abtest.webhook.Webhook;
import com.zoho.logs.common.jsonorg.JSONObject;
public class Oauth extends CustomAuth {
private static final long serialVersionUID = 1L;
private static final Logger LOGGER = Logger.getLogger(Oauth.class.getName());
private static StringBuffer getToken(String accessurl){
StringBuffer response = new StringBuffer();
String s=null;
try {
accessurl=accessurl.trim();
URL obj = new URL(accessurl);
s=obj.getProtocol();
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST"); //NO I18N
con.setDoOutput(true);
con.setConnectTimeout(3000);
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
public static Webhook userAuth(Webhook webhook) throws Exception{
try{
HashMap<String,String> headers=hashHeaders(webhook.getHeaders());
Criteria c = new Criteria(new Column(OAUTH.TABLE,OAUTH.WEBHOOK_ID),webhook.getWebhookId(), QueryConstants.EQUAL);
DataObject dobj=getRow(OAUTH.TABLE,c);
Iterator it =dobj.getRows(OAUTH.TABLE);
if(it.hasNext())
{
Row row=(Row) it.next();
String accessurl=(String)row.get(OAUTH.ACCESS_TOKEN_URL);
String refreshtoken=(String)row.get(OAUTH.REFRESH_TOKEN);
accessurl=replaceToken(accessurl,refreshtoken);
headers=replaceToken(headers, refreshtoken);
String tokens=getToken(accessurl).toString();
JSONObject json=new JSONObject(tokens);
String access_token =json.get("access_token").toString();
//Getting access token using access url and refresh token
webhook.setWebhookUrl(replaceToken(webhook.getWebhookUrl(),access_token));
headers=replaceToken(headers,access_token);
JSONObject json1=new JSONObject(headers);
webhook.setHeaders(json1.toString());
}
}
catch (Exception e) {
e.printStackTrace();
}
return webhook;
}
public static Webhook sysAuth(Webhook webhook) throws Exception{
try{
HashMap<String,String> headers=hashHeaders(webhook.getHeaders());
String webhookname=webhook.getWebhookLinkName();
Criteria c = new Criteria(new Column(OAUTH_SYS.TABLE,OAUTH_SYS.WEBHOOK_NAME),webhookname, QueryConstants.EQUAL);
DataObject dobj=getRow(OAUTH_SYS.TABLE,c);
Iterator it =dobj.getRows(OAUTH_SYS.TABLE);
if(it.hasNext())
{
Row row=(Row) it.next();
String accessurl=webhook.getAccessTokenUrl();
String refreshtoken=(String)row.get(OAUTH_SYS.REFRESH_TOKEN);
accessurl=replaceToken(accessurl,refreshtoken);
String tokens=getToken(accessurl).toString(); //post method
JSONObject json=new JSONObject(tokens);
String access_token =json.get("access_token").toString();
//Getting access token using access url and refresh token
webhook.setWebhookUrl(replaceToken(webhook.getWebhookUrl(),access_token));
headers=replaceToken(headers,access_token);
headers=replaceToken(headers, refreshtoken);
JSONObject json1=new JSONObject(headers);
webhook.setHeaders(json1.toString());
}
}
catch (Exception e) {
e.printStackTrace();
}
return webhook;
}
public Webhook authentication(Webhook webhook){
final Logger LOGGER = Logger.getLogger(Oauth.class.getName());
try{
if(!webhook.isSystem()){
webhook=userAuth(webhook);
}
else{
webhook=sysAuth(webhook);
}
}
catch(Exception e){
LOGGER.log(Level.SEVERE, e.getMessage(),e);
}
return webhook;
}
}