/
introspection_script.py
212 lines (201 loc) · 13.3 KB
/
introspection_script.py
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
# oxAuth is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
# Copyright (c) 2018, Gluu
#
# Author: Yuriy Zabrovarnyy
#
#
from org.gluu.oxauth.model.jwt import Jwt
from org.gluu.service.cdi.util import CdiUtil
from org.gluu.oxauth.model.crypto import OxAuthCryptoProvider
from org.gluu.model.custom.script.type.introspection import IntrospectionType
from java.net import HttpURLConnection, URL
from org.json import JSONArray, JSONObject
from java.lang import String
from java.io import BufferedReader, InputStreamReader
class Introspection(IntrospectionType):
def __init__(self, currentTimeMillis):
self.currentTimeMillis = currentTimeMillis
def init(self, customScript, configurationAttributes):
print "Introspection script. Initializing ..."
print "Introspection script. Initialized successfully"
return True
def destroy(self, configurationAttributes):
print "Introspection script. Destroying ..."
print "Introspection script. Destroyed successfully"
return True
def getApiVersion(self):
return 11
# Returns boolean, true - apply introspection method, false - ignore it.
# This method is called after introspection response is ready. This method can modify introspection response.
# Note :
# responseAsJsonObject - is org.codehaus.jettison.json.JSONObject, you can use any method to manipulate json
# context is reference of org.gluu.oxauth.service.external.context.ExternalIntrospectionContext (in https://github.com/GluuFederation/oxauth project, )
def modifyResponse(self, responseAsJsonObject, context):
print "Inside modifyResponse method of introspection script ..."
try:
# Getting user-info-jwt
ujwt = context.getHttpRequest().getParameter("ujwt")
print ujwt
if not ujwt:
print "UJWT is empty or null"
# Parse jwt
userInfoJwt = Jwt.parse(ujwt)
# Get auth-server keys
url = URL("https://gasmyr.gluu.org/jans-auth/restv1/jwks")
conn = url.openConnection()
conn.setDoOutput(True)
conn.setRequestMethod("GET")
conn.setRequestProperty("Content-type", "application/json")
if conn.getResponseCode() != 200:
print "Failed!!"
print conn.getResponseCode()
print conn.getResponseMessage()
else:
print "Success!! Able to connect for auth-server jwks"
print conn.getResponseCode()
print conn.getResponseMessage()
instr = conn.getInputStream()
instrreader = InputStreamReader(instr)
breader = BufferedReader(instrreader)
output = breader.readLine()
jsonResult = ""
while output != None:
if output != None:
jsonResult += output
output = breader.readLine()
# JWKS
jwks = JSONObject(jsonResult)
conn.disconnect()
# Validate JWT
authCryptoProvider = OxAuthCryptoProvider()
validJwt = authCryptoProvider.verifySignature(userInfoJwt.getSigningInput(), userInfoJwt.getEncodedSignature(), userInfoJwt.getHeader().getKeyId(), jwks, None, userInfoJwt.getHeader().getSignatureAlgorithm())
print validJwt
if validJwt == True:
print "user-info jwt is valid"
# Get claims from parsed JWT
jwtClaims = userInfoJwt.getClaims()
jansAdminUIRole = jwtClaims.getClaim("jansAdminUIRole")
print jansAdminUIRole.getString(0)
# role-scope mapping
scope = []
if jansAdminUIRole.getString(0) == 'api-viewer':
scope.append("https://jans.io/oauth/config/attributes.readonly")
scope.append("https://jans.io/oauth/config/acrs.readonly")
scope.append("https://jans.io/oauth/config/scopes.readonly")
scope.append("https://jans.io/oauth/config/scripts.readonly")
scope.append("https://jans.io/oauth/config/openid/clients.readonly")
scope.append("https://jans.io/oauth/config/smtp.readonly")
scope.append("https://jans.io/oauth/config/logging.readonly")
scope.append("https://jans.io/oauth/config/uma/resources.readonly")
scope.append("https://jans.io/oauth/config/database/ldap.readonly")
scope.append("https://jans.io/oauth/config/jwks.readonly")
scope.append("https://jans.io/oauth/config/fido2.readonly")
scope.append("https://jans.io/oauth/config/cache.readonly")
scope.append("https://jans.io/oauth/jans-auth-server/config/properties.readonly")
scope.append("https://jans.io/oauth/config/database/couchbase.readonly")
elif jansAdminUIRole.getString(0) == 'api-editor':
scope.append("https://jans.io/oauth/config/attributes.readonly")
scope.append("https://jans.io/oauth/config/attributes.write")
scope.append("https://jans.io/oauth/config/acrs.readonly")
scope.append("https://jans.io/oauth/config/acrs.write")
scope.append("https://jans.io/oauth/config/scopes.readonly")
scope.append("https://jans.io/oauth/config/scopes.write")
scope.append("https://jans.io/oauth/config/scripts.readonly")
scope.append("https://jans.io/oauth/config/scripts.write")
scope.append("https://jans.io/oauth/config/openid/clients.readonly")
scope.append("https://jans.io/oauth/config/openid/clients.write")
scope.append("https://jans.io/oauth/config/smtp.readonly")
scope.append("https://jans.io/oauth/config/smtp.write")
scope.append("https://jans.io/oauth/config/logging.readonly")
scope.append("https://jans.io/oauth/config/logging.write")
scope.append("https://jans.io/oauth/config/uma/resources.readonly")
scope.append("https://jans.io/oauth/config/uma/resources.write")
scope.append("https://jans.io/oauth/config/database/ldap.readonly")
scope.append("https://jans.io/oauth/config/database/ldap.write")
scope.append("https://jans.io/oauth/config/jwks.readonly")
scope.append("https://jans.io/oauth/config/jwks.write")
scope.append("https://jans.io/oauth/config/fido2.readonly")
scope.append("https://jans.io/oauth/config/fido2.write")
scope.append("https://jans.io/oauth/config/cache.readonly")
scope.append("https://jans.io/oauth/config/cache.write")
scope.append("https://jans.io/oauth/config/database/couchbase.readonly")
scope.append("https://jans.io/oauth/config/database/couchbase.write")
scope.append("https://jans.io/oauth/jans-auth-server/config/properties.readonly")
elif jansAdminUIRole.getString(0) == 'api-manager':
scope.append("https://jans.io/oauth/config/attributes.readonly")
scope.append("https://jans.io/oauth/config/attributes.write")
scope.append("https://jans.io/oauth/config/attributes.delete")
scope.append("https://jans.io/oauth/config/acrs.readonly")
scope.append("https://jans.io/oauth/config/acrs.write")
scope.append("https://jans.io/oauth/config/acrs.delete")
scope.append("https://jans.io/oauth/config/scopes.readonly")
scope.append("https://jans.io/oauth/config/scopes.write")
scope.append("https://jans.io/oauth/config/scopes.delete")
scope.append("https://jans.io/oauth/config/scripts.readonly")
scope.append("https://jans.io/oauth/config/scripts.write")
scope.append("https://jans.io/oauth/config/scripts.delete")
scope.append("https://jans.io/oauth/config/openid/clients.readonly")
scope.append("https://jans.io/oauth/config/openid/clients.write")
scope.append("https://jans.io/oauth/config/openid/clients.delete")
scope.append("https://jans.io/oauth/config/smtp.readonly")
scope.append("https://jans.io/oauth/config/smtp.write")
scope.append("https://jans.io/oauth/config/smtp.delete")
scope.append("https://jans.io/oauth/config/logging.readonly")
scope.append("https://jans.io/oauth/config/logging.write")
scope.append("https://jans.io/oauth/config/uma/resources.readonly")
scope.append("https://jans.io/oauth/config/uma/resources.write")
scope.append("https://jans.io/oauth/config/uma/resources.delete")
scope.append("https://jans.io/oauth/config/database/ldap.readonly")
scope.append("https://jans.io/oauth/config/database/ldap.write")
scope.append("https://jans.io/oauth/config/database/ldap.delete")
scope.append("https://jans.io/oauth/config/jwks.readonly")
scope.append("https://jans.io/oauth/config/jwks.write")
scope.append("https://jans.io/oauth/config/fido2.readonly")
scope.append("https://jans.io/oauth/config/fido2.write")
scope.append("https://jans.io/oauth/config/cache.readonly")
scope.append("https://jans.io/oauth/config/cache.write")
scope.append("https://jans.io/oauth/config/database/couchbase.readonly")
scope.append("https://jans.io/oauth/config/database/couchbase.write")
scope.append("https://jans.io/oauth/jans-auth-server/config/properties.readonly")
elif jansAdminUIRole.getString(0) == 'api-admin':
scope.append("https://jans.io/oauth/config/attributes.readonly")
scope.append("https://jans.io/oauth/config/attributes.write")
scope.append("https://jans.io/oauth/config/attributes.delete")
scope.append("https://jans.io/oauth/config/acrs.readonly")
scope.append("https://jans.io/oauth/config/acrs.write")
scope.append("https://jans.io/oauth/config/acrs.delete")
scope.append("https://jans.io/oauth/config/scopes.readonly")
scope.append("https://jans.io/oauth/config/scopes.write")
scope.append("https://jans.io/oauth/config/scopes.delete")
scope.append("https://jans.io/oauth/config/scripts.readonly")
scope.append("https://jans.io/oauth/config/scripts.write")
scope.append("https://jans.io/oauth/config/scripts.delete")
scope.append("https://jans.io/oauth/config/openid/clients.readonly")
scope.append("https://jans.io/oauth/config/openid/clients.write")
scope.append("https://jans.io/oauth/config/openid/clients.delete")
scope.append("https://jans.io/oauth/config/smtp.readonly")
scope.append("https://jans.io/oauth/config/smtp.write")
scope.append("https://jans.io/oauth/config/smtp.delete")
scope.append("https://jans.io/oauth/config/logging.readonly")
scope.append("https://jans.io/oauth/config/logging.write")
scope.append("https://jans.io/oauth/config/uma/resources.readonly")
scope.append("https://jans.io/oauth/config/uma/resources.write")
scope.append("https://jans.io/oauth/config/uma/resources.delete")
scope.append("https://jans.io/oauth/config/database/ldap.readonly")
scope.append("https://jans.io/oauth/config/database/ldap.write")
scope.append("https://jans.io/oauth/config/database/ldap.delete")
scope.append("https://jans.io/oauth/config/jwks.readonly")
scope.append("https://jans.io/oauth/config/jwks.write")
scope.append("https://jans.io/oauth/config/fido2.readonly")
scope.append("https://jans.io/oauth/config/fido2.write")
scope.append("https://jans.io/oauth/config/cache.readonly")
scope.append("https://jans.io/oauth/config/cache.write")
scope.append("https://jans.io/oauth/config/database/couchbase.readonly")
scope.append("https://jans.io/oauth/config/database/couchbase.write")
scope.append("https://jans.io/oauth/jans-auth-server/config/properties.write")
scope.append("https://jans.io/oauth/jans-auth-server/config/properties.readonly")
responseAsJsonObject.accumulate("scope", scope)
except Exception as e:
print "Exception occured. Unable to resolve role/scope mapping."
print e
return True