OAuthToken.java
4.31 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
*/
package com.adobe.granite.auth.oauth.impl.helper;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class OAuthToken {
public static final int UNITIALIZED = 0;
public static final int UNAUTHORIZED = 1;
public static final int AUTHORIZED = 2;
public static final int ACCESS_TOKEN = 3;
private int _state;
private String _ck;
private String _key;
private String _secret;
private Map<String, Object> _attributes;
public OAuthToken() {
}
public OAuthToken(String ck) {
this._ck = ck;
}
public OAuthToken(String ck, String key, String secret) {
this(ck, key, secret, 0);
}
public OAuthToken(String ck, String key, String secret, int state) {
this._ck = ck;
this._key = key;
this._secret = secret;
this._state = state;
}
public int getState() {
return this._state;
}
void setState(int state) {
this._state = state;
}
public String getCk() {
return this._ck;
}
public String getKey() {
return this._key;
}
void setKey(String key) {
this._key = key;
}
public String getSecret() {
return this._secret;
}
void setSecret(String secret) {
this._secret = secret;
}
void set(int state, String key, String secret) {
this._state = state;
this._key = key;
this._secret = secret;
}
public boolean authorize(String key, String verifier) {
if (this._state < 2) {
if (this._state > 0 && this._key != null && this._key.equals(key)) {
this._state = 2;
if (verifier != null) {
this.setAttribute("oauth_verifier", verifier);
}
return true;
}
return false;
}
return true;
}
public boolean isAuthentic() {
return this._state == 3;
}
public Map<String, Object> getAttributes() {
return this._attributes;
}
public Object getAttribute(String key) {
return this._attributes == null ? null : this._attributes.get(key);
}
public void setAttribute(String key, Object value) {
if (this._attributes == null) {
this._attributes = new HashMap<String, Object>();
}
this._attributes.put(key, value);
}
public static OAuthToken fromJSON(String jsonString) {
try {
JSONObject json = new JSONObject(jsonString);
OAuthToken token = new OAuthToken();
token._state = json.optInt("st");
token._ck = json.optString("ck");
token._key = json.optString("k");
token._secret = json.optString("sk");
JSONObject attr = json.optJSONObject("a");
if (attr != null) {
HashMap<String, Object> attributes = new HashMap<String, Object>();
Iterator keys = attr.keys();
while (keys.hasNext()) {
String key = (String)keys.next();
attributes.put(key, attr.get(key));
}
token._attributes = attributes;
}
return token;
}
catch (JSONException je) {
return null;
}
}
public String toJSON() throws IOException {
try {
JSONObject json = new JSONObject();
json.put("st", this._state);
json.put("ck", (Object)this._ck);
if (this._key != null) {
json.put("k", (Object)this._key);
}
if (this._secret != null) {
json.put("sk", (Object)this._secret);
}
if (this._attributes != null) {
json.put("a", this._attributes);
}
return json.toString();
}
catch (JSONException je) {
throw new IOException(je.getMessage());
}
}
}