forked from pcalcado/java-api-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToken.java
More file actions
154 lines (131 loc) · 4.99 KB
/
Token.java
File metadata and controls
154 lines (131 loc) · 4.99 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
package com.soundcloud.api;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Represents an OAuth2 access/refresh token pair.
*/
public class Token implements Serializable {
private static final long serialVersionUID = 766168501082045382L;
public static final String ACCESS_TOKEN = "access_token";
public static final String REFRESH_TOKEN = "refresh_token";
public static final String SCOPE = "scope";
public static final String EXPIRES_IN = "expires_in";
public static final String SCOPE_DEFAULT = "*";
/** Special scope for signup / password recovery */
public static final String SCOPE_SIGNUP = "signup";
public static final String SCOPE_PLAYCOUNT = "playcount";
/** Don't expire access token - returned tokens won't include a refresh token */
public static final String SCOPE_NON_EXPIRING = "non-expiring";
// XXX these should be private
public String access, refresh, scope;
public long expiresIn;
public final Map<String, String> customParameters = new HashMap<String, String>();
/**
* Constructs a new token with the given sub-tokens
* @param access A token used by the client to make authenticated requests on behalf of the resource owner.
* @param refresh A token used by the client to obtain a new access token without having
* to involve the resource owner.
*/
public Token(String access, String refresh) {
this(access, refresh, null);
}
public Token(String access, String refresh, String scope) {
this.access = access;
this.refresh = refresh;
this.scope = scope;
}
/**
* Construct a new token from a JSON response
* @param json the json response
* @throws IOException JSON format error
*/
public Token(JSONObject json) throws IOException {
try {
for (Iterator it = json.keys(); it.hasNext(); ) {
String key = it.next().toString();
if (ACCESS_TOKEN.equals(key)) {
access = json.getString(ACCESS_TOKEN);
} else if (REFRESH_TOKEN.equals(key)) {
// refresh token won't be set if we don't expire
refresh = json.getString(REFRESH_TOKEN);
expiresIn = System.currentTimeMillis() + json.getLong(EXPIRES_IN) * 1000;
} else if (SCOPE.equals(key)) {
scope = json.getString(SCOPE);
} else {
// custom parameter
customParameters.put(key, json.getString(key));
}
}
} catch (JSONException e) {
throw new IOException(e.getMessage());
}
}
/** Invalidates the access token */
public void invalidate() {
this.access = null;
}
/**
* @return null or the date of expiration of this token
*/
public Date getExpiresIn() {
return expiresIn == 0 ? null : new Date(expiresIn);
}
public boolean defaultScoped() {
return scoped(SCOPE_DEFAULT);
}
/** @return has token the signup scope ("signup") */
public boolean signupScoped() {
return scoped(SCOPE_SIGNUP);
}
public boolean scoped(String scope) {
if (this.scope != null) {
for (String s : this.scope.split(" "))
if (scope.equals(s)) return true;
}
return false;
}
/** @return is this token valid */
public boolean valid() {
return access != null && (scoped(SCOPE_NON_EXPIRING) || refresh != null);
}
/** indicates whether this token was issued after a signup */
public String getSignup() {
return customParameters.get("soundcloud:user:sign-up");
}
@Override
public String toString() {
return "Token{" +
"access='" + access + '\'' +
", refresh='" + refresh + '\'' +
", scope='" + scope + '\'' +
", expires=" + getExpiresIn() +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
if (o instanceof Token) {
Token token = (Token) o;
if (access != null ? !access.equals(token.access) : token.access != null) return false;
if (refresh != null ? !refresh.equals(token.refresh) : token.refresh != null) return false;
if (scope != null ? !scope.equals(token.scope) : token.scope != null) return false;
return true;
} else {
return super.equals(o);
}
}
@Override
public int hashCode() {
int result = access != null ? access.hashCode() : 0;
result = 31 * result + (refresh != null ? refresh.hashCode() : 0);
result = 31 * result + (scope != null ? scope.hashCode() : 0);
return result;
}
}