-
Notifications
You must be signed in to change notification settings - Fork 5
/
access_token.cfc
264 lines (224 loc) · 7.09 KB
/
access_token.cfc
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
* @displayname oauth2 client access token
* @output false
* @hint The oauth2 access token object.
* @authors Joshua Rountree
* @website http://github.com/joshuairl/oauth2.cfc
* @purpose A ColdFusion Component to manage authentication using the OAuth2 protocol.
**/
component accessors="true" {
/**
* @getter true
* @setter true
* @type oauth2.client
* @hint The client component
**/
property name="client_object";
/**
* @getter true
* @setter true
* @type string
* @hint The access token for your requests.
**/
property name="token_prop";
/**
* @getter true
* @setter true
* @type string
* @hint The access token expiration time
**/
property name="expires_in";
/**
* @getter true
* @setter true
* @type string
* @hint the access token expires at
**/
property name="expires_at";
/**
* @getter true
* @setter true
* @type struct
* @hint The parameters for access token
**/
property name="params";
/**
* @getter true
* @setter true
* @type struct
* @hint The options for access token
**/
property name="options";
/**
* @getter true
* @setter true
* @type string
* @hint The refresh_token
**/
property name="refresh_token";
public oauth2.access_token function init(oauth2.client client_object,string token,struct opts = {}) {
var options = arguments.opts;
var loc = {};
setclient_object(arguments.client_object);
setToken_prop(arguments.token);
if(structKeyExists(arguments,'opts')) {
arrayEach(["refresh_token","expires_in","expires_at"],function(key,index) {
if (structKeyExists(options,key)) {
evaluate("set#key#(options[key])"); // probably a proper way to do this I'm sure.
structDelete(options,key);
}
});
}
if(structKeyExists(options,'expires') AND !structKeyExists(variables,'expires_in')) {
setExpires_in(options.expires);
}
if(!isNumeric(getExpires_at())) {
if(isNumeric(getExpires_in())) {
setExpires_at(getEpochTime() + getEpochTime(getExpires_in()));
}
}
setOptions({
'mode':structKeyExists(options,'mode') ? structDeleteAndReturn(options,'mode') : 'header',
'header_format':structKeyExists(options,'header_format') ? structDeleteAndReturn(options,'header_format') : 'Bearer %s',
'param_name':structKeyExists(options,'param_name') ? structDeleteAndReturn(options,'param_name') : 'access_token'
});
setParams(options);
return this;
}
public function from_hash(oauth2.client client_object, struct props) {
var newHash = duplicate(arguments.props);
var accessToken = arguments.props.access_token;
var client_object = {};
structDelete(arguments.props,'access_token');
var client_object = init(arguments.client_object,accessToken,arguments.props,newHash);
return client_object;
}
private function getEpochTime() {
var datetime = 0;
if (ArrayLen(Arguments) is 0) {
datetime = Now();
}
else {
if (IsDate(Arguments[1])) {
datetime = Arguments[1];
} else {
return arguments[1];
}
}
return DateDiff("s", "January 1 1970 00:00", datetime);
}
private any function getParam(key) {
if (structKeyExists(getParams(),key)) {
return getParams()[key];
} else {
return false;
};
}
public function does_expire() {
!isEmpty(getExpires_at());
}
public function is_expired() {
return (does_expire() && (getExpires_at() < getEpochTime()))
}
public oauth2.access_token function refresh(params = {}) {
var opts = arguments.params;
var client_object = getClient_object();
if (isEmpty(getRefresh_token())) {
throw(type="oauth2.no_refresh_token",message="You attempted to refresh a token without a `refresh_token`");
}
structAppend(opts,{
'client_id': client_object.getId(),
'client_secret': client_object.getSecret(),
'grant_type': 'refresh_token',
'refresh_token': getRefresh_token()
},true);
new_token = client_object.get_token(opts);
new_token.setOptions(getOptions());
if(isEmpty(new_token.getRefresh_token())) {
new_token.setRefresh_token(getRefresh_token());
}
return new_token;
}
public struct function to_hash() {
var currentParams = getParams();
structAppend(currentParams,{
'access_token': getToken_prop(),
'refresh_token': getRefresh_token(),
'expires_at': getExpires_at()
},true)
return currentParams;
}
public function request(verb,path,opts = {}) {
var client_object = getClient_object();
arguments.opts = structureOptions(arguments.opts);
return client_object.request(argumentCollection=arguments);
}
public function get(path,opts={}) {
arguments['verb'] = 'get';
return request(argumentCollection=arguments);
}
public function post(path,opts={}) {
arguments['verb'] = 'post';
return request(argumentCollection=arguments);
}
public function put(path,opts={}) {
arguments['verb'] = 'put';
return request(argumentCollection=arguments);
}
public function patch(path,opts={}) {
arguments['verb'] = 'patch';
return request(argumentCollection=arguments);
}
public function delete(path,opts={}) {
arguments['verb'] = 'delete';
return request(argumentCollection=arguments);
}
public function getHeaders() {
return {
'Authorization': replace(getOption('header_format'),'%s',getToken_prop())
}
}
//allows you to call getXXXX() on non-properties of this class that are stored in the params struct
public any function OnMissingMethod(string missingMethodName,struct missingMethodArguments) {
if (left(arguments.missingMethodName,3) EQ "get") {
//return value response for params object;
return getParam(replace(arguments.missingMethodName,'get',''));
} else {
return false;
}
}
include "utils/struct_delete_and_return.cfm";
public any function getOption(key) {
var options = getOptions();
if (structKeyExists(options,key)) {
return options[key];
} else {
throw "Option not available.";
}
}
private function structureOptions(struct opts) {
var newOptions = arguments.opts;
var selfOptions = getOptions();
switch (selfOptions.mode) {
case 'header':
newOptions['headers'] = !structKeyExists(newOptions,'headers') ? {} : newOptions['headers'];
structAppend(newOptions.headers,getHeaders());
break;
case 'query':
newOptions['params'] = !structKeyExists(newOptions,'params') ? {} : newOptions['params'];
newOptions['params'][selfOptions['param_name']] = token
break;
case 'body':
newOptions['body'] = !structKeyExists(newOptions,'body') ? {} : newOptions['body'];
if (isStruct(newOptions['body'])) {
newOptions['body'][selfOptions['param_name']] = token
} else {
listAppend(newOptions['body'],'&#selfOptions['param_name']#=#getToken_prop()#','&');
}
break;
default:
throw(type="oAuth2.invalid_mode",message="invalid `mode` option of `#selfOptions.mode#` in `access_token`");
}
return newOptions;
}
}