-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogleUserDirectory.cfc
363 lines (281 loc) · 17.3 KB
/
GoogleUserDirectory.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<cfcomponent displayname="Google User Directory" extends="farcry.core.packages.security.UserDirectory" output="false" key="GUD">
<cffunction name="getLoginForm" access="public" output="false" returntype="string" hint="Returns the form component to use for login">
<cfreturn "gudLogin" />
</cffunction>
<cffunction name="authenticate" access="public" output="false" returntype="struct" hint="Attempts to process a user. Runs every time the login form is loaded.">
<cfset var stResult = structnew() />
<cfset var stTokens = structnew() />
<cfset var stTokenInfo = structnew() />
<cfset var oUser = application.fapi.getContentType("gudUser") />
<cfif structkeyexists(url,"error")>
<cfset stResult.userid = "" />
<cfset stResult.authenticated = false />
<cfset stResult.message = url.error />
<cfelseif isDefined("url.type") and url.type eq "gudLogin" and structkeyexists(url,"code")>
<cftry>
<cfset stTokens = validate(code=url.code, redirectURL=application.security.userdirectories.gud.getRedirectURL()) />
<!--- Get Google access information --->
<cfparam name="session.security.ga" default="#structnew()#" >
<cfset session.security.ga[hash(stTokens.user_id)] = stTokens />
<!--- If there isn't a gudUser record, create one --->
<cfset stUser = oUser.getByUserID(stTokens.user_id) />
<cfif structisempty(stUser)>
<cfset stUser = oUser.getData(createuuid()) />
<cfset stUser.userid = stTokens.user_id />
<cfif structkeyexists(stTokens,"refresh_token")>
<cfset stUser.refreshToken = stTokens.refresh_token />
</cfif>
<cfset stUser.providerEmail = stTokens.profile.email />
<cfset stUser.providerDomain = listlast(stUser.providerEmail,"@") />
<cfset oUser.setData(stProperties=stUser) />
<cfelse>
<cfset session.security.ga[hash(stTokens.user_id)].refresh_token = stUser.refreshToken />
</cfif>
<cfset stResult.authenticated = "true" />
<cfset stResult.userid = stTokens.user_id />
<cfset stResult.ud = "GUD" />
<cfcatch>
<cfset application.fc.lib.error.logData(application.fc.lib.error.normalizeError(cfcatch)) />
<cfset stResult.authenticated = "false" />
<cfset stResult.userid = "" />
<cfset stResult.message = "Error while logging into Google: #cfcatch.message#" />
</cfcatch>
</cftry>
</cfif>
<cfreturn stResult />
</cffunction>
<cffunction name="validate" access="public" output="false" returntype="struct" hint="Validates a google authentication code">
<cfargument name="code" type="string" required="true" />
<cfargument name="redirectURL" type="string" required="true" />
<cfset var stTokens = getTokens(arguments.code, application.fapi.getConfig('GUD', 'clientID'), application.fapi.getConfig('GUD', 'clientSecret'), arguments.redirectURL, application.fapi.getConfig('GUD', 'proxy')) />
<cfset var stTokenInfo = getTokenInfo(application.fapi.getConfig('GUD', 'clientID'), stTokens.access_token, application.fapi.getConfig('GUD', 'proxy')) />
<cfset structAppend(stTokens, {
"user_id" = stTokenInfo.user_id,
"profile" = getGoogleProfile(stTokens.access_token, application.fapi.getConfig('GUD', 'proxy'))
}) />
<cfreturn stTokens />
</cffunction>
<cffunction name="getUserGroups" access="public" output="false" returntype="array" hint="Returns the groups that the specified user is a member of">
<cfargument name="UserID" type="string" required="true" hint="The user being queried" />
<cfset var qGroups = "" />
<cfset var aGroups = arraynew(1) />
<cfset var stUser = application.fapi.getContentType(typename="gudUser").getByUserID(arguments.userID) />
<cfquery datasource="#application.dsn#" name="qGroups">
select title
from #application.dbowner#gudGroup
where objectid in (
select <cfif application.dbtype eq "mysql">`data`<cfelse>data</cfif>
from #application.dbowner#gudUser_aGroups
where parentid=<cfqueryparam cfsqltype="cf_sql_varchar" value="#stUser.objectid#" />
)
or objectid in (
select parentid
from #application.dbowner#gudGroup_aDomains
where <cfif application.dbtype eq "mysql">`data`<cfelse>data</cfif>=<cfqueryparam cfsqltype="cf_sql_varchar" value="*" />
or <cfif application.dbtype eq "mysql">`data`<cfelse>data</cfif>=<cfqueryparam cfsqltype="cf_sql_varchar" value="#stUser.providerDomain#" />
or <cfif application.dbtype eq "mysql">`data`<cfelse>data</cfif>=<cfqueryparam cfsqltype="cf_sql_varchar" value="#stUser.providerEmail#" />
)
</cfquery>
<cfloop query="qGroups">
<cfset arrayappend(aGroups,title) />
</cfloop>
<cfreturn listtoarray(valuelist(qGroups.title)) />
</cffunction>
<cffunction name="getAllGroups" access="public" output="false" returntype="array" hint="Returns all the groups that this user directory supports">
<cfset var qGroups = "" />
<cfset var aGroups = arraynew(1) />
<cfquery datasource="#application.dsn#" name="qGroups">
select *
from #application.dbowner#gudGroup
order by title
</cfquery>
<cfloop query="qGroups">
<cfset arrayappend(aGroups,title) />
</cfloop>
<cfreturn aGroups />
</cffunction>
<cffunction name="getGroupUsers" access="public" output="false" returntype="array" hint="Returns all the users in a specified group">
<cfargument name="group" type="string" required="true" hint="The group to query" />
<cfset var qUsers = "" />
<cfquery datasource="#application.dsn#" name="qUsers">
select userid
from #application.dbowner#gudUser
where objectid in (
select parentid
from #application.dbowner#gudUser_aGroups ug
inner join
#application.dbowner#gudGroup g
on ug.<cfif application.dbtype eq "mysql">`data`<cfelse>data</cfif>=g.objectid
where g.title=<cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.group#" />
or objectid in (
select parentid
from #application.dbowner#gudGroup_aDomains
where <cfif application.dbtype eq "mysql">`data`<cfelse>data</cfif>=<cfqueryparam cfsqltype="cf_sql_varchar" value="*" />
or <cfif application.dbtype eq "mysql">`data`<cfelse>data</cfif>=gudUser.providerDomain
or <cfif application.dbtype eq "mysql">`data`<cfelse>data</cfif>=gudUser.providerEmail
)
)
</cfquery>
<cfreturn listtoarray(valuelist(qUsers.userid)) />
</cffunction>
<cffunction name="getProfile" access="public" output="false" returntype="struct" hint="Returns profile data available through the user directory">
<cfargument name="userid" type="string" required="true" hint="The user directory specific user id" />
<cfargument name="currentprofile" type="struct" required="false" hint="The current user profile" />
<cfset var stProfile = structnew() />
<cfset var useridHash = hash(arguments.userid) />
<cfif isdefined("session.security.ga.#useridHash#") and not isdefined("session.security.ga.#useridHash#.profile")>
<cfset session.security.ga[useridHash].profile = getGoogleProfile(access_token=session.security.ga[useridHash].access_token,proxy=application.fapi.getConfig('GUD', 'proxy')) />
</cfif>
<cfif isdefined("session.security.ga") AND structKeyExists(session.security.ga, useridHash) AND structKeyExists(session.security.ga[useridHash], "profile")>
<cfset stProfile.firstname = session.security.ga[useridHash].profile.given_name />
<cfset stProfile.lastname = session.security.ga[useridHash].profile.family_name />
<cfset stProfile.emailaddress = session.security.ga[useridHash].profile.email />
<cfset stProfile.label = "#stProfile.firstname# #stProfile.lastname#" />
<cfset stProfile.avatar = replace(session.security.ga[useridHash].profile.picture, "https://", "//") />
</cfif>
<cfset stProfile.override = true />
<cfreturn stProfile />
</cffunction>
<cffunction name="isEnabled" access="public" output="false" returntype="boolean" hint="Returns true if this user directory is active. This function can be overridden to check for the existence of config settings.">
<cfreturn len(application.fapi.getConfig('GUD', 'clientID', '')) and len(application.fapi.getConfig('GUD', 'clientSecret', '')) />
</cffunction>
<cffunction name="parseProxy" access="private" output="false" returntype="struct">
<cfargument name="proxy" type="string" required="true" />
<cfset var stResult = structnew() />
<cfif len(arguments.proxy)>
<cfif listlen(arguments.proxy,"@") eq 2>
<cfset stResult.login = listfirst(arguments.proxy,"@") />
<cfset stResult.proxyUser = listfirst(stResult.login,":") />
<cfset stResult.proxyPassword = listlast(stResult.login,":") />
<cfelse>
<cfset stResult.proxyUser = "" />
<cfset stResult.proxyPassword = "" />
</cfif>
<cfset stResult.server = listlast(arguments.proxy,"@") />
<cfset stResult.proxyServer = listfirst(stResult.server,":") />
<cfif listlen(stResult.server,":") eq 2>
<cfset stResult.proxyPort = listlast(stResult.server,":") />
<cfelse>
<cfset stResult.proxyPort = "80" />
</cfif>
</cfif>
<cfreturn stResult />
</cffunction>
<!---
From http://code.google.com/apis/analytics/docs/gdata/v3/gdataAuthorization.html:
1) When you create your application, you register it with Google. Google then provides information you'll need later, such as a client ID and a client secret.
2) Activate the Google Analytics API in the Services pane of the Google APIs Console. (If it isn't listed in the Console, then skip this step.)
3) When your application needs access to user data, it asks Google for a particular scope of access. ***
4) Google displays an OAuth dialog to the user, asking them to authorize your application to request some of their data.
5) If the user approves, then Google gives your application a short-lived access token.
6) Your application requests user data, attaching the access token to the request.
7) If Google determines that your request and the token are valid, it returns the requested data.
--->
<cffunction name="getAuthorisationURL" access="public" output="false" returntype="string">
<cfargument name="clientid" type="string" required="true" />
<cfargument name="redirectURL" type="string" required="true" />
<cfargument name="scope" type="string" required="false" default="profile email" />
<cfargument name="state" type="string" required="false" default="" />
<cfargument name="prompt" type="string" required="false" default="consent" />
<cfreturn "https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=#arguments.clientid#&redirect_uri=#urlencodedformat(arguments.redirectURL)#&scope=#urlencodedformat(arguments.scope)#&access_type=offline&state=#urlencodedformat(arguments.state)#&prompt=#arguments.prompt#" />
</cffunction>
<cffunction name="getTokens" access="private" output="false" returntype="struct">
<cfargument name="authorizationCode" type="string" required="true" />
<cfargument name="clientID" type="string" required="true" />
<cfargument name="clientSecret" type="string" required="true" />
<cfargument name="redirectURL" type="string" required="true" />
<cfargument name="proxy" type="string" required="false" default="" />
<cfset var cfhttp = structnew() />
<cfset var stResult = structnew() />
<cfset var stAttr = structnew() />
<cfset stAttr.url = "https://accounts.google.com/o/oauth2/token" />
<cfset stAttr.method = "POST" />
<cfif len(arguments.proxy)>
<cfset structappend(stAttr,parseProxy(arguments.proxy)) />
</cfif>
<cfhttp attributeCollection="#stAttr#">
<cfhttpparam type="formfield" name="code" value="#arguments.authorizationCode#" />
<cfhttpparam type="formfield" name="client_id" value="#arguments.clientID#" />
<cfhttpparam type="formfield" name="client_secret" value="#arguments.clientSecret#" />
<cfhttpparam type="formfield" name="redirect_uri" value="#arguments.redirectURL#" />
<cfhttpparam type="formfield" name="grant_type" value="authorization_code" />
</cfhttp>
<cfif not cfhttp.statuscode eq "200 OK">
<cfset throwError(message="Error accessing Google API: #cfhttp.statuscode#",endpoint="https://accounts.google.com/o/oauth2/token",response=trim(cfhttp.filecontent),args=arguments,stAttr=stAttr) />
</cfif>
<cfset stResult = deserializeJSON(cfhttp.FileContent.toString()) />
<cfset stResult.access_token_expires = dateadd("s",stResult.expires_in,now()) />
<cfreturn stResult />
</cffunction>
<cffunction name="getAccessToken" access="public" output="false" returntype="string">
<cfargument name="refresh_token" type="string" required="false" />
<cfargument name="access_token" type="string" required="true" />
<cfargument name="access_token_expires" type="date" required="true" />
<cfargument name="proxy" type="string" required="false" default="" />
<cfset var cfhttp = structnew() />
<cfset var stResult = structnew() />
<cfset var stProxy = parseProxy(arguments.proxy) />
<cfif isdefined("arguments.refresh_token") and datecompare(arguments.access_token_expires,now()) lt 0>
<cfhttp url="https://accounts.google.com/o/oauth2/token" method="POST" attributeCollection="#stProxy#">
<cfhttpparam type="formfield" name="refresh_token" value="#arguments.refreshToken#" />
<cfhttpparam type="formfield" name="client_id" value="#arguments.clientID#" />
<cfhttpparam type="formfield" name="client_secret" value="#arguments.clientSecret#" />
<cfhttpparam type="formfield" name="grant_type" value="refresh_token" />
</cfhttp>
<cfif not cfhttp.statuscode eq "200 OK">
<cfset throwError(message="Error accessing Google API: #cfhttp.statuscode#",endpoint="https://accounts.google.com/o/oauth2/token",response=cfhttp.filecontent,argumentCollection=arguments) />
</cfif>
<cfset stResult = deserializeJSON(cfhttp.FileContent.toString()) />
<cfreturn stResult.access_token />
<cfelseif not isdefined("arguments.refresh_token")>
<cfset throwError(message="Error accessing Google API: access token has expired and no refresh token is available",endpoint="https://accounts.google.com/o/oauth2/token",response=cfhttp.filecontent,argumentCollection=arguments) />
</cfif>
<cfreturn arguments.access_token />
</cffunction>
<cffunction name="getTokenInfo" access="private" output="false" returntype="struct">
<cfargument name="clientID" type="string" required="true" />
<cfargument name="accessToken" type="string" required="true" />
<cfargument name="proxy" type="string" required="false" default="" />
<cfset var cfhttp = structnew() />
<cfset var stResult = structnew() />
<cfset var stProxy = parseProxy(arguments.proxy) />
<cfhttp url="https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=#arguments.accessToken#" method="GET" attributeCollection="#stProxy#" />
<cfif not cfhttp.statuscode eq "200 OK">
<cfset throwError(message="Error accessing Google API: #cfhttp.statuscode#",endpoint="https://www.googleapis.com/oauth2/v1/tokeninfo",response=cfhttp.filecontent,argumentCollection=arguments) />
</cfif>
<cfset stResult = deserializeJSON(cfhttp.FileContent.toString()) />
<cfif structkeyexists(stResult,"error")>
<cfset throwError(message="Error accessing Google API: #stResult.error#",endpoint="https://www.googleapis.com/oauth2/v1/tokeninfo",response=cfhttp.filecontent,argumentCollection=arguments) />
<cfelseif stResult.audience neq arguments.clientID>
<cfset throwError(message="Error accessing Google API: Authorisation is for the wrong application",endpoint="https://www.googleapis.com/oauth2/v1/tokeninfo",response=cfhttp.filecontent,argumentCollection=arguments) />
</cfif>
<cfreturn stResult />
</cffunction>
<cffunction name="getGoogleProfile" access="private" output="false" returntype="struct">
<cfargument name="accessToken" type="string" required="true" />
<cfargument name="proxy" type="string" required="false" default="" />
<cfset var cfhttp = structnew() />
<cfset var stResult = structnew() />
<cfset var stProxy = parseProxy(arguments.proxy) />
<cfhttp url="https://www.googleapis.com/oauth2/v1/userinfo" method="GET" attributeCollection="#stProxy#">
<cfhttpparam type="header" name="Authorization" value="Bearer #arguments.accessToken#" />
</cfhttp>
<cfif not cfhttp.statuscode eq "200 OK">
<cfset throwError(message="Error accessing Google API: #cfhttp.statuscode#",endpoint="https://www.googleapis.com/oauth2/v1/userinfo",response=cfhttp.filecontent,argumentCollection=arguments) />
</cfif>
<cfset stResult = deserializeJSON(cfhttp.FileContent.toString()) />
<cfreturn stResult />
</cffunction>
<cffunction name="getRedirectURL" access="public" output="false" returntype="string" hint="For use with getAuthorisationURL and getRefreshToken">
<cfreturn "#application.fc.lib.seo.getCanonicalProtocol()#://#cgi.http_host##application.url.webroot#/index.cfm?type=gudLogin&view=displayLogin" />
</cffunction>
<cffunction name="throwError" access="private" output="false" returntype="void">
<cfargument name="message" type="string" required="true" />
<cfset var stLog = application.fc.lib.error.collectRequestInfo() />
<cfset structappend(stLog,arguments) />
<cfset stLog.stack = application.fc.lib.error.getStack(true,false,1) />
<cfset stLog.logtype = "GUD" />
<cfset application.fc.lib.error.logData(stLog,false,false) />
<cfthrow message="#arguments.message#" detail="#serializeJSON(arguments)#" />
</cffunction>
</cfcomponent>