forked from zendesk/zendesk_jwt_sso_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassic_asp_jwt_with_ad.asp
More file actions
executable file
·266 lines (212 loc) · 9.01 KB
/
Copy pathclassic_asp_jwt_with_ad.asp
File metadata and controls
executable file
·266 lines (212 loc) · 9.01 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
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
<!--#include file="jwt.asp" -->
<%
Dim sKey, sSubdomain, sLdapReaderUsername, sLdapReaderPassword, sLoginErrorMessage
Dim dAttributes, sParameter, sRedirectUrl, sExternalIdField, sOrganizationField, sTagsField
Dim sPhotoURLField, sPhoneField, sRoleField, sCustomRoleIDField, sLocaleField, sLocaleIDField
Dim dUserFields, sUserFieldKey1, sUserFieldValue1, sUserFieldKey2, sUserFieldValue2
' This script relies on the classic ASP implementation from https://github.com/zendesk/classic_asp_jwt
' Once you have that in place, proceed to configure the script as instructed in the documentation below
'
' 1. Place this script in a folder on your IIS, and disable anonymous access for the script.
' 2. Add a valid user/password for the LDAP lookups by setting the variables sLdapReaderUsername
' and sLdapReaderPassword below.
'
' Please note that the ISS does not have to be in the DMZ or in any way accessible via the internet,
' as the authentication is driven via browser redirects.
'
' Please refer to https://support.zendesk.com/entries/23675367 for an in depth explanation on how
' remote authentication with JWT works.
' Set your shared secret and Zendesk subdomain
sKey = ""
sSubdomain = ""
' Credentials for a domain user for LDAP access
sLdapReaderUsername = ""
sLdapReaderPassword = ""
' The below fields can OPTIONALLY be sent to Zendesk. In order to do so, set each variable to the field
' name on the local user record. E.g. sExternalIdField = "sAMAccountName" and so forth.
sExternalIdField = ""
sOrganizationField = ""
sTagsField = ""
sPhotoUrlField = ""
sPhoneField = ""
sRoleField = ""
' If the sRoleField is set to 'agent', you can specify a custom role ID (Enterprise only) with the below field
sCustomRoleIDField = ""
' Use sLocaleField for end-users, and sLocaleIDField for agents. Must be a valid integer from the available locales in your Zendesk.
' For a list of valid locales and localeIDs, see: http://developer.zendesk.com/documentation/rest_api/locales.html
sLocaleField = ""
sLocaleIDField = ""
' To use custom user fields, specify the 'Field key' value from Zendesk with sUserFieldKey#, and set sUserFieldValue#
' to the field name on the local user record. For more info, see: https://support.zendesk.com/entries/24740352
' To add additional custom user fields, add additional entries here as well as both commented areas in GetAuthenticatedUser()
sUserFieldKey1 = ""
sUserFieldValue1 = ""
sUserFieldKey2 = ""
sUserFieldValue2 = ""
' Debug Mode Switch
' Set this to True to turn on Debug Mode. Set it to False to use in production.
Dim dM
dM = False
Set dAttributes = GetAuthenticatedUser()
If dAttributes Is Nothing Then
Response.Write("Could not login to Zendesk. Please contact your administrator.")
Debug "Account '" & Request.ServerVariables("LOGON_USER") & "' not found."
ElseIf dAttributes("email") = "" Then
Response.write("Could not login to Zendesk. Please contact your administrator.")
Debug "User '" & Request.ServerVariables("LOGON_USER") & "' has no email."
Else
sParameter = JWTTokenForUser(dAttributes, dUserFields)
sRedirectUrl = "https://" & sSubdomain & ".zendesk.com/access/jwt?jwt=" & sParameter
If dM Then
Debug "Redirecting to " & sRedirectUrl
Else
Response.redirect sRedirectUrl
End If
End If
%>
<%
Function JWTTokenForUser(dAttributes, dUserFields)
Dim i, aAttributeKeys, aUserFieldKeys
dAttributes.Add "jti", UniqueString
dAttributes.Add "iat", SecsSinceEpoch
If Not isempty(Request.QueryString("return_to")) Then
dAttributes.Add "return_to", Request.QueryString("return_to")
End If
aAttributeKeys = dAttributes.keys
For i = 0 To dAttributes.Count-1
Debug("Attribute " & aAttributeKeys(i) & ": " & dAttributes(aAttributeKeys(i)))
Next
If dUserFields.Count = 0 Then
Debug("'user_fields' not in use")
Else
aUserFieldKeys = dUserFields.keys
For i = 0 to dUserFields.Count-1
Debug("Custom User Field " & aUserFieldKeys(i) & ": " & dUserFields(aUserFieldKeys(i)))
Next
End If
JWTTokenForUser = JWTEncode(dAttributes, dUserFields, sKey)
End Function
Function Encode_UTF8(astr)
utftext = ""
For n = 1 To Len(astr)
c = AscW(Mid(astr, n, 1))
If c < 128 Then
utftext = utftext + Mid(astr, n, 1)
ElseIf ((c > 127) And (c < 2048)) Then
utftext = utftext + Chr(((c \ 64) Or 192))
'((c>>6)|192);
utftext = utftext + Chr(((c And 63) Or 128))
'((c&63)|128);}
Else
utftext = utftext + Chr(((c \ 144) Or 234))
'((c>>12)|224);
utftext = utftext + Chr((((c \ 64) And 63) Or 128))
'(((c>>6)&63)|128);
utftext = utftext + Chr(((c And 63) Or 128))
'((c&63)|128);
End If
Next
Encode_UTF8 = utftext
End Function
Function Debug(sMessage)
If dM Then
response.Write("[DEBUG] " & sMessage & "<br/>")
End If
End Function
Function GetAuthenticatedUser()
Dim sDomainContainer, sUsername, sFields
' Retrieve authenticated user
sUsername = split(Request.ServerVariables("LOGON_USER"),"\")(1)
Debug Request.ServerVariables("LOGON_USER") & " - should be of the form DOMAIN\username - if blank, your IIS probably allows anonymous access to this file."
Set rootDSE = GetObject("LDAP://RootDSE")
Set oConn = CreateObject("ADODB.Connection")
sDomainContainer = rootDSE.Get("defaultNamingContext")
Debug "DomainContainer: " & sDomainContainer
oConn.Provider = "ADSDSOObject"
oConn.properties("user id") = sLdapReaderUsername
oConn.properties("password") = sLdapReaderPassword
oConn.Open "ADs Provider"
sFields = "mail,displayName"
If sExternalIdField > "" Then
sFields = sFields & "," & sExternalIdField
End If
If sOrganizationField > "" Then
sFields = sFields & "," & sOrganizationField
End If
If sTagsField > "" Then
sFields = sFields & "," & sTagsField
End If
If sPhotoUrlField > "" Then
sFields = sFields & "," & sPhotoUrlField
End If
If sPhoneField > "" Then
sFields = sFields & "," & sPhoneField
End If
If sRoleField > "" Then
sFields = sFields & "," & sRoleField
End If
If sCustomRoleIDField > "" Then
sFields = sFields & "," & sCustomRoleIDField
End If
If sLocaleField > "" Then
sFields = sFields & "," & sLocaleField
End If
If sLocaleIDField > "" Then
sFields = sFields & "," & sLocaleIDField
End If
' If you need more custom user fields, add additional entries below as well as above in the settings.
If sUserFieldValue1 > "" Then
sFields = sFields & "," & sUserFieldValue1
End If
If sUserFieldValue2 > "" Then
sFields = sFields & "," & sUserFieldValue2
End If
sQuery = "<LDAP://" & sDomainContainer & ">;(sAMAccountName=" & sUsername & ");adspath," & sFields & ";subtree"
Set userRS = oConn.Execute(sQuery)
If Not userRS.EOF and not err then
Set dAttributes = Server.CreateObject("Scripting.Dictionary")
Set dUserFields = Server.CreateObject("Scripting.Dictionary")
dAttributes.Add "name", Encode_UTF8(userRS("displayName").Value)
dAttributes.Add "email", userRS("mail").Value
If sExternalIdField > "" Then
dAttributes.Add "external_id", userRS(sExternalIdField).Value
End If
If sOrganizationField > "" Then
dAttributes.Add "organization", Encode_UTF8(userRS(sOrganizationField).Value)
End If
If sTagsField > "" Then
dAttributes.Add "tags", userRS(sTagsField).Value
End If
If sPhotoUrlField > "" Then
dAttributes.Add "remote_photo_url", userRS(sPhotoUrlField).Value
End If
If sPhoneField > "" Then
dAttributes.Add "phone", userRS(sPhoneField).Value
End If
If sRoleField > "" Then
dAttributes.Add "role", userRS(sRoleField).Value
End If
If sCustomRoleIDField > "" Then
dAttributes.Add "custom_role_id", userRS(sCustomRoleIDField).Value
End If
If sLocaleField > "" Then
dAttributes.Add "locale", userRS(sLocaleField).Value
End If
If sLocaleIDField > "" Then
dAttributes.Add "locale_id", userRS(sLocaleIDField).Value
End If
' If you need more custom user fields, add additional entries below as well as above in the settings.
If sUserFieldKey1 > "" And sUserFieldValue1 > "" Then
dUserFields.Add Encode_UTF8(sUserFieldKey1), Encode_UTF8(userRS(sUserFieldValue1).Value)
End If
If sUserFieldKey2 > "" And sUserFieldValue2 > "" Then
dUserFields.Add Encode_UTF8(sUserFieldKey2), Encode_UTF8(userRS(sUserFieldValue2).Value)
End If
Set GetAuthenticatedUser = dAttributes
Else
Set GetAuthenticatedUser = Nothing
End if
userRS.Close
oConn.Close
End Function
%>