-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCAS-Auth.cfc
More file actions
133 lines (101 loc) · 4.75 KB
/
CAS-Auth.cfc
File metadata and controls
133 lines (101 loc) · 4.75 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
<cfcomponent hint="Slate Central Authentication Service (CAS) integration" output="false">
<!--- Built by Jason Quatrino on 8/25/2017 based on Slate CAS Authentication Documentation found at:
https://technolutions.zendesk.com/hc/en-us/articles/216174348-Slate-authentication-service#article-comments
--->
<cfset slateEndpoint = "https://Your.SlateDomainName.here/" />
<cfset serviceURI = "https://#cgi.server_name#" />
<cfset serviceURI &= cgi.SCRIPT_NAME />
<cffunction name="authUser" returntype="struct" output="false">
<cfargument name="uri" type="string" required="false" default="" hint="URI of the service you'd like to redirect to after authentication." />
<cfif Len(arguments.uri) EQ 0>
<cfset arguments.uri = serviceURI />
</cfif>
<cfset local.results = {} />
<cfset local.results.msg = "" />
<cfif structKeyExists(url,"ticket") AND Len(url.ticket)>
<cfset local.results = retrieveCredentials(ticket=url.ticket,uri=arguments.uri) />
<cfelse>
<cfif IsValid("url",arguments.uri)>
<cflocation url="#slateEndpoint#/account/cas/login?service=#arguments.uri#" addtoken="false" />
<cfelse>
<cfset local.results.msg &= " AuthUser ERROR: Bad URI provided." />
</cfif>
</cfif>
<cfreturn local.results />
</cffunction>
<cffunction name="retrieveCredentials" returntype="struct" output="false">
<cfargument name="ticket" type="string" required="true" />
<cfargument name="uri" type="string" required="false" />
<cfset local.results = {} />
<cfset local.results.msg = "" />
<cfset local.results.isAuth = false />
<cfset local.results.slateData = {} />
<cfif Len(arguments.uri) EQ 0>
<cfset arguments.uri = serviceURI />
</cfif>
<cfif Len(url.ticket)>
<cftry>
<cfhttp url="#slateEndpoint#account/cas/serviceValidate" method="get" result="local.httpResult">
<cfhttpparam type="url" name="service" value="#arguments.uri#" />
<cfhttpparam type="url" name="ticket" value="#arguments.ticket#" />
</cfhttp>
<cfcatch type="any">
<cfset local.results.msg &= "AuthUser HTTP ERROR: #cfcatch.message#" />
<cfreturn results />
</cfcatch>
</cftry>
<cfif structKeyExists(local.httpResult,"errordetail") AND Len(local.httpResult.errordetail)>
<cfset local.results.msg &= "retrieveCredentials HTTP ERROR: #local.httpResult.errordetail#" />
<cfelse>
<cfif structKeyExists(local.httpResult,"filecontent") AND Len(local.httpResult.filecontent)>
<cfif isValid("xml",local.httpResult.filecontent)>
<!--- parse results --->
<cfset local.results.slateData = parseSlateXML(XMLParse(local.httpResult.filecontent)) />
<!--- check whether authenticated properly --->
<cfset local.results.isAuth = isSuccessfulLogin(local.results.slateData) />
<cfif structKeyExists(local.results.slateData,"cas:AuthenticationFailure")>
<cfset local.results.msg &= local.results.slateData["cas:AuthenticationFailure"] />
</cfif>
<cfelse>
<cfset local.results.msg &= " retrieveCredentials File Content ERROR: Invalid XML." />
</cfif>
<cfelse>
<cfset local.results.msg &= " retrieveCredentials File Content ERROR: Unexpected result." />
</cfif>
</cfif>
<cfelse>
<cfset local.results.msg &= " retrieveCredentials ERROR: ticket not provided." />
</cfif>
<cfreturn local.results />
</cffunction>
<cffunction name="logout" output="false">
<cfhttp url="#slateEndpoint#account/cas/logout" method="get" result="local.httpResult">
</cfhttp>
</cffunction>
<cffunction name="parseSlateXML" returntype="struct" output="false">
<cfargument name="slateXML" type="xml" required="true" />
<cfset local.slateResults = {} />
<cfset local.resultNodes = arguments.slateXML.XmlRoot />
<cfif structKeyExists(local.resultNodes,"XmlChildren")>
<cfset local.slateResults = xmlChildrenParser(local.resultNodes.XmlChildren) />
</cfif>
<cfreturn local.slateResults />
</cffunction>
<cffunction name="xmlChildrenParser" returntype="struct" output="false">
<cfargument name="xmldata" type="xml" required="true">
<cfset local.returnStruct = {} />
<cfif isValid("array",arguments.xmldata)>
<cfloop array="#arguments.xmldata#" item="local.child">
<cfset local.returnStruct[local.child.xmlName] = local.child.xmlText />
<cfif structKeyExists(local.child,"XmlChildren") AND ArrayLen(local.child.XmlChildren)>
<cfset StructAppend(local.returnStruct,xmlChildrenParser(local.child.XmlChildren)) />
</cfif>
</cfloop>
</cfif>
<cfreturn local.returnStruct />
</cffunction>
<cffunction name="isSuccessfulLogin" returntype="boolean" output="false">
<cfargument name="slateConfig" type="struct" required="true" />
<cfreturn (structKeyExists(arguments.slateConfig,"cas:AuthenticationSuccess") ? true : false) />
</cffunction>
</cfcomponent>